2016-05-28 71 views
1

我已經成功地編譯這個java程序(這會產生0到25之間的100個隨機數字,把它們放在一個陣列,並將它們分類成基於兩種不同的陣列,每個陣列是偶數還是奇數),儘管它不運行。我懷疑我在一個while循環中犯了一個錯誤,儘管我不確定。另外,我掙扎着爬在正確格式化的問題的代碼,所以標籤是有點不同,但它仍然是大多清晰可辨。這裏是文字的.java:我需要幫助,我介紹到Java分配

public class Assignment8 
{ 
    public static void main(String [] args) 
    { 
     int storage [] = new int[100]; 
     int j = 0; 

     while (storage.length < 100) { 
      int testVariable = 0 + (int) (Math.random() * ((25 - 0) + 1)); 
      storage[j] = testVariable; 
      j++; 
     } 

     int oddArray[] = OddNumbers(storage); 
     int evenArray[] = EvenNumbers(storage); 
     int currentNumber = 0; 

     System.out.println("The odd numbers are: " + "\n"); 

     while (currentNumber <= 99) { 
      System.out.println(oddArray[currentNumber] + "\n"); 
      currentNumber++; 
     } 

     System.out.println("\n" + "The even numbers are: " + "\n"); 
     currentNumber = 0; 

     while (currentNumber <= 99) { 
      System.out.println(evenArray[currentNumber] + "\n"); 
      currentNumber++; 
     } 
    } 

    public static int[] OddNumbers(int storage[]) 
    { 
     int currentNumber = 0; 
     int currentValue = storage[currentNumber]; 
     int oddArray[] = new int[100]; 

     while (currentNumber <= 99) { 
      if (storage[currentNumber] % 2 != 0) { 
       oddArray[currentNumber] = currentValue; 
      } else { 
       continue; 
      } 

      currentNumber++;   
     } 

     return oddArray; 
    } 

    public static int[] EvenNumbers(int storage[]) 
    { 
     int currentNumber = 0; 
     int currentValue = storage[currentNumber]; 
     int evenArray[] = new int[100]; 

     while (currentNumber <= 99) { 
      if (storage[currentNumber] % 2 == 0) { 
       evenArray[currentNumber] = currentValue; 
      } else { 
       continue; 
      } 

      currentNumber++;   
     } 

     return evenArray; 
    } 
} 
+0

的地方,開始是在你的IDE調試一次通過一行代碼步驟。你做到了嗎?如果是這樣,告訴我們你發現你不明白。 –

+0

請包括示例輸出或錯誤。 – nateyolles

+0

這不是重點,但爲什麼寫'25-0'而不是'25' – nhouser9

回答

2

storage.length沒有整個程序的執行發生改變,因爲陣列已分配。你先while循環因此錯誤的,因爲100是不小於100,它永遠不會執行。相反,你可以使用一個簡單的循環for

for (int j = 0; j < storage.length; ++j) { 
    int testVariable = 0 + (int) (Math.random() * ((25 - 0) + 1)); 
    storage[j] = testVariable; 
} 
1

雖然它不運行

是它。這只是執行可能會卡在OddNumbersEvenNumbers方法的無限循環。 在此細看:

while (currentNumber <= 99) { 
    if (storage[currentNumber] % 2 != 0) { 
     oddArray[currentNumber] = currentValue; 
    } 
    else { 
     continue; 
    } 

的問題是,當storage[currentNumber]均勻, 程序執行else分支與continue聲明, 以來currentNumber一直沒有改變,所以storage[currentNumber]也沒有任何變化,依舊是偶數,且else分支將再次執行,又一次,又一次,直到永遠。 EvenNumber也有同樣的問題。

下面是OddNumbers修復:

public static int[] OddNumbers(int[] storage) { 
    int[] oddArray = new int[storage.length]; 

    int oddIndex = 0; 
    for (int num : storage) { 
     if (num % 2 != 0) { 
      oddArray[oddIndex++] = num; 
     } 
    } 

    return Arrays.copyOf(oddArray, oddIndex); 
} 

一個額外的觸摸我的這個方法的確是Arrays.copyOf電話, 斬去陣列的多餘元素,這將是否則爲0

然後當您在main中打印此數組的內容時,請這樣寫:

System.out.println("The odd numbers are: " + "\n"); 

for (int num : oddArray) { 
    System.out.println(num); 
} 

按照相同的模式修復EvenNumbers

As @Mureinik指出, 環路main填充storage也被打破。 而且你還有其它的編碼問題, 例如隨機數產生特別難看,並且使用過時的技術。

完整的改進執行:

import java.util.Arrays; 
import java.util.Random; 

public class Assignment8 { 
    public static void main(String[] args) { 
     Random random = new Random(); 

     int[] storage = new int[100];  
     for (int i = 0; i < storage.length; i++) { 
      storage[i] = random.nextInt(25); 
     } 

     System.out.println("The odd numbers are: " + "\n"); 

     int oddArray[] = OddNumbers(storage); 
     for (int num : oddArray) { 
      System.out.println(num); 
     } 

     System.out.println("\n" + "The even numbers are: " + "\n"); 

     int evenArray[] = EvenNumbers(storage); 
     for (int num : evenArray) { 
      System.out.println(num); 
     } 
    } 

    public static int[] OddNumbers(int[] storage) { 
     int index = 0; 
     int[] result = new int[storage.length]; 

     for (int num : storage) { 
      if (num % 2 != 0) { 
       result[index++] = num; 
      } 
     } 

     return Arrays.copyOf(result, index); 
    } 

    public static int[] EvenNumbers(int storage[]) { 
     int index = 0; 
     int[] result = new int[storage.length]; 

     for (int num : storage) { 
      if (num % 2 == 0) { 
       result[index++] = num; 
      } 
     } 

     return Arrays.copyOf(result, index); 
    } 
}