2013-10-09 201 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+"."); 

}