我有程序设置要求分钟数和每分钟成本,然后计算该电话的费用。我有一个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
}
}
}
}
非常感谢任何帮助。
答案 0 :(得分: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
}
答案 1 :(得分:0)
double costforallcalls;
for(int i = 0; i < 3;i++)
{
costforallcalls += minutes * costpercall
}
答案 2 :(得分:0)
只需创建一个跟踪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需要是一个公共财产才能做到这一点。
希望这有帮助。