如何添加 C# for 循环的结果?

发布于 2024-10-24 01:54:36 字数 1814 浏览 1 评论 0原文

我将程序设置为询问分钟数和每分钟的费用,然后计算该电话的费用。我有一个 for 循环设置来执行 3 次。我的问题是如何显示所有 3 个通话的总费用?

这是我的代码:

using System;
using System.Collections.Generic;
using System.Text;


namespace phonecall
{
    class Call
    {
        public
        int callid;  //used with counter in the main()
        int minutes;     //minutes
        double costpermin;  //output for per minute cost
        double pricepercall;    //output for total cost


        public void getdata(int x) //this method gets the data and stores it in the class data members
        {
            callid = ++x;

            Console.WriteLine("Enter the number minutes: ");
            minutes = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Enter the price per minute: ");
            costpermin = Convert.ToDouble(Console.ReadLine());
            pricepercall = minutes * costpermin;
        }

        public void displaydata() //this method displays the values of the class data members
        {

            Console.WriteLine("Call Number: {0}", callid);
            Console.WriteLine("Number of Minutes: {0}", minutes);
            Console.WriteLine("Cost Per Minute: ${0}", costpermin);
            Console.WriteLine("Total cost of Call: ${0}", pricepercall);
            Console.WriteLine();

        }
    }


    class forloop
    {
        public static void Main()
        {
            Call myCall = new Call(); //instantiation of the Call class

            for (int x = 0; x < 3; x++) //will get the data for 3 calls
            {

                myCall.getdata(x); //calls the object method and takes the value of x to use when getting data from the user
                myCall.displaydata(); //calls the object method to display the data
            }
        }
    }
}

非常感谢任何帮助。

I have the program setup to ask for the amount of minutes and cost per minute then it calculates cost for that phone call. I have a for loop setup to go through this 3 times. My question is how do I display the combined cost of all 3 calls?

Here is my code:

using System;
using System.Collections.Generic;
using System.Text;


namespace phonecall
{
    class Call
    {
        public
        int callid;  //used with counter in the main()
        int minutes;     //minutes
        double costpermin;  //output for per minute cost
        double pricepercall;    //output for total cost


        public void getdata(int x) //this method gets the data and stores it in the class data members
        {
            callid = ++x;

            Console.WriteLine("Enter the number minutes: ");
            minutes = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Enter the price per minute: ");
            costpermin = Convert.ToDouble(Console.ReadLine());
            pricepercall = minutes * costpermin;
        }

        public void displaydata() //this method displays the values of the class data members
        {

            Console.WriteLine("Call Number: {0}", callid);
            Console.WriteLine("Number of Minutes: {0}", minutes);
            Console.WriteLine("Cost Per Minute: ${0}", costpermin);
            Console.WriteLine("Total cost of Call: ${0}", pricepercall);
            Console.WriteLine();

        }
    }


    class forloop
    {
        public static void Main()
        {
            Call myCall = new Call(); //instantiation of the Call class

            for (int x = 0; x < 3; x++) //will get the data for 3 calls
            {

                myCall.getdata(x); //calls the object method and takes the value of x to use when getting data from the user
                myCall.displaydata(); //calls the object method to display the data
            }
        }
    }
}

Any help would be very much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

允世 2024-10-31 01:54:36

让您的 getData() 方法返回调用的成本:

public double getdata(int x) //this method gets the data and stores it in the class data members
{
  callid = ++x ;
  Console.WriteLine("Enter the number minutes: ");
  minutes = Convert.ToInt32(Console.ReadLine());
  Console.WriteLine("Enter the price per minute: ");
  costpermin = Convert.ToDouble(Console.ReadLine());
  pricepercall = minutes * costpermin;
  return pricePercall; 
}

现在您可以将它们相加:

double sumPrices = 0;
for (int x = 0; x < 3; x++) //will get the data for 3 calls
{
   sumPrices+=myCall.getdata(x); //calls the object method and takes the value of x to use when getting data from the user
   myCall.displaydata(); //calls the object method to display the data
}

Have your getData() method return the cost of the call:

public double getdata(int x) //this method gets the data and stores it in the class data members
{
  callid = ++x ;
  Console.WriteLine("Enter the number minutes: ");
  minutes = Convert.ToInt32(Console.ReadLine());
  Console.WriteLine("Enter the price per minute: ");
  costpermin = Convert.ToDouble(Console.ReadLine());
  pricepercall = minutes * costpermin;
  return pricePercall; 
}

Now you can just sum them up:

double sumPrices = 0;
for (int x = 0; x < 3; x++) //will get the data for 3 calls
{
   sumPrices+=myCall.getdata(x); //calls the object method and takes the value of x to use when getting data from the user
   myCall.displaydata(); //calls the object method to display the data
}
嘿看小鸭子会跑 2024-10-31 01:54:36
    double costforallcalls;

    for(int i = 0; i < 3;i++)
    {
        costforallcalls += minutes * costpercall


    }
    double costforallcalls;

    for(int i = 0; i < 3;i++)
    {
        costforallcalls += minutes * costpercall


    }
桃扇骨 2024-10-31 01:54:36

只需创建一个变量来跟踪 for 循环中的成本,然后将其写出来

double runningTotal = 0;
for (int x = 0; x < 3; x++) //will get the data for 3 calls
{
    myCall.getdata(x); //calls the object method and takes the value of x to use when getting data from the user
    runningTotal += myCall.pricepercall;
    myCall.displaydata(); //calls the object method to display the data
    Console.WriteLine("Running Total: {0}", runningTotal);
}

或者您可以为每个调用创建一个对象并保留周围的值,然后在需要运行总计时对它们进行总计

var callList = new List<Call>();
for (int x = 0; x < 3; x++) //will get the data for 3 calls
{
    var myCall = new Call();
    myCall.getdata(x); //calls the object method and takes the value of x to use when getting data from the user
    myCall.displaydata(); //calls the object method to display the data
    callList.Add(myCall);
}


Console.WriteLine("Running Total: ${0}", callList.Sum (c => c.pricepercall));

显然,pricepercall 需要是公共的财产来做到这一点。

希望这有帮助。

Just create a variable that tracks you costs in the for loop and then write it out

double runningTotal = 0;
for (int x = 0; x < 3; x++) //will get the data for 3 calls
{
    myCall.getdata(x); //calls the object method and takes the value of x to use when getting data from the user
    runningTotal += myCall.pricepercall;
    myCall.displaydata(); //calls the object method to display the data
    Console.WriteLine("Running Total: {0}", runningTotal);
}

Or alternatively you could create an object for each call and keep the around and then total them when ever you needed the running total

var callList = new List<Call>();
for (int x = 0; x < 3; x++) //will get the data for 3 calls
{
    var myCall = new Call();
    myCall.getdata(x); //calls the object method and takes the value of x to use when getting data from the user
    myCall.displaydata(); //calls the object method to display the data
    callList.Add(myCall);
}


Console.WriteLine("Running Total: ${0}", callList.Sum (c => c.pricepercall));

Obviously pricepercall would need to be a public property to do this.

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文