2011-03-19 61 views
0

我有程序设置要求的分钟数量和每分钟的费用,然后它计算该电话费用。我有一个for循环设置来通过这个3次。我的问题是如何显示所有3个电话的总成本?如何添加C#for-loop的结果?

这里是我的代码:

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 
      } 
     } 
    } 
} 

任何帮助将是非常赞赏。

回答

0
double costforallcalls; 

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


    } 
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 
} 
0

只需创建一个跟踪变量,你的成本for loop然后写出来

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将需要一个公共属性来做到这一点。

希望这会有所帮助。