【问题标题】:How do you add the results of a C# for-loop?如何添加 C# for 循环的结果?
【发布时间】:2011-03-19 16:35:15
【问题描述】:

我有程序设置询问分钟数和每分钟费用,然后它计算该电话的费用。我有一个 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
            }
        }
    }
}

非常感谢任何帮助。

【问题讨论】:

    标签: c# object methods for-loop


    【解决方案1】:

    让您的 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
    }
    

    【讨论】:

      【解决方案2】:
          double costforallcalls;
      
          for(int i = 0; i < 3;i++)
          {
              costforallcalls += minutes * costpercall
      
      
          }
      

      【讨论】:

        【解决方案3】:

        只需在 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 需要成为公共属性才能执行此操作。

        希望这会有所帮助。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2022-01-25
          • 1970-01-01
          • 2022-12-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-10-06
          • 1970-01-01
          相关资源
          最近更新 更多