我們有這個任務的一些準則: 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!");
}
}
}
你的問題還不清楚。請詳細說明。 – Maroun
喜歡什麼?我使用BlueJ進行編碼。我們有來自我們老師的任務,我不知道如何正確解決它。問題寫在代碼中。我如何使用scanner.nextLine();正確,我怎麼能重新輸入錯誤消息後的數字「非法userinput!(...)」 – JavaBeginner