使用扫描仪读取int和char输入?

时间:2022-03-15 23:16:05

Ok hello guys. I'm trying to develop a code for the game Othello. I have come across a question that I need help with. I want to know how to take the input of an int and char in one single scanner. For example, if user enters D for column and 6 for the row, it woud look like this

好的你好。我正在尝试为奥赛罗游戏开发代码。我遇到了一个需要帮助的问题。我想知道如何在一个扫描仪中输入int和char。例如,如果用户为列输入D,为行输入6,则它看起来像这样

使用扫描仪读取int和char输入?

What I want is to be able to draw the board with the new spot when the user enters D6 instead of asking for column and row seperately, i want it to do the scanner input all in one go. I have looked all over the web but could not come to a conclusion. This is the code i need help with

我想要的是当用户输入D6而不是单独询问列和行时能够用新点绘制板,我希望它一次性完成扫描输入。我已经浏览了整个网络,但无法得出结论。这是我需要帮助的代码

public static void main (String args[]){
    char column;
    int row;
    Scanner scan = new Scanner(System.in);
    Othello game = new Othello();
    //game.startGame();
    game.displayBoard();
    do{
        do{
            System.out.print("Enter the column: ");     //get column from user
            column = scan.next().charAt(0);
        }while (game.checkColumn(column) == false);     //loop until input is valid 


        do{
            System.out.print("Enter the row   : ");     //get row from user
            row = scan.nextInt();
        }while (game.checkRow(row) == false);           //loop until input is valid 

        game.takeTurn(game, column, row);
    }while (1==1);

2 个解决方案

#1


2  

I would suggest you read a whole line, then parse what you need.

我建议你阅读整行,然后解析你需要的东西。

Note: This solution will only work for single character / digit combos. Use a regular expression if you want something more complex.

注意:此解决方案仅适用于单个字符/数字组合。如果您想要更复杂的东西,请使用正则表达式。

public static void main (String args[]){
    char column;
    int row;
    Scanner scan = new Scanner(System.in);
    Othello game = new Othello();
    //game.startGame();

    while (true) {

        game.displayBoard();

        do {
            System.out.print("Enter the column, then row, for example (A0): "); 
            String line = scan.nextLine();
            column = line.charAt(0);
            row = Integer.parseInt("" + line.charAt(1));
        } while (!( game.checkColumn(column) && game.checkRow(row) );     //loop until input is valid 

        game.takeTurn(column, row); // Remove game as a parameter here. It is not needed

        // if (gameOver) break; 
    }
}

#2


0  

It's worth noting that Scanner will read space-separated input as a sequence of values, so, something like this would work if the user types "D 6":

值得注意的是,Scanner会将空格分隔的输入读作一系列值,因此,如果用户输入“D 6”,这样的东西就会起作用:

System.out.println("Enter ROW COL: ");
char column = scan.next().charAt(0);
int row = scan.nextInt();
scan.nextLine(); // clear new line

If, on the other hand, you want to read "D6", then you'll need to read the input as a string, and extract components from it manually:

另一方面,如果您想要读取“D6”,那么您需要将输入作为字符串读取,并手动从中提取组件:

System.out.println("Enter ROWCOL: ");
String raw = scan.next();
char column = raw.charAt(0);
int row = Character.getNumericValue(raw.charAt(1)); //get 2nd digit as int
scan.nextLine(); // clear new line

#1


2  

I would suggest you read a whole line, then parse what you need.

我建议你阅读整行,然后解析你需要的东西。

Note: This solution will only work for single character / digit combos. Use a regular expression if you want something more complex.

注意:此解决方案仅适用于单个字符/数字组合。如果您想要更复杂的东西,请使用正则表达式。

public static void main (String args[]){
    char column;
    int row;
    Scanner scan = new Scanner(System.in);
    Othello game = new Othello();
    //game.startGame();

    while (true) {

        game.displayBoard();

        do {
            System.out.print("Enter the column, then row, for example (A0): "); 
            String line = scan.nextLine();
            column = line.charAt(0);
            row = Integer.parseInt("" + line.charAt(1));
        } while (!( game.checkColumn(column) && game.checkRow(row) );     //loop until input is valid 

        game.takeTurn(column, row); // Remove game as a parameter here. It is not needed

        // if (gameOver) break; 
    }
}

#2


0  

It's worth noting that Scanner will read space-separated input as a sequence of values, so, something like this would work if the user types "D 6":

值得注意的是,Scanner会将空格分隔的输入读作一系列值,因此,如果用户输入“D 6”,这样的东西就会起作用:

System.out.println("Enter ROW COL: ");
char column = scan.next().charAt(0);
int row = scan.nextInt();
scan.nextLine(); // clear new line

If, on the other hand, you want to read "D6", then you'll need to read the input as a string, and extract components from it manually:

另一方面,如果您想要读取“D6”,那么您需要将输入作为字符串读取,并手动从中提取组件:

System.out.println("Enter ROWCOL: ");
String raw = scan.next();
char column = raw.charAt(0);
int row = Character.getNumericValue(raw.charAt(1)); //get 2nd digit as int
scan.nextLine(); // clear new line