【问题标题】:Questions about scanner (BlueJ)关于扫描仪 (BlueJ) 的问题
【发布时间】: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


【解决方案1】:

您需要做的是使用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();
}

【讨论】:

  • 你能说明/写下你的确切意思吗?我不是java pro :) 刚开始学习它。你能添加一些代码并展示你如何使用catch吗?顺便谢谢。
  • @JavaBeginner:我已经更新了我的答案。我希望这会有所帮助:)
  • @JavaBeginner:奇怪。现在应该可以看到了。
  • 感谢这个工作,但仍然有一个小错误:如果我输入 6 - 使用默认 system.out.println 但之后我无法再次输入数字
  • 有效!!!哦,伙计,你真棒!非常感谢。抱歉,我没有 15+ 代表投票支持答案 - 或者我可以“授予”你代表吗?你帮了我很多。我做了一点工作——也许我稍后还有另一个问题。我该怎么写你?
【解决方案2】:

您不能直接将nextLine() 的输出用于switch-case,因为nextLine() 返回的是String,而不是char。但是,如果您只阅读单个字符,则可以:

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

scanner.nextInt() 如果下一个标记不可解析为 int,显然会抛出异常。您应该将这些语句包装在 try-catch 块中,并在输入非数字时执行适当的操作。

【讨论】:

  • 我试过这样运行它。输入 1 并且:“非法用户输入!(...)”并中断 - 所以下一个问题是 - 在此错误消息之后 - 我如何输入另一个数字?
  • Java 7 开始,您可以打开一个字符串(子句需要是"1""2" 等)。如果您打开char,则案例应为'1''2' 等。
【解决方案3】:

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

看看Integer.parseInt()方法

【讨论】:

  • 完成了,但我仍然有问题 - 你能帮帮我吗?
  • 具体问题。
  • @JavaBeginner 我刚刚尝试过并没有发现任何特殊问题。向我们展示您的尝试和遇到的错误
  • 我自己试过了,但没有用 - 我删除了我的代码并尝试了下面的版本(npinti's) - 但非常感谢你的努力:)
猜你喜欢
  • 2014-11-22
  • 1970-01-01
  • 1970-01-01
  • 2012-10-05
  • 2014-06-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多