有关扫描仪的问题(BlueJ)

时间:2014-02-13 09:20:10

标签: java switch-statement java.util.scanner

我们为此任务提供了一些指导原则: 1.使用扫描仪(扫描仪扫描仪=新扫描仪(System.in);) 2.使用方法scanner .nextLine()

我们必须逐步建立一个游戏(Mastermind),我总是得到一个错误(BlueJ),用于使用带有switchcase的nextInt()(用于输入非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!");
    }
  }
}

3 个答案:

答案 0 :(得分:0)

您需要做的是使用Integer.parseInt(String s)方法将字符串(由.nextLine()方法产生)解析为整数。如果给定的字符串不是整数,则此方法将抛出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();
}

答案 1 :(得分:0)

您不能直接使用nextLine()的输出作为switch-case,因为nextLine()返回String而不是char。但是,如果您只阅读一个字符,则可以执行以下操作:

String userInput = scanner.nextLine();
switch(userInput.charAt(0)) {
如果下一个标记不能解析为int,那么

scanner.nextInt()显然会抛出异常。您应该将这些语句包装在try-catch块中,并在输入非数字时执行适当的操作。

答案 2 :(得分:0)

nextLine()给你一个String,你应该把它转换成一个整数(就像你期望的那样)并执行后续的逻辑。

查看Integer.parseInt()方法