2013-10-09 156 views
0

我用扫描仪把从用户编号和保存创建的程序为“一”时,它是从1到100的整数请参阅下的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位。

回答

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的调用已经从流中移除输入。

+0

啊,所以我把b中的整数从流中移除了呢?完美的答案,非常感谢! – DanielDC88

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; 
} 
+0

我想你可能已经删除了错误的“SC。下一个();”但我解决了它。 = P非常感谢! – DanielDC88

+0

我到底在那里哈哈 –

0

尝试用下面主要将工作

公共静态无效的主要(字串[] 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+"."); 

}