有人可以解释为什么我会遇到逻辑错误。 输出应该是 输入·浇道·1·名称:·输入·浇道·1·时间·(在·分钟):·输入·浇道·2·名称:·(在·分钟)·输入·浇道·2·时间:·输入·亚军·3·名称:·输入·亚军·3·时间·(在·分):·Runner3↵ Runner1↵ Runner2↵
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");
}
}
}
}
答案 0 :(得分:0)
我来到这里寻找同一问题的答案,并在这个例子中探讨了代码。最后有一个逻辑问题。第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");
}
}
}
}