控制台忽略输入

时间:2013-10-09 07:10:01

标签: java

我创建了一个程序,使用Scanner从用户那里获取一个数字,当它是1到100之间的整数时,将它保存到'a'。请参阅下面的Java文件:

public class Parity_Check {
  private static Scanner sc;

  public static void main(String[] args) throws InterruptedException {

    sc = new Scanner(System.in);
    int a, b;
    System.out.print("Enter a number     between 1 and 100: ");

    while(true) {
      b = 0;
      if(!sc.hasNextInt()) {
        System.out.print("That isn't an integer! Try again: "); 
        sc.next();
      }
      else{
        b = sc.nextInt();
        if(b < 1 || b > 100) {
          System.out.print("That integer isn't between 1 and 100! Try again: "); 
          sc.next();
        }
        else{
          a = b; 
          break;
        }
      }
    }
    System.out.print("The number is: "+a+".");
  }
}

我遇到的问题如下: 程序返回后“该整数不在1和100之间!再试一次:“它等待用户的两个输入(而不是它应该的输入) - 第一个被完全忽略! 这是我用来说明问题的控制台会话:

"Enter a number between 1 and 100: 2.5
That isn't an integer! Try again: 101
That integer isn't between 1 and 100! Try again: Apple.
42
The number is: 42.”

正如你所看到的,它甚至没有注意输入"Apple".我完全迷失了为什么它不能正常工作,如下所示:

"Enter a number between 1 and 100: 2.5
That isn't an integer! Try again: 101
That integer isn't between 1 and 100! Try again: Apple.
That isn't an integer! Try again: 42
The number is: 42.”

我对Java很陌生,所以一个很好解释的答案将是天赐之物;我更感兴趣的是它为什么不起作用而不是如何解决它,因为希望我能够学习。

顺便说一句,我正在使用最新版本的Eclipse 64位。

3 个答案:

答案 0 :(得分:0)

不需要if语句中的sc.next(),并使您的代码跳过用户的下一个输入。下面的代码段可以按照您的意图正常工作。

private static Scanner sc;

public static void main(String[] args) throws InterruptedException {

    sc = new Scanner(System.in);
    int a, b;
    System.out.println("Enter a number     between 1 and 100: ");

    while(true) {
        b = 0;
        if(!sc.hasNextInt()) {
            System.out.println("That isn't an integer! Try again: ");
            sc.next(); 
        }
        else {
            b = sc.nextInt();
        }
        if(b < 1 || b > 100) {
            System.out.println("That integer isn't between 1 and 100! Try again: "); 
        }
        else {
            a = b; 
            break;
        }
    }
    System.out.println("The number is: "+a+".");
    return;
}

答案 1 :(得分:0)

此处您已从流中删除了一个int,因此您必须删除其他内容。取出对sc.next()的调用:

    b = sc.nextInt();
    if(b < 1 || b > 100) {
      System.out.print("That integer isn't between 1 and 100! Try again: "); 
      // sc.next(); remove this
    }

请注意这与早期if语句的情况有何不同:如果用户键入的内容不是数字,则必须通过调用next()将其从流中删除,因为没有别的会删除它。这里对nextInt的调用已经从流中删除了输入。

答案 2 :(得分:0)

尝试使用以下主要功能

public static void main(String [] args)抛出InterruptedException {

sc = new Scanner(System.in);
int a, b;
System.out.print("Enter a number     between 1 and 100: ");

while(true) {
  b = 0;
  if(!sc.hasNextInt()) {
    System.out.print("That isn't an integer! Try again: "); 
    sc.next();
  }
  else{
    b = sc.nextInt();
    if(b < 1 || b > 100) {
      System.out.print("That integer isn't between 1 and 100! Try again: "); 
    }
    else{
      a = b; 
      break;
    }
  }
}
System.out.print("The number is: "+a+".");

}