1

我想做的是,我有 2 款游戏,一款名为 ToF,一款名为 spin。如果输入的字符串是“spin”,则开始旋转游戏。并将控制台输入转换为 int。

如果输入的 int 为 1,则转动轮子。如果输入的 int 为 -1,则将控制台输入更改为 String,然后退出循环。

但我收到此错误:

src\Main.java:39: error: incompatible types
input = console.nextLine();

我的属性:

    private static Scanner console = new Scanner(System.in);
    private static Spin spin = new Spin();
    private static String input = "";
    private static String[] gamesArray = new String[] {"spin", "tof"};
    private static boolean spinWheel = false;
    private static boolean tof = false;

并且有错误:

        while (input.equals("spin")) {
            System.out.println("Spin game!");
            spinWheel = true;
            int input = console.nextInt();

            if (spinWheel) {
                System.out.println("Welcome to the spin game! Please write 1 to spin. and -1 to exit back");

                switch (input) {
                    case -1:
                        input = console.nextLine();
                    break;

                    case 1:
                    break;
                }                   
            }
        }

怎么了?为什么要这样做?我怎样才能解决这个问题?

4

1 回答 1

1

你需要

this.input = console.nextLine();

因为你隐藏了同名input的局部变量。int

一个更简洁的解决方案是给这两个变量之一一个不同的名称。

于 2013-06-24T21:55:42.700 回答