如何在 java 中使用 Scanner 时使用空格键分隔新行和列

How to use space key to separate new rows and columns while using Scanner in java

提问人:Cheng Kai Hing Jacky 提问时间:11/12/2023 最后编辑:Cheng Kai Hing Jacky 更新时间:11/13/2023 访问量:58

问:

我正在制作这样的黑白棋游戏:

import java.util.Arrays;
import java.util.Scanner;
public class test{

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

    public static void main(String[] args) {

    System.out.print("Please enter the board size (4 or above and even number) : ");
    int num = sc.nextInt();
    int rows = num; // Number of rows 
    int cols = num; // Number of columns 
    if (num % 2 == 1 || num < 4) {
        System.out.println("Error - input number should be 4 or above and even number.");
    } else {
        int[][] reversigame = new int[rows][cols];
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                System.out.print(reversigame[i][j] + " ");
            }
            reversigame[num - num / 2][num - num / 2] = 1;
            reversigame[(num - num / 2) - 1][(num - num / 2) - 1] = 1;
            reversigame[(num - num / 2)][(num - num / 2) - 1] = 2;
            reversigame[(num - num / 2) - 1][(num - num / 2)] = 2;
            System.out.println();
        }

但我想在使用 Scanner 时使用空格键分隔新的行和列,如下所示:

System.out.print("Please enter the position of '1' (row col): ");
int bcp = sc.nextInt();//bcp=black chess place
int b = 1;//black chess = 1
String[] digits = String.valueOf(bcp).split(" ");

但是在我执行下一步时,它不能更改为行和列:

if (bcp > 5 || bcp < 0) {/*Control variables between 0 and 5*/
System.out.println("Error - input number should be 0 to 5!");

            }
            for (; reversigame[bcp  + 1][bcp] == 2 || reversigame[bcp][bcp + 1] == 2 || reversigame[bcp - 1][bcp] == 2 || reversigame[bcp][bcp + 1] == 2;) {/*Check the black chess is or is't near the white chess*/
                reversigame[bcp][bcp] = b;
                System.out.println(Arrays.deepToString(reversigame));
                if (reversigame[bcp + 1][bcp] != 2 || reversigame[bcp][bcp+ 1] != 2 || reversigame[bcp - 1][bcp] != 2 || reversigame[bcp][bcp + 1] != 2) {
                    System.out.println("Error - invalid move");
                    break;
                }
            }

那么,我应该如何在一个句子中添加行和列,同时使用空格键将它们分开呢?

Java 多维数组

评论


答:

2赞 Alexey R. 11/12/2023 #1

你可以用空格分隔数字,并在一个序列中读取两个整数:

Scanner scanner = new Scanner(System.in);
int a = scanner.nextInt();
int b = scanner.nextInt();
System.out.println("result = " + (a + b));

输出如下:

2 5
result = 7
0赞 Reilas 11/13/2023 #2

"...我想在使用 Scanner 时使用空格键分隔新行和列......”

只需调用 nextInt 两次。

int[] digits = { sc.nextInt(), sc.nextInt() };

如果需要 String 值,请使用 next 方法。

String[] digits = { sc.next(), sc.next() };

或者,使用 nextLine 方法,然后在空格字符上拆分

String[] digits = sc.nextLine().split(" +");
0赞 Oleg Cherednik 11/13/2023 #3

您只需要使用两次。scan.nextInt()

public class Reversi {

    private static final int WHITE = 1;
    private static final int BLACK = 2;

    private static final int ROW = 0;
    private static final int COL = 1;

    public static void main(String... args) {
        Scanner scan = new Scanner(System.in);
        int size = readBoardSize(scan);
        int[][] board = new int[size][size];
        int nextMove = WHITE;

        printBoard(board);

        while (true) {
            int[] point = readNextMove(scan, nextMove, size);
            int row = point[ROW];
            int col = point[COL];
            board[row][col] = nextMove;
            printBoard(board);
            nextMove = nextMove == WHITE ? BLACK : WHITE;
        }
    }

    private static void printBoard(int[][] board) {
        for (int row = 0; row < board.length; row++) {
            for (int col = 0; col < board[row].length; col++) {
                if (col > 0)
                    System.out.print(' ');
                System.out.print(board[row][col]);
            }

            System.out.println();
        }

    }

    private static int readBoardSize(Scanner scan) {
        final int min = 4;
        System.out.format("Please enter the board size (%d or above and even number): ", min);

        int num = scan.nextInt();

        if (num < min || num % 2 == 1) {
            System.err.format("Input number should be %d or above and even number.\n", min);
            throw new RuntimeException();
        }

        return num;
    }

    private static int[] readNextMove(Scanner scan, int nextMove, int size) {
        final int min = 0;

        System.out.format("Please enter the position of '%d' (row col): ", nextMove);
        int row = scan.nextInt();
        int col = scan.nextInt();

        if (row < min || row >= size || col < min || col >= size) {
            System.err.format("Input number should be between %d and %d.\n", min, size);
            throw new RuntimeException();
        }

        int[] point = new int[2];
        point[ROW] = row;
        point[COL] = col;

        return point;
    }
}