2013-01-11 210 views
0

我正在嘗試創建一個程序,提供比賽中前5名的地方。代碼編譯,但是當你運行它時,有一個明顯的邏輯錯誤。它重複在所有5位相同的決賽,所以第一名被放置在第一,第二,第三,等等...嵌套if語句的邏輯錯誤

這裏是我的代碼:

import java.util.Scanner; 

public class Assignment0 
{ 
    public static void main (String [] args) 
    { 
     int numberOfLanes; 
     int lane = 0; 
     double first; 
     double second; 
     double third; 
     double fourth; 
     double fifth; 
     double [] time = null; 

     Scanner keyboard = new Scanner (System.in); 

     System.out.println ("How many lanes hold competitors?"); 
     numberOfLanes = keyboard.nextInt(); 

     time = new double [numberOfLanes]; 
     for (int i = 0; i < numberOfLanes; i++) 
     { 
      System.out.println("Enter time for lane " + i); 
      time [i] = keyboard.nextDouble(); 
     } 

     System.out.println(); 
     System.out.println(); 

     first = time[0]; 

     for (int i = 0; i < time.length; i++) 
     { 
      if (time[i] < first) 
       { 
        first = time[i]; 
        lane = i; 
       } 
     } 

     System.out.println("First place = Lane " + lane + ". Time = " + first + " seconds."); 

     second = time[0]; 

     for (int i = 0; i < time.length; i++) 
     { 
      if (time [i] > first) 
       if (time [i] < second) 
       { 
        second = time[i]; 
        lane = i; 
       } 
     } 

     System.out.println("Second place = Lane " + lane + ". Time = " + second + " seconds."); 

     third = time[0]; 

     for (int i = 0; i < time.length; i++) 
     { 
      if (time[i] > second) 
       if (time[i] < third) 
       { 
        third = time[i]; 
        lane = i; 
       } 

     } 

     System.out.println("Third place = Lane " + lane + ". Time = " + third + " seconds."); 

     fourth = time[0]; 

     for (int i = 0; i < time.length; i++) 
     { 
      if (time[i] > third) 
       if (time[i] < fourth) 
          {  
           fourth = time[i]; 
           lane = i; 
          } 
     } 


     System.out.println("Fourth place = Lane " + lane + ". Time = " + fourth + " seconds."); 

     fifth = time[0]; 

     for (int i = 0; i < time.length; i++) 
     { 
      if (time [i] > fourth) 
      if (time[i] < fifth) 
       { 
       fifth = time[i]; 
       lane = i; 
       } 
     } 


     System.out.println("Fifth place = Lane " + lane + ". Time = " + fifth + " seconds."); 
    } 
} 

輸出看起來是這樣的:

How many lanes hold competitors? 
5 
Enter time for lane 0 
9.72 
Enter time for lane 1 
9.8 
Enter time for lane 2 
9.82 
Enter time for lane 3 
9.86 
Enter time for lane 4 
9.9 


First place = Lane 0. Time = 9.72 seconds. 
Second place = Lane 0. Time = 9.72 seconds. 
Third place = Lane 0. Time = 9.72 seconds. 
Fourth place = Lane 0. Time = 9.72 seconds. 
Fifth place = Lane 0. Time = 9.72 seconds. 

回答

0

首先,這不是接近問題的一個好辦法。但是,如果我們堅持使用這種方法,解決的辦法是初始化secondthirdfourthfifth這樣的:

second = Double.MAX_VALUE; 

等了3日至5日。你也需要把這個作爲你的代碼的頭:

import java.lang.Double; 

不過請注意,即使上面的修復,如果兩個車道有相同的時間第二車道將被跳過,最後車道將有一個時間MAX_VALUE,所以你需要完全重寫代碼來使用數組並對它們進行排序。

1

您可以設置second = time[0];third = time[0]等之前,每個循環,使每個for循環if語句條件永真,因此變量secondthird等從來沒有從time[0]和012改變永不改變0

您應該用Double.MAX_VALUE來初始化它們。具體而言,在聲明中,使用下面的代碼:

int numberOfLanes; 
    int lane = 0; 
    double first = Double.MAX_VALUE; 
    double second = Double.MAX_VALUE; 
    double third = Double.MAX_VALUE; 
    double fourth = Double.MAX_VALUE; 
    double fifth = Double.MAX_VALUE; 
    double [] time = null; 

,並刪除所有second = time[0];third = time[0],等等。

+0

我應該發起變量數組中不同的元素?我知道,爲了使代碼與變量一起編譯,它必須被初始化爲某個值。 –

+0

將每個變量初始化爲'Double.MAX_VALUE',你應該很好去。我會用特定的代碼更新我的帖子。 –

+0

謝謝!代碼現在正在工作。 –

0

你的初始化是怪異的,而不是設置first = time[0]和這樣的,只是它們初始化爲無窮大並且刪除個人作業線。

double first = Double.POSITIVE_INFINITY; 
double second = Double.POSITIVE_INFINITY; 
double third = Double.POSITIVE_INFINITY; 
double fourth = Double.POSITIVE_INFINITY; 
double fifth = Double.POSITIVE_INFINITY; 
0
first = time[0]; 

直到

fifth = time[0]; 
+0

你能詳細說明你的答案嗎? – afuzzyllama

0

變化second = time[0];second = time[1];

third = time[0];third = time[2];

fourth = time[0];fourth = time[3];

fifth = time[0];fifth = time[4];

+0

只適用於已經按順序的特定數據集。 –

+0

我用不同的數據集嘗試了這個解決方案,它工作。該代碼不是解決問題的最佳方法;所以我會繼續自行研究這個計劃,並繼續研究它。如果任何人有任何建議,信譽良好的文章或網站進行審查,我將不勝感激任何幫助。 –

+0

@ KenY-N:我知道這隻適用於這個特定的數據集。我只是指出了邏輯錯誤。 – Raed

0

您在比較時間的循環之前無條件地分配second和所有進一步的排名。因此,如果最快的時間被放置爲元素[0],您將獲得觀察到的行爲。你應該閱讀約sort

0

這個怎麼樣:

String[] data = new String[]{"First","Second","Third", "Forth", "Fifth"}; 
    int num[] = new int[]{0,1,2,3,4}; 
    //now we sort the array(time) and we note the index 
    for(int a=0;a<numberOfLanes;a++){ 
     for(int b=a+1;b<numberOfLanes;b++){ 
      if(time[a]>time[b]){ 
       int temp=time[a]; 
       time[a] = time[b]; 
       time[b] = temp; 

       temp = num[a]; 
       num[a] = num[b]; 
       num[b] = temp; 
      } 
     } 
    } 
    //-- 
    //print the output 
    for(int a=0;a<numberOfLanes;a++){ 
     System.out.println(data[a]+" place = Lane "+num[a]+". Time = "+time[a]+" seconds."); 
    } 
    //-- done