2014-02-24 13 views
-2

基本上我需要這個while循環執行4次nextInt()在while循環有問題

它會要求用戶輸入,然後使用該輸入在嵌套的if語句。 (重複4次)

第一個提示會出現,我可以輸入一個輸入。

但之後它只是給了我一個錯誤。

並保持將錯誤指向「int userInput = scan.nextInt();」

我不知道什麼是錯的。有人可以指導我嗎?感謝

int count = 0; 

    while (count < 4) { 

     System.out.println(prompt1); 
     int userInput = scan.nextInt(); 

     (some nested if statements here to do my task, they all have 
     count++ at the end) 

    } 
+2

** **什麼錯誤你好嗎?你在哪裏定義和初始化'scan'? –

+0

在while循環之外初始化它 – Oxtis

+1

我們可以看到該代碼嗎?同時發佈你正在獲取的整個錯誤/異常。 –

回答

2

務必檢查nextInt可以要求它之前:

if(scan.hasNextInt()) 
{ 
int userInput = scan.nextInt(); 
// do something 
} 
0

您需要定義掃描

Scanner scan = new Scanner(System.in);