只应运行一次的方法正在运行两次(Method being run through twice when it should only run once)

编程入门 行业动态 更新时间:2024-10-28 12:18:22
只应运行一次的方法正在运行两次(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();

更多推荐

本文发布于:2023-07-27 14:18:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1291662.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:两次   正在运行   方法   run   Method

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!