gpt4 book ai didi

Java Codelab 逻辑错误

转载 作者:太空宇宙 更新时间:2023-11-04 12:56:54 26 4
gpt4 key购买 nike

有人可以解释一下为什么我会遇到逻辑错误吗?输出应该是输入·运行者·1·姓名:·输入·运行者·1·时间(单位:分钟):·输入·运行者·2·姓名:·输入·运行者·2·时间·(单位·分钟):·输入·运行者·3·姓名:·输入·运行者·3·时间·(单位:分钟):·运行者3↵运行者1↵运行者2↵

import java.util.Scanner;

public class Runners {

public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);

double t1,
t2,
t3;



System.out.print("Enter runner 1 name:");
String name1;
name1 = keyboard.nextLine();
System.out.print(" Enter runner 1 time (in minutes):");
String time1;
time1 = keyboard.nextLine();

System.out.print(" Enter runner 2 name:");
String name2;
name2 = keyboard.nextLine();
System.out.print(" Enter runner 2 time (in minutes):");
String time2;
time2 = keyboard.nextLine();

System.out.print(" Enter runner 3 name:");
String name3;
name3 = keyboard.nextLine();
System.out.print(" Enter runner 3 time (in minutes):");
String time3;
time3 = keyboard.nextLine();


t1 = Double.parseDouble(time1);
t2 = Double.parseDouble(time2);
t3 = Double.parseDouble(time3);




if(t1 > t2 && t1 > t3){
if(t2>t3){
System.out.print(" "+ name3 + "\n"+ name2 + "\n" + name1+ "\n");
}
else{
System.out.print(" "+ name2 + "\n" + name3 + "\n" + name1+ "\n");
}
}
else if(t2>t1 && t2>t3){
if(t1>t3){
System.out.print(" " + name3 + "\n" + name1 + "\n" + name2+ "\n");
}
else{
System.out.print(" " + name1 + "\n" + name3 + "\n" + name2+ "\n");
}
}
else if(t3>t2 && t3>t1){
if(t2>t1){
System.out.print(" " + name1 + "\n" + name2 + "\n" + name3+ "\n");
}
else{
System.out.print(" " + name2 + "\n" + name1 + "\n" + name3+ "\n");
}
}
else if(t3==t2 && t3==t1){
if(t2>t1){
System.out.print(" " + name1 + "\n" + name2 + "\n" + name3+ "\n");
}
else{
System.out.print(" " + name1 + "\n" + name2 + "\n" + name3 + "\n");
}




}
}
}

最佳答案

我来到这里寻找同一问题的答案,并研究了本示例中的代码。最后出现逻辑问题。第 67 行中的 AND 应该是 OR。您不能先设定 1 等于 2、2 等于 3 的条件,然后询问 2 是否大于 1 并期望它起作用。

    import java.util.Scanner;

public class Runners {

public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);

double t1,
t2,
t3;



System.out.print("Enter runner 1 name:");
String name1;
name1 = keyboard.nextLine();
System.out.print(" Enter runner 1 time (in minutes):");
String time1;
time1 = keyboard.nextLine();

System.out.print(" Enter runner 2 name:");
String name2;
name2 = keyboard.nextLine();
System.out.print(" Enter runner 2 time (in minutes):");
String time2;
time2 = keyboard.nextLine();

System.out.print(" Enter runner 3 name:");
String name3;
name3 = keyboard.nextLine();
System.out.print(" Enter runner 3 time (in minutes):");
String time3;
time3 = keyboard.nextLine();


t1 = Double.parseDouble(time1);
t2 = Double.parseDouble(time2);
t3 = Double.parseDouble(time3);




if(t1 > t2 && t1 > t3){
if(t2>t3){
System.out.print(" "+ name3 + "\n"+ name2 + "\n" + name1+ "\n");
}
else{
System.out.print(" "+ name2 + "\n" + name3 + "\n" + name1+ "\n");
}
}
else if(t2>t1 && t2>t3){
if(t1>t3){
System.out.print(" " + name3 + "\n" + name1 + "\n" + name2+ "\n");
}
else{
System.out.print(" " + name1 + "\n" + name3 + "\n" + name2+ "\n");
}
}
else if(t3>t2 && t3>t1){
if(t2>t1){
System.out.print(" " + name1 + "\n" + name2 + "\n" + name3+ "\n");
}
else{
System.out.print(" " + name2 + "\n" + name1 + "\n" + name3+ "\n");
}
}
else if(t3==t2 || t3==t1){
if(t2>t1){
System.out.print(" " + name1 + "\n" + name2 + "\n" + name3+ "\n");
}
else{
System.out.print(" " + name1 + "\n" + name2 + "\n" + name3 + "\n");
}




}
}
}

关于Java Codelab 逻辑错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35323210/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com