How do you add the results of a C# for-loop?
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 开发者_如何学Goprice 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.
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
}
double costforallcalls;
for(int i = 0; i < 3;i++)
{
costforallcalls += minutes * costpercall
}
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.
精彩评论