我有程序设置要求的分钟数量和每分钟的费用,然后它计算该电话费用。我有一个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
}
}
}
}
任何帮助将是非常赞赏。