【问题标题】:Scanner + switch use扫描仪+开关使用
【发布时间】:2018-09-11 05:09:13
【问题描述】:

我是 Java 新手。

我目前正在做一个副项目;制作基于文本的游戏。我意识到使用 Switch 语句对于这种类型的游戏非常有用。

所以它基本上是这样工作的。

我问用户,你想做什么?

  1. 步行

那么,构建switch-statement 和Scanner 以及“再次询问用户的默认语句”的最佳方式是什么?

我一直在这样做(我的代码在这里),但它似乎有很多潜在的问题。

你们能给我一些关于如何用Scanner 制作最佳switch-statement 的提示吗?

非常感谢您。

public static void ask() {
 Scanner sc = new Scanner(System.in);
    System.out.println("What do you want to do?");
    while (!sc.hasNextInt()) {
        sc.next();
    }
        select = sc.nextInt();
        switch (select) {

        case 1:
            eat();
            break;
        case 2:
            walk();
            break;
        case 3:
            sleep();
            break;

        default:
            System.out.println("choose from 1 to 3");
           ask();  //would you re call itself again here? or is there any otherway to do without recalling itself?

        }

【问题讨论】:

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


    【解决方案1】:

    您的代码看起来不错,但我会重构它。还要添加while循环再次询问:

    public static void ask() {
    
     Scanner sc = new Scanner(System.in);
     boolean isWrongAnswer;
     do {
        isWrongAnswer = false;
        System.out.println("What do you want to do?");
        switch (sc.nextInt()) {
            case 1:
                eat();
                break;
            case 2:
                walk();
                break;
            case 3:
                sleep();
                break;
            default:
               System.out.println("choose from 1 to 3");
               isWrongAnswer = true;
            }
     } while (isWrongAnswer);
    }
    

    【讨论】:

    • 如果我有很多 switch 语句,最好在所有 switch 语句中使用 'static Sc​​anner sc' 并共享 'sc' 还是在每个 switch 语句中声明它更好单独?
    • 不要将扫描仪置于静态范围。创建一些对象,将扫描仪对象传递给它并让该对象处理您的输入。该对象可以是单个实例,以便从多个对象中可见。
    【解决方案2】:

    最好先检查扫描仪有没有东西,然后检查它是否是 int 值。为此,可以使用 sc.hasNext() 和 sc.hasNextInt(),如下所示,

     public static void ask() {
                Scanner sc = new Scanner(System.in);
                System.out.println("What do you want to do?");
                while (sc.hasNext()) {
                    int select = 0;
                    if (sc.hasNextInt()) {
                        select = sc.nextInt();
                    }
                    switch (select) {
    
                    case 1:
                        System.out.println("call eat()");
                        break;
                    case 2:
    
                        System.out.println("call walk()");
                        break;
                    case 3:
                        System.out.println("call sleep()");
                        break;
    
                    default:
                        System.out.println("choose from 1 to 3");
                        ask(); 
    
                    }
                }
    

    【讨论】:

    • 这取决于用户是否可以输入多个数字来一次做多个决定。我认为情况并非如此,hashNextInt() 在这里毫无意义。
    【解决方案3】:

    我建议你在这种情况下使用 String。提高代码的可读性,避免出错。(http://www.oracle.com/technetwork/java/codeconventions-150003.pdf)

    private static final String EAT = "1";
    private static final String WALK = "2";
    private static final String SLEEP = "3";
    
    public void ask() {
        Scanner sc = new Scanner(System.in);
        System.out.println("What do you want to do?");
        switch (sc.next()) {
            case EAT:
                eat();
                break;
            case WALK:
                walk();
                break;
            case SLEEP:
                sleep();
                break;
            default:
                System.out.println("choose from 1 to 3");
                ask();  // as described above via "do while", but there is nothing wrong with recursion. The garbage collector works.
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-04-15
      • 1970-01-01
      • 2013-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多