2013-07-10 66 views
0
public class AssignmentChapter8 
{ 
    public static void main(String[] args) 
    { 
     int randomNumbers = new int[100]; 
     int counter = 0; 

     while(counter < randomNumbers.length) 
     { 
      randomNumbers[counter] = (int)(Math.random() * 25); 
      counter++; 
     } 

     int oddNumbers[] = new int[100]; 
     oddNumbers[] = getOddNumbers(randomNumbers); 
     int evenNumbers[] = new int[100]; 
     evenNumbers[] = getEvenNumbers(randomNumbers); 

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

     for(int k = 0; k < oddNumbers.length; k++) 
      System.out.print("\t" + oddNumbers[k]); 

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

     for(int l = 0; l < evenNumbers.length; l++) 
      System.out.print("\t" + evenNumbers[l]); 
    } 
    public static int getOddNumbers(int randomNumbers) 
    { 
     int oddNumbers[] = new int[100]; 
     int counterA = 0; 
     int counterB = 0; 
     int counter = 0; 
     int placeholder; 

     while(counter < randomNumbers.length) 
     { 
      if(randomNumbers[counterA] % 2 > 0) 
      { 
       oddNumbers[counterB] = randomNumbers[counterA]; 
       counterB++; 
      } 

      counterA++; 
      counter++; 
     } 

     return oddNumbers; 
    } 
    public static int getEvenNumbers(int randomNumbers) 
    { 
     int evenNumbers[] = new int[100]; 
     int counterA = 0; 
     int counterB = 0; 
     int counter = 0; 
     int placeholder; 

     while(counter < randomNumbers.length) 
     { 
      if(randomNumbers[counterA] % 2 > 0) 
      { 
       evenNumbers[counterB] = randomNumbers[counterA]; 
       counterB++; 
      } 

      counterA++; 
      counter++; 
     } 

     return evenNumbers; 
    } 
} 

我一直在試圖執行一個程序來對數組中的變量進行排序,但我不斷地得到';'在程序應該從函數中檢索數組的聲明之後,行中的期望錯誤。任何幫助,將不勝感激。錯誤';'期望在數組中定義,;已經使用

+1

你需要解決這個問題:int randomNumbers = new int [100];你聲明瞭一個arry do:int [] randomNumbers = new int [100]; – Matt

回答

1

這是不好的語法(其導致';' expected error):

oddNumbers[] = getOddNumbers(randomNumbers); 

不需要托架。你可以這樣做:

oddNumbers = getOddNumbers(randomNumbers); 

除此之外,你有大量的錯誤:

int randomNumbers[] = new int[100]; // you need the brackets 

在方法聲明你的返回值是錯誤的(你是返回一個數組,而不是一個int):

public static int[] getEvenNumbers(int randomNumbers) 
0
int oddNumbers[] = new int[100]; 

通過初始化成爲oddNumbers的陣列。當你想分配數值時,

oddNumbers={elements of array} 

左右兩側都應該是數組。

也許你應該使用IDE進行編碼,然後它可以幫助你理解這樣的一些問題。

相關問題