如何添加 C# for 循环的结果?
我将程序设置为询问分钟数和每分钟的费用,然后计算该电话的费用。我有一个 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
}
}
}
}
非常感谢任何帮助。
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 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
}
}
}
}
Any help would be very much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
让您的
getData()
方法返回调用的成本:现在您可以将它们相加:
Have your
getData()
method return the cost of the call:Now you can just sum them up:
只需创建一个变量来跟踪 for 循环中的成本,然后将其写出来
或者您可以为每个调用创建一个对象并保留周围的值,然后在需要运行总计时对它们进行总计
显然,pricepercall 需要是公共的财产来做到这一点。
希望这有帮助。
Just create a variable that tracks you costs in the for loop and then write it out
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
Obviously pricepercall would need to be a public property to do this.
Hope this helps.