只应运行一次的方法正在运行两次(Method being run through twice when it should only run once)
class Myclass
{
public string Driver1()
{
string a = "";
Console.Write("Please enter drivers name: ");
a = Console.ReadLine();
return a;
}
public int numberOfTrips()
{
int a = 0;
{
Console.Write("Enter the number of trips: ");
a = Convert.ToInt32(Console.ReadLine());
}
return a;
}
public List<float> Payments()
{
List<float> a = new List<float>();
float input;
for (int i = 0; i<numberOfTrips(); i++)
{
Console.Write("Enter payment {0}: ", (1 + i));
input = float.Parse(Console.ReadLine());
Console.WriteLine("Payment added");
a.Add(input);
}
return a;
}
}
class Program
{
static void Main(string[] args)
{
Myclass a = new Myclass();
string name = a.Driver1();
int trip = a.numberOfTrips();
float total = a.Payments().Sum();
Console.WriteLine("\nDriver: {0}\n" + "Number of trips: {1}\n" + "Total payment: {2}\n", name, trip, total);
}
}
我遇到的问题是“public int numberOfTrips()”方法在它到达包含for循环的方法之前运行两次。 我认为这与我在for循环中使用它来指定循环何时停止的事实有关。 我猜我已经做了这个错误,所以我会如何纠正这一点? 我需要用户能够设置要求付款的次数。
任何帮助表示赞赏。
class Myclass { public string Driver1() { string a = ""; Console.Write("Please enter drivers name: "); a = Console.ReadLine(); return a; } public int numberOfTrips() { int a = 0; { Console.Write("Enter the number of trips: "); a = Convert.ToInt32(Console.ReadLine()); } return a; } public List<float> Payments() { List<float> a = new List<float>(); float input; for (int i = 0; i<numberOfTrips(); i++) { Console.Write("Enter payment {0}: ", (1 + i)); input = float.Parse(Console.ReadLine()); Console.WriteLine("Payment added"); a.Add(input); } return a; } } class Program { static void Main(string[] args) { Myclass a = new Myclass(); string name = a.Driver1(); int trip = a.numberOfTrips(); float total = a.Payments().Sum(); Console.WriteLine("\nDriver: {0}\n" + "Number of trips: {1}\n" + "Total payment: {2}\n", name, trip, total); } }The issue i am having is that the "public int numberOfTrips()" method is running twice before it gets to the method containing the for loop. I think this is to do with the fact i am using it within the for loop to specify when the loop should stop. I am guessing i have done this wrong so how would i correct this? I need the user to be able to set the how many times it will ask for payment.
Any help is appreciated.
最满意答案
只需将numberOfTrips的号码作为参数传递给Payments :
public List<float> Payments(int tripCount) { List<float> a = new List<float>(); float input; for (int i = 0; i < tripCount; i++) { Console.Write("Enter payment {0}: ", (1 + i)); input = float.Parse(Console.ReadLine()); Console.WriteLine("Payment added"); a.Add(input); } return a; }在你的Main方法中:
Myclass a = new Myclass(); string name = a.Driver1(); int trip = a.numberOfTrips(); float total = a.Payments(trip).Sum();Just pass the number from numberOfTrips as a parameter to Payments:
public List<float> Payments(int tripCount) { List<float> a = new List<float>(); float input; for (int i = 0; i < tripCount; i++) { Console.Write("Enter payment {0}: ", (1 + i)); input = float.Parse(Console.ReadLine()); Console.WriteLine("Payment added"); a.Add(input); } return a; }In your Main method:
Myclass a = new Myclass(); string name = a.Driver1(); int trip = a.numberOfTrips(); float total = a.Payments(trip).Sum();更多推荐
发布评论