2016-02-10 96 views
1

有人可以解釋爲什麼我得到邏輯錯誤。 輸出應該 輸入·澆道·1·名稱:·輸入·澆道·1·時間·(在·分鐘):·輸入·澆道·2·名稱:·輸入·澆道·2·時間·(在·分鐘):·輸入·亞軍·3·名稱:·(在·分·輸入·亞軍·3·時間):·Runner3↵ Runner1↵ Runner2↵Java Codelab邏輯錯誤

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

你的問題不是很清楚你能否詳細說明 – basic

+0

提示是:11。跑步 寫一個程序,詢問三名跑步者的名字和時間,幾分鐘內,他們每個人都花了 完成比賽。程序應顯示 命令中的跑步者姓名,他們已完成。我必須使輸出與我上面發佈的完全相同,當我發佈它時,我會得到一個邏輯錯誤。它說我的標準輸出內容不正確。 – jkjk

+0

沒有邏輯錯誤?它完全按照它應該的輸出。首先完成第一名,然後是第二名,然後是第三名。 – basic

回答

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"); 
     } 




} 
    } 
}