【问题标题】:Using Scanner with sequence of Integers使用带有整数序列的扫描仪
【发布时间】:2014-09-06 14:57:41
【问题描述】:

我正在编写一个程序来从控制台获取整数序列,例如

1 5 3 4 5 5 5 4 3 2 5 5 5 3

然后计算出现次数并打印以下输出:

0 - 0
1 - 1
2 - 1
3 - 3
4 - 2
5 - 7
6 - 0
7 - 0
8 - 0
9 - 0

其中第二个数字是第一个数字的出现次数。

代码:

public static void main (String args[])
{
    Scanner chopper = new Scanner(System.in);
    System.out.println("Enter a list of number: ");

    int[] numCount = new int[10];
    int number;

    while (chopper.hasNextInt()) {
        number = chopper.nextInt();
        numCount[number]++;
    }

    for (int i = 0; i < 10; i++) {
        System.out.println(i + " - " + numCount[i]);
    }
}

但是在输入序列后,我们必须输入一个非整数字符并按“Enter”来终止Scanner并执行“for”循环。有什么方法可以让我们不必输入非整数字符来终止扫描器?

【问题讨论】:

  • @user3681996 请指定您希望用户做什么来终止程序?例如,您不希望他输入“结束”吗?或者可能是一个标记值 99999?
  • @Jean-FrançoisSavard 这将成为答案的一部分,以解释为什么在使用 Scanner 时不能正常工作。
  • @Jean-François Savard 用户输入整数序列并回车结束程序

标签: java java.util.scanner


【解决方案1】:

你可以通过按 Enter 然后按 Control-D 退出。

如果您不想这样做,那么Scanner 没有其他方法。

您必须通过其他方式读取输入,例如使用BufferedReader

String line = new BufferedReader(new InputStreamReader(System.in)).readLine();
Scanner chopper = new Scanner(line);

更好(受@user3512478 的方法启发),有两个扫描仪,没有BufferedReader

Scanner chopper = new Scanner(new Scanner(System.in).nextLine());

【讨论】:

  • 谢谢,我明白了。把'扫描仪斩波器=新扫描仪(新扫描仪(System.in).nextLine());' after 'System.out.println("请输入数字列表:");'
【解决方案2】:

国际海事组织的最佳方式:

String str;
Scanner readIn = new Scanner(System.in);
str = readIn.nextLine();
String[] nums = str.split(" ");
int[] finalArray = new int[nums.length];
for(int i = 0; i < nums.length; i++) {
    finalArray[i] = Integer.parseInt(nums[i]);
return finalArray;

希望这会有所帮助!

【讨论】:

    【解决方案3】:

    最佳方式:不只是这种模式,对于任何类型的序列输入识别,修改Scanner对象的分隔符以获得所需的序列识别。

    在这里,在这种情况下,将斩波分隔符更改为空白字符(空格)。即"\\s"。您还可以使用"\\s*" 指定零次或多次出现的空白字符

    这使得 Scanner 检查空格而不是等待 Enter 击键。

    public static void main (String args[])
    {
        Scanner chopper = new Scanner(System.in).useDelimiter("\\s"); \\ Delimiter changed to whitespace.
        System.out.println("Enter a list of number: ");
    
        int[] numCount = new int[10];
        int number;
    
        while (chopper.hasNextInt()) {
            number = chopper.nextInt();
            numCount[number]++;
        }
    
        for (int i = 0; i < 10; i++) {
            System.out.println(i + " - " + numCount[i]);
        }
    }
    

    【讨论】:

      【解决方案4】:

      尝试使用 for 循环。

      for (int i:1; i<10;i++) {
           chopper.hasNextInt()
           number = chopper.nextInt();
           numCount[number]++;
      }
      

      从 Oracle Doc 中它说: 扫描操作可能会阻塞等待输入。
      hasNext 和 next 方法都可能阻塞等待进一步的输入

      【讨论】:

      • 为什么是负面的?我尝试了这个例子,它工作得很好。我添加了一些异常处理程序和日志记录行,但它保持不变。 int[] numCount = 新的 int[10];整数; for (int i=1; i
      • "我使用了我的示例,它运行良好" 不,它没有。此代码假定输入中必须有 9 个整数,因此(1)它将要求用户提供 9 个数字,(2)它将忽略从第 10 个开始的其余数字。而不是for (int i:1; i&lt;10;i++),您可能想要使用while (scanner.hasNextInt())
      猜你喜欢
      • 2014-05-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多