[英]Why is my program terminating after it takes 3 inputs?
该程序试图在命令行上制作类似于《战舰》的游戏。 如果我的程序接受3个“ HITS”输入,它将终止。 为什么是这样? 我怀疑if / else语句中的逻辑有问题。
之所以有3个列表,是因为我想让用户知道他们已经连续打了3个数字,并且在游戏中沉没了。 另外,我知道当随机方法在多个列表中生成相同数字时可能发生的潜在问题。
很抱歉,如果代码编写得不太好。 我还是一个初学者。
主要
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);
}
}
}
}
游戏
import java.util.ArrayList;
public class Game {
private double randomNumber;
public int numGenerator() {
randomNumber = Math.random() * 30 + 1;
return (int) randomNumber;
}
}
错误
我没有得到编译器错误。 这是一个逻辑错误。 这是一个例子:
[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(list.isEmpty() == false && list2.isEmpty() == false && list3.isEmpty() == false)
它检查直到列表中的任何一个都不为空。
更改为:
while(list.isEmpty() == false || list2.isEmpty() == false || list3.isEmpty() == false) {
这将检查直到所有列表都不为空。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.