我正在研究一個程序,需要生成一個三位數的隨機數,然後掃描每個數字以便與猜測遊戲的輸入進行比較。Java:找不到合適的構造函數掃描器(int)
我沒有初始化實例變量,我只是沒有把它們放在這裏。我也有其他方法,我認爲這不會影響我現在遇到的問題。老實說,我對編程和Java很陌生,所以它可能比我想象的要複雜得多。但我的問題是,當我創建一個名爲randScan的掃描器對象並嘗試將其設置爲掃描我的secretNumber對象(隨機生成)時,出現一個錯誤消息,提示「找不到適合Scanner(int)的構造函數...」和那麼它下面的很多其他錯誤(輸入方式太多)。我只是不明白爲什麼它不會掃描randomNumber,因爲它是一個int。
任何幫助將不勝感激! :)
import java.util.Random;
import java.util.Scanner;
import javax.swing.JOptionPane;
// Generates three random single digit ints. The first cannot be zero
// and all three will be different. Called by public method play()
public String generateSecretNumber()
{
do
{
secretNumber = (int)(generator.nextInt(888)+101) ;
// scan the random integer
Scanner randScan = new Scanner(secretNumber) ; //<- THIS IS THE PROBLEM!
num1 = randScan.nextInt(); // scan the first digit
num2 = randScan.nextInt() ; // scan the second digit
num3 = randScan.nextInt() ; // scan the third digit
}
while (num1 == 0 || num1 == num2 ||
num2 == num3 || num1 == num3) ; // re-generate if any digits are the same
return number ;
爲什麼你需要'掃描儀? – NilsH
我需要用它來比較另一個3位數的輸入,所以我使用掃描儀分別分離每個數字,並將其與三位猜測輸入進行對比,以便我們可以打印出他們正確猜測數字的「提示」。 –