Java 为什么我的程序在接受3次输入后终止?
这个程序试图在命令行上制作一个类似于战舰的游戏。如果我的程序接受3个“命中”的输入,它将终止。为什么会这样?我怀疑if/else语句中的逻辑有问题 我之所以有3个列表,是因为我想让用户知道他们已经连续命中了3个数字,并且他们在游戏中击沉了一艘船。另外,我知道当random方法在多个列表中生成相同的数字时可能会出现的潜在问题 如果代码写得不太好,我表示歉意。我还是个初学者 MainJava 为什么我的程序在接受3次输入后终止?,java,Java,这个程序试图在命令行上制作一个类似于战舰的游戏。如果我的程序接受3个“命中”的输入,它将终止。为什么会这样?我怀疑if/else语句中的逻辑有问题 我之所以有3个列表,是因为我想让用户知道他们已经连续命中了3个数字,并且他们在游戏中击沉了一艘船。另外,我知道当random方法在多个列表中生成相同的数字时可能会出现的潜在问题 如果代码写得不太好,我表示歉意。我还是个初学者 Main import java.util.ArrayList; import java.util.InputMismatch
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;
public class Main {
public static int numOfHits = 0;
public static int numOfGuesses = 0;
public static int userInput;
public static int number;
public static void main(String[] args) {
Game a = new Game();
ArrayList<Integer> list = new ArrayList<Integer>();
ArrayList<Integer> hitlist = new ArrayList<Integer>();
ArrayList<Integer> list2 = new ArrayList<Integer>();
ArrayList<Integer> list3 = new ArrayList<Integer>();
int temp = a.numGenerator();
list.add(temp);
list.add(temp+ 1);
list.add(temp + 2);
int temp2 = a.numGenerator();
list2.add(temp2);
list2.add(temp2 + 1);
list2.add(temp2 + 2);
int temp3 = a.numGenerator();
list3.add(temp3);
list3.add(temp3 + 1);
list3.add(temp3 + 2);
System.out.println(list + " " + list2 + " " + list3);
Scanner input = new Scanner(System.in);
System.out.println("Welcome to BattleShips! In this version; the game is on a long 30 cell row and there are 3 different sub rows to kill. Input your guesses.");
while(!input.hasNextInt()) {
System.out.println("That is not an integer. Please try again.");
input.next();
}
while(list.isEmpty() == false && list2.isEmpty() == false && list3.isEmpty() == false) {
while(!input.hasNextInt()) {
System.out.println("That is not an integer or the number is too large to be stored as an integer. Please try again.");
input.next();
}
userInput = input.nextInt();
while(hitlist.contains(userInput)) {
System.out.println("You have already hit that one! Try again.");
userInput = input.nextInt();
}
while(true) {
if(list.contains(userInput)) {
numOfGuesses++;
numOfHits++;
int index = list.indexOf(userInput);
hitlist.add(userInput);
list.remove(index);
System.out.println("HIT!");
userInput = -100;
break;
}
if(list2.contains(userInput) && !list.contains(userInput)) {
numOfGuesses++;
numOfHits++;
int index = list2.indexOf(userInput);
hitlist.add(userInput);
list2.remove(index);
System.out.println("HIT!");
break;
}
if(list3.contains(userInput)) {
numOfGuesses++;
numOfHits++;
int index = list2.indexOf(userInput);
hitlist.add(userInput);
list3.remove(index);
System.out.println("HIT!");
break;
}
else {
numOfGuesses++;
System.out.println("MISS!");
}
}
if(numOfHits == 9) {
System.out.println("Congratulations! You have beaten the game.");
System.out.println("You took " + numOfGuesses + " guesses");
System.exit(0);
}
}
}
}
错误
我没有得到编译器错误。这是一个逻辑错误。以下是一个例子:
[15, 16, 17] [8, 9, 10] [28, 29, 30] // NUMBERS GENERATED FROM MATH.RANDOM
Welcome to BattleShips! In this version; the game is on a long 30 cell row and there are 3 different sub rows to kill. Input your guesses.
15
HIT!
15
You have already hit that one! Try again.
16
HIT!
17
HIT!
//AFTER THIS LINE IT TERMINATES. I WANT THE USER TO KEEP MAKING GUESSES UNTIL 9 NUMBERS ARE HIT.
问题在于您的while状态:
while(list.isEmpty() == false && list2.isEmpty() == false && list3.isEmpty() == false)
它会检查,直到列表中的任何一个不是空的
将其更改为:
while(list.isEmpty() == false || list2.isEmpty() == false || list3.isEmpty() == false) {
这会一直检查到所有列表都不是空的。你得到了哪一行和哪一个错误?@anaxin我将编辑这篇文章now@StephenPeterWisniewski这三张名单是什么?您打算在其中存储什么?int index=list2.indexOf(userInput);如果在主循环
中的list3 if部分考虑条件,而(list.isEmpty()==false&&list2.isEmpty()==false&&list3.isEmpty()==false)
如果列表中的一个为空,则整个条件将为false。循环将停止。这就是你试图运行它时发生的事情。你需要做的是改变你的条件非常感谢。这是一个如此简单的错误
while(list.isEmpty() == false || list2.isEmpty() == false || list3.isEmpty() == false) {