2014-02-13 69 views
0

我們有這個任務的一些準則: 1.使用掃描儀(掃描儀掃描=新的掃描儀(System.in);) 2.使用方法scanner.nextLine()有關掃描儀問題(BlueJ的)

我們必須一步一步地構建一個遊戲(Mastermind),並且我總是在switchcase中使用nextInt()時遇到錯誤(BlueJ)(用於輸入不是int的東西) btw:我們不應該使用nextInt - 我們應該使用nextLine - 但我怎麼能用switchcase做到這一點?

import java.util.Scanner; 
public class Game { 
/** 
* Methods 
*/ 
public void play() { 
    System.out.println("******************* Game **********************"); 
    System.out.println("* (1) CPU vs Human        *"); 
    System.out.println("* (2) CPU vs CPU        *"); 
    System.out.println("* (3) Human vs CPU        *"); 
    System.out.println("* (4) Highscore         *"); 
    System.out.println("* (5) End          *"); 
    System.out.println("-------------------------------------------------"); 
    System.out.println("Your choice:         "); 

    Scanner scanner = new Scanner(System.in); 

    // i used this so far but i get an error for entering a-z or other stuff than numbers 
    int userInput = scanner.nextInt(); 

    //i have to use this but it doesnt work with switchcase - any suggestions? 
    //String userInput = scanner.nextLine(); 

    scanner.close(); 
    switch(userInput) { 
     case 1: // not written yet 
     case 2: // not written yet 
     case 3: // not written yet 
     case 4: // not written yet 
     case 5: System.exit(0); 
     default: System.out.println("Illegal userinput! Only enter numbers between 1 and 5!"); 
    } 
    } 
} 
+0

你的問題還不清楚。請詳細說明。 – Maroun

+0

喜歡什麼?我使用BlueJ進行編碼。我們有來自我們老師的任務,我不知道如何正確解決它。問題寫在代碼中。我如何使用scanner.nextLine();正確,我怎麼能重新輸入錯誤消息後的數字「非法userinput!(...)」 – JavaBeginner

回答

0

你需要做的是解析字符串(這是由.nextLine()方法產生)到使用Integer.parseInt(String s)方法的整數。如果給定的字符串不是整數,則此方法將拋出一個NumberFormatException,您可以通過該方法捕獲並用它來了解用戶何時輸入了一個無效號碼,以便您可以再次詢問他/她。

事情是這樣:

public static void main(String[] args) 
{ 
    boolean validInput = false; 
    Scanner scanner = new Scanner(System.in); 
    while (!validInput) 
    { 
     System.out.println("******************* Game **********************"); 
     System.out.println("* (1) CPU vs Human        *"); 
     System.out.println("* (2) CPU vs CPU        *"); 
     System.out.println("* (3) Human vs CPU        *"); 
     System.out.println("* (4) Highscore         *"); 
     System.out.println("* (5) End          *"); 
     System.out.println("-------------------------------------------------"); 
     System.out.println("Your choice:         "); 

     try 
     { 

      // i used this so far but i get an error for entering a-z or other stuff than numbers 
      int userInput = Integer.parseInt(scanner.nextLine().trim()); 
      //If no error took place, then the input is valid. 
      validInput = true; 

      //i have to use this but it doesnt work with switchcase - any suggestions? 
      //String userInput = scanner.nextLine();     
      switch (userInput) 
      { 
       case 1: // not written yet 
       case 2: // not written yet 
       case 3: // not written yet 
       case 4: // not written yet 
       case 5: 
        System.exit(0); 
       default: 
        validInput = false; 
        System.out.println("Illegal userinput! Only enter numbers between 1 and 5!"); 
      } 
     } catch (Exception e) 
     { 
      System.out.println("Invalid Input, please try again"); 
     } 
    } 
    scanner.close(); 
} 
+0

你能顯示/寫你到底是什麼意思?我不是一個java專業版:)剛開始學習它。你可以添加一些代碼並顯示你如何使用catch?謝謝你的方式。 – JavaBeginner

+0

@JavaBeginner:我已經更新了我的答案。我希望這有助於:) – npinti

+0

請說點什麼 – JavaBeginner

0

不能直接使用的nextLine()輸出的開關情況,因爲nextLine()返回一個字符串,而不是一個char。但是,如果你只在單個字符閱讀,你可以這樣做:

String userInput = scanner.nextLine(); 
switch(userInput.charAt(0)) { 

scanner.nextInt()顯然會拋出一個異常,如果下一個標記是不是可解析爲int。您應該將這些語句包裝在try-catch塊中,並在輸入非數字時執行相應的操作。

+0

我試過這樣並運行它。輸入1和:「非法userinput!(...)」並中斷 - 所以下一個問題是 - 在此錯誤消息之後 - 我如何輸入另一個數字? – JavaBeginner

+0

從[Java 7](http://openjdk.java.net/projects/jdk7/features/)開始,您*可以*切換到一個字符串(子句需要是「1」,「2」 「等)。如果你打開一個'char'的話,應該是「1」,「2」等。 – tom

0

nextLine()給你一個String,你應該把它轉換爲一個整數(因爲它是你所期望的)並執行後續的邏輯。

看一看Integer.parseInt()方法

+0

做到了,但我仍然存在這個問題 - 你能幫我一下嗎? – JavaBeginner

+0

具體問題。 –

+0

@JavaBeginner我剛剛嘗試過,發現沒有特別的問題。向我們展示您嘗試過的以及您遇到的錯誤 –