Java 偶尔忽略输入
Java ignoring input occasionally
所以我是新手,一般来说我是编程新手,我一直在开发剪刀石头布游戏,用户输入字母并与计算机对战,但有时我的输入会被忽略,有时游戏会重新启动plays on 我花了无数个小时试图弄清楚为什么会这样。这是代码
添加else语句,打印input和computer的值。你会知道你没有处理的组合。
有点像..
}else if(input == 3 && computer == 3){
report(computer,"tie with the computer");
else {
System.out.println("input: " + input + "computer: " + computer);
}
方法 .nextInt(n)
生成一个从 0
到 n
并排除 n
的整数(在您的例子中是 0
到 2
)。基本上,当计算机生成 0
时,它总是 "ignores" 您的输入,因为没有 if
条件为真。要解决您的问题并生成从 1
到 n
的数字,请使用 .nextInt(n) + 1
.
所以我是新手,一般来说我是编程新手,我一直在开发剪刀石头布游戏,用户输入字母并与计算机对战,但有时我的输入会被忽略,有时游戏会重新启动plays on 我花了无数个小时试图弄清楚为什么会这样。这是代码
添加else语句,打印input和computer的值。你会知道你没有处理的组合。
有点像..
}else if(input == 3 && computer == 3){
report(computer,"tie with the computer");
else {
System.out.println("input: " + input + "computer: " + computer);
}
方法 .nextInt(n)
生成一个从 0
到 n
并排除 n
的整数(在您的例子中是 0
到 2
)。基本上,当计算机生成 0
时,它总是 "ignores" 您的输入,因为没有 if
条件为真。要解决您的问题并生成从 1
到 n
的数字,请使用 .nextInt(n) + 1
.