2016-03-21 120 views
-1

我在這個論壇上搜索有人遇到類似於我的問題,我無法找到爲什麼地球上我的程序不能正常工作,除了看起來堅實。我剛剛開始學習Java,所以我不確定這裏是否有某種語法錯誤,或者更多,但我一直在盯着它,而這個網站在過去的一個小時裏,我沒有看到怎麼了。陣列沒有正確初始化

public class BubbleSort { 

    public static void main(String[] args) { 
     int a, b, n, change, i, operations, choice; 
     Scanner input = new Scanner(System.in); 
     System.out.println("Would you like to add your own numbers?"); 
     System.out.println("enter '1' for no and '2' for yes"); 
     choice = input.nextInt(); 

     if (choice == 1) { 
      int[] array = { 
       5, 
       7, 
       3, 
       9, 
       1, 
       0, 
       6 
      }; 
      n = 7; 
     } else if (choice == 2) { 

      System.out.println("How many numbers would you like to add to the" + 
       "Array? (Add up to 10)"); 
      n = input.nextInt(); 

      int array[] = new int[n]; 

      System.out.println("Input " + n + " integers"); 

      for (a = 0; a < n; a++) { 
       array[a] = input.nextInt(); 
      } 
     } 

     System.out.println("Bubble Sort operation:"); 

     for (a = 0; a < (n - 1); a++) { 
      System.out.print("iteration " + (a + 1) + ": "); 

      for (b = 0; b < n - a - 1; b++) { 
       if (array[b] > array[b + 1]) { 

        change = array[b]; 
        array[b] = array[b + 1]; 
        array[b + 1] = change; 
        operations++; 

       } 
      } 
      for (i = 0; i < n; i++) { 
       System.out.print(array[i]); 
      } 
      System.out.println(); 
     } 

     System.out.print("Finished array after bubble sort: "); 
     for (i = 0; i < n; i++) { 
      System.out.print(array[i]); 
     } 
     System.out.println(); 
     System.out.println("This operation took " + operations + " cycles"); 
     System.out.println(); 
    } 
} 

這只是一個簡單的泡沫排序程序,但我在這裏嚴重難住。任何想法會有什麼錯誤?錯誤說n從不初始化。

+0

你得到的錯誤是什麼? – haihui

+0

這是說n沒有被初始化 –

+0

另外,它沒有認識到循環中的數組與if語句中初始化的數組相同 –

回答

2

Java編譯器告訴你這個,因爲變量n可能未被初始化。

「但是我做了!我寫了n = 7;n = input.nextInt();!這不是叫'初始化'嗎?」你問。你完全正確!你在這些地方初始化了n。但是,根據您的代碼,n僅在choice爲1或2時纔會初始化。如果choice爲3,該怎麼辦?或-999?如果是這樣的話,n將不會被初始化,對嗎?

比方說choice是-999。這兩個if語句將被跳過,並直接執行到冒泡排序部分。被執行後,談到這個代碼:

System.out.print("Finished array after bubble sort: "); 
    for (i = 0; i < n; i++) { 
     System.out.print(array[i]); 
    } 
    System.out.println(); 
    System.out.println("This operation took " + operations + " cycles"); 
    System.out.println(); 

和它說,

Waaaaait一分鐘,對於環說,我需要n比較i決定循環是否應該繼續被執行。但我甚至不知道什麼是價值!

這就是爲什麼它抱怨這一點。

如何來解決這個問題:

這取決於你是否把其他號碼爲無效的輸入。如果你這樣做,你可以寫一個else子句的if語句

else { 
    System.out.println("Input Invalid. Program will now exit."); 
    return; 
} 

如果你想,就好像用戶已經進入2對待其他數字,改變else if子句人。或者你可以只寫

n = 0; 

在開頭。

此外,我注意到你有另一個錯誤。在if語句,你寫了這個:

int[] array = { 
    5, 
    7, 
    3, 
    9, 
    1, 
    0, 
    6 
}; 

這:

int array[] = new int[n]; 

這兩個數組不會if語句外部訪問。我認爲你只需要在if語句之外聲明一個數組,如下所示:

int[] array = { 
    5, 
    7, 
    3, 
    9, 
    1, 
    0, 
    6 
}; 
if (choice == 1) { 
    // The original declaration should be removed 
    n = 7; 
} else { 
    // ... 
} 
+0

感謝您的輸入!我把n = 0,然後刷新程序,它的工作。然後我回到這裏說了些什麼,我看到你讚揚了它!哈哈。如果數字不是1或2,我還打算在該部分添加關於退出的部分。謝謝!現在我只需要弄清楚爲什麼程序不會像我選擇選項1時那樣喜歡我的數組。我不想像數組[0] = 5 array [1] =等等等等等等。 java中的列表/數組與python相比是一場噩夢......而且你比我更快!非常感謝您的幫助,您可以在這裏保存我的成績 –