我的循环任务存在编码问题,更多信息如下
我的任务是仅使用 for 循环和数学函数创建一个程序,该程序输入 N 辆汽车,每次迭代都会获取汽车到达终点所需的时间,最终输出需要是第一个到达终点的汽车,第二个,意味着两次最低的时间。
我创建了一个程序:
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("enter total cars: ");
int cars = sc.nextInt();
double first = 0, second = 0;
int car1 = 1, car2 = 1;
for (int i = 1; i <= cars; i++) {
System.out.println("enter car number " + i + " speed:");
double speed = sc.nextDouble();
if (i == 1) {
first = speed;
second = speed;
}
if (speed < first) {
second = first;
first = speed;
car2 = car1;
car1 = i;
} else {
if (speed < second) {
car2 = i;
second = speed;
}
}
}
System.out.println("car number one is " + car1 +" with speed of " + first);
System.out.println("car number two is " + car2 +" with speed of " + second);
}
}
我遇到的问题是如何启动第一个和第二个变量,因为它们不能设置为 0,因为我需要检查我获得的速度是否低于第一个或第二个。我可能在第一次迭代中尝试将第一和第二设置为速度以开始一些东西,但它仍然有错误,所以我想知道如何修复它? 谢谢。
I have a task to create a program using only for loop and math functions that input N amount of cars and each iteration gets the time it took the car to reach the end, the final output needs to be the first one to reach the end and the second, meaning the two lowest times.
I've created a program:
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("enter total cars: ");
int cars = sc.nextInt();
double first = 0, second = 0;
int car1 = 1, car2 = 1;
for (int i = 1; i <= cars; i++) {
System.out.println("enter car number " + i + " speed:");
double speed = sc.nextDouble();
if (i == 1) {
first = speed;
second = speed;
}
if (speed < first) {
second = first;
first = speed;
car2 = car1;
car1 = i;
} else {
if (speed < second) {
car2 = i;
second = speed;
}
}
}
System.out.println("car number one is " + car1 +" with speed of " + first);
System.out.println("car number two is " + car2 +" with speed of " + second);
}
}
the problem I had is how to start the first and second var, as they cant be set to 0, because I need to check if the speed I get is lower than the first or the second. i tried maybe on the first iteration to set first and second to speed to have something to start with, but it still bugs, so I wondered how could I fix it?
thanks ahead.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
下面是下面的工作代码,它可以让用户输入任意数量的汽车,询问其所用时间,然后打印两个最低时间的汽车编号及其各自所用的时间:
重要的部分是您可以初始化时间使用
Double.MAX_VALUE
的double
值来确保前 2 辆汽车始终替换初始值:除此之外,代码确实具有令人困惑的变量名称。我使用了更长的、更有意义的名称,以使代码更容易理解。
运行示例:
Here is the working code below that will let the user enter any number of cars, ask for their time taken, and then print the car number of the two lowest times and their respective time taken:
The important part is that you can initialize the time
double
values usingDouble.MAX_VALUE
in order to ensure the first 2 cars will always replace the initial values:Besides that the code honestly has the confusing variable names. I used longer names that have more meaning in order to make the code easier to understand.
Example Run:
那么会是这样的事情吗?
}
So it will be something like this?
}