【问题标题】:Java: No suitable constructor found for Scanner(int)Java:没有为 Scanner(int) 找到合适的构造函数
【发布时间】:2013-03-24 16:08:01
【问题描述】:

我正在开发一个程序,该程序需要生成一个三位数的随机数,然后扫描每个数字以与猜谜游戏的输入进行比较。

我确实初始化了实例变量,只是没有把它们放在这里。我也有其他方法,我认为这不会影响我现在遇到的问题。

老实说,我对编程和 Java 还很陌生,所以它可能没有我想象的那么复杂。但我的问题是,当我创建一个名为 randScan 的 Scanner 对象并尝试将其设置为扫描我的 secretNumber 对象(随机生成)时,我收到一条错误消息,提示“没有为 Scanner(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 ; 

【问题讨论】:

  • 为什么需要Scanner
  • 我需要使用它来与另一个 3 位输入进行比较,因此我使用 Scanner 分别分隔每个数字并将其与猜测的三位输入进行对比,以便我可以打印出“提示”他们猜对的数字。

标签: java object netbeans int java.util.scanner


【解决方案1】:

您应该通过

处理secretNumber
String secretNumberString = new String(secretNumber);

作为字符串,然后你需要尝试Scanner#hasNextInt
根据文档

Returns true if the next token in this scanner's input can be 
interpreted as an int value in the default radix using the nextInt() method.
The scanner does not advance past any input.


所以我想它可能会解决您的问题
所以你的代码就像

secretNumber = (int)(generator.nextInt(888)+101) ;              
       String secretNumberString = new String(secretNumber);
       Scanner randScan = new Scanner(secretNumberString) ;  
        if(randScan.hasNextInt())
            num1 = randScan.nextInt();
           //Remaining code

【讨论】:

    【解决方案2】:

    Available constructors for scanner:

    Scanner(File source) 
    Scanner(File source, String charsetName) 
    Scanner(InputStream source) 
    Scanner(InputStream source, String charsetName) 
    Scanner(Readable source) 
    Scanner(ReadableByteChannel source)
    Scanner(ReadableByteChannel source, String charsetName)
    Scanner(String source)
    

    对于单个数字,您可能应该使用 String.valueOf(yourInt) 传递一个字符串

    【讨论】:

      【解决方案3】:

      如果您只是想获取secretNumber 的三位数字(作为整数值),您可以使用:

      num1 = secretNumber / 100;
      num2 = (secretNumber / 10) % 10;
      num3 = secretNumber % 10;
      

      这里不需要转换为使用字符串。另一方面,如果您不需要secretNumber 本身,那么您肯定只需要生成 1 到 9 之间的三个数字。 最简单的方法是使用类似的东西:

      List<Integer> digits = new ArrayList<Integer>();
      for (int i = 1; i <= 9; i++) {
          digits.add(i);
      }
      Collections.shuffle(digits, generator);
      

      ...然后使用列表中的前三个值:

      num1 = digits.get(0);
      num2 = digits.get(1);
      num3 = digits.get(2);
      

      【讨论】:

      • 我这样做的原因是因为该方法需要一个字符串类型的输出,所以我必须将该数字转换为字符串才能从该方法返回值。但是您对三位数有一个很好的看法,而且仅划分和使用 mod 要简单得多。也许我可以把每个数字分别变成一个字符串,然后将它们连接成一个。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-14
      • 1970-01-01
      • 1970-01-01
      • 2018-12-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多