我想做的是,我有 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;
}
}
}
出了什么问题?它为什么要这样做?我该如何解决这个问题?
最佳答案
你需要
this.input = console.nextLine();
因为您使用同名的本地 int
变量隐藏了 input
。
更简洁的解决方案是为两个变量之一指定不同的名称。
关于java - 错误: incompatible types Simple game selection with scanner?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17285776/