为什么我的程序在需要3个输入后终止?

时间:2016-02-15 12:45:41

标签: java

此程序试图在命令行上与战舰进行类似的游戏。如果我的程序需要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.

1 个答案:

答案 0 :(得分:0)

问题在于你的状况:

while(list.isEmpty() == false && list2.isEmpty() == false && list3.isEmpty() == false)

检查直到任何一个列表都不为空。

将其更改为:

while(list.isEmpty() == false || list2.isEmpty() == false || list3.isEmpty() == false) {

这将检查直到所有列表都不为空。