【发布时间】:2014-02-13 09:20:10
【问题描述】:
我们为此任务制定了一些指导方针: 1.使用扫描仪(扫描仪扫描仪=新扫描仪(系统.in);) 2.使用方法scanner.nextLine()
我们必须一步一步地构建一个游戏(Mastermind),我总是得到一个错误(BlueJ),因为我使用 nextInt() 和 switchcase(用于输入不是 int 的东西) 顺便说一句:我们不应该使用 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!");
}
}
}
【问题讨论】:
-
您的问题不清楚。请详细说明。
-
像什么?我使用 BlueJ 进行编码。我们有老师的任务,我不知道如何正确解决它。问题写在代码中。我如何使用scanner.nextLine();正确,如何在错误消息“非法用户输入!(...)”之后重新输入数字
标签: java switch-statement java.util.scanner