【发布时间】:2011-03-19 16:35:15
【问题描述】:
我有程序设置询问分钟数和每分钟费用,然后它计算该电话的费用。我有一个 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
}
}
}
}
非常感谢任何帮助。
【问题讨论】:
标签: c# object methods for-loop