2016-05-17 134 views
-2

在此代碼getRandomInt()錯誤未定義

class randomArray 
{ 
    int[] array1; 
    randomArray() 
{ 

Scanner userInput = new Scanner(System.in); 
do{ 
    System.out.println("Please enter an interger value between 5-10"); 
    array1 = new int[userInput.nextInt()]; 
}while(array1.length>10||array1.length<5); 
} 
public void assignment() 
{ 
    for(int counter=array1.length-1;counter >=0;counter--) 
{ 
    array1[counter] = getRandomInt(1, 50);//(Math.random()*(50-1))+1; 
} 
} 
public void display() 
{ 
    System.out.println("Array="+array1); 
} 
} 

我得到的錯誤 的方法getRandomInt(INT,INT)是未定義的類型randomArray randomArray是我的類名

能有人告訴我在這個錯誤是什麼意思

我使用java的博士與Eclipse編譯器

+0

你在哪裏挖掘'array1'? –

+3

顯示您的完整課程,因爲錯誤表示該方法在您的課程中不存在。最有可能的錯字錯誤 –

+0

@sᴜʀᴇsʜᴀᴛᴛᴀ更多代碼添加 – kiwiOceAn

回答

1

我得到錯誤的方法getRandomInt(INT,INT)是未定義的類型randomArray randomArray是我的類名

誰能告訴我的這個錯誤是什麼意思

這意味着什麼說,您尚未添加方法和簽名getRandomInt(int, int)randomArray。喜歡的東西

public static int getRandomInt(int a, int b) { 
    return (int) ((Math.random() * (b - a)) + a); 
} 

基於對您的評論的代碼。

+0

當我嘗試使用(Math.random()*(50-1))+ 1;我有一個評論它說我的數組是int,我做了它,但然後Math.random返回一個雙因此這兩個不會適合有任何方式我可以使用Math.random,但把它放到int ?? @ Elliott Frisch – kiwiOceAn

+0

**是**。看我的*答案*; 'array1 [counter] =(int)((Math.random()*(50 - 1))+ 1);' –