我正在研究一个程序,需要生成一个三位数的随机数,然后扫描每个数字以便与猜测游戏的输入进行比较。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位数的输入,所以我使用扫描仪分别分离每个数字,并将其与三位猜测输入进行对比,以便我们可以打印出他们正确猜测数字的“提示”。 –