陷入无尽的循环

时间:2018-04-28 13:49:00

标签: java

我将循环条件设置为< b。但是,当> b .I得到 input == 3 中的循环时,它仍然会继续 我还没有完成代码,所以如果你想运行它[input == 3],你必须初始化游戏大小,然后再进行游戏。 我尝试了while和for循环,我得到了相同的结果。



import java.util.Scanner;
import java.util.Random;

public class Assignment2 {
	static Scanner sc = new Scanner(System.in);

	public static void main(String[] args) {
		Random rand = new Random();
		int input;
		int[][] board = null;
		int rows = 0;
		int columns = 0;

		do {

			System.out.println("0. End Program" + "\n" + "1. Initialize Game Size" + "\n" + "2. Initialize Game" + "\n"
					+ "3. Print 1 stage Ahead" + "\n" + "4. Print k stage ahead");
			input = sc.nextInt();

			if (input < 0 || input > 4) {
				System.out.println("Wrong menu input");
			}
			if (input == 0) {

				System.out.println("End Program");
				break;
			}
			if (input == 1) { // Setting a game board size
				System.out.println("Enter number of rows");
				rows = sc.nextInt();
				System.out.println("Enter number of columns");
				columns = sc.nextInt();
				if (rows < 1 || columns < 1) {
					System.out.println("Wrong game size");

				} else {
					board = new int[rows][columns];

					System.out.println("set Game size");

				}
			}
			if (input == 2) { // initialize a random game with 1 and 0
				if (columns == 0 || rows == 0) {
					System.out.println("No game size stored");

				} else {
					for (int i = 0; i < rows; i++) {
						for (int j = 0; j < columns; j++) {
							if (rand.nextBoolean())
								board[i][j] = 1;
							else
								board[i][j] = 0;

						}
					}

					for (int k = 0; k < board.length; k++) {
						for (int j = 0; j < board[k].length; j++) { // loop to check every square in the board
							System.out.print(board[k][j] + " ");
						}
						System.out.println();
					}
				}
			}
			if (input == 3) { // 1 movement ahead in the game
				for (int k = 0; k < board.length; k++) {
					for (int j = 0; j < board[k].length; j++) { // loop to check every square in the board
						if (board[k][j] == 1) {
							int rowMove = 0; // checks for possible movement
							int columnMove = 0; // checks for possible movement
							int counter = 0;
							for (rowMove = 0; rowMove < rows || columnMove < columns; rowMove++) {
								for (columnMove = 0; rowMove < rows || columnMove < columns; columnMove++) {
									int difrow = rowMove - rows; // row distance
									int difcolumn = columnMove - columns; // column distance
									if (difrow < 0) { // absolute number
										difrow = -difrow;

									}
									if (difcolumn < 0) { // absolute number
										difcolumn = -difcolumn;
									}
									if (difcolumn + difrow == 3) {
										counter++;

									}

								}

							}
							if (counter == 1 || counter == 2) {
								board[k][j] = 1;
							} else {
								board[k][j] = 0;
							}
						}
					}

				}
				for (int k = 0; k < board.length; k++) {
					for (int j = 0; j < board[k].length; j++) { // loop to check every square in the board
						System.out.print(board[k][j] + " ");
					}
					System.out.println();
				}
			}
		} while (input != 0);
	}
}
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

在你的内部for循环中

for (columnMove = 0; rowMove < rows || columnMove < columns; columnMove++)

条件是只要rowMove < rows || columnMove < columns进行迭代,但只有columnMove进展,因此rowMove < rows始终为真

答案 1 :(得分:0)

你尝试过这个:

for (rowMove = 0; rowMove < rows; rowMove++) {
    for (columnMove = 0; columnMove < columns; columnMove++)