【问题标题】:Java need another Enter to finishJava 需要另一个 Enter 来完成
【发布时间】:2021-05-22 15:31:55
【问题描述】:

我是新会员。目前我对属于我的小项目的这个 Player 类有问题。每当我输入一个字符串名称时,它都需要我再次输入才能完成:

 public void setName(StringBuffer str) {
        for (int i = 0; i < str.length(); i++) {
            Character c = str.charAt(i);
            if (i == 0)
                str.replace(i, i + 1, Character.toUpperCase(c) + "");
            if (c == ' ') {
                Character x = str.charAt(i + 1);
                str.replace(i + 1, i + 2, Character.toUpperCase(x) + "");
            }
            if ((Character.isLetter(c)) && (i < str.length() - 1)) {
                Character x = str.charAt(i + 1);
                if (x != ' ') {
                    str.replace(i + 1, i + 2, Character.toLowerCase(x) + "" );
                }
            }
        }
        name = str.toString();
    }

这里是主类:

import java.util.Scanner;
public class main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        final int NUM_PLAYER = 4;
        int choice;
        int playerChoice;
        String name;

        Matrix matrix = new Matrix();
        Player[] player = new Player[NUM_PLAYER];
        do {
            System.out.println("THE LAST PASSENGER\n");
            System.out.println("1. Create Matrix");
            System.out.println("2. Change color of the Matrix");
            System.out.println("3. Input information of players and play the game");
            System.out.println("4. Play the game");
            System.out.println("5. Display");
            System.out.println("6. Highest and Lowest score");
            System.out.println("7. Exit");
            System.out.print("Enter your choice: ");
            choice = sc.nextInt();
            switch (choice) {
                case 1: System.out.println(matrix);
                break;

                case 2: matrix.swap();
                break;

                case 3:
                for (int i = 0; i < NUM_PLAYER; i++) {
                    player[i] = new Player();
                    sc.nextLine();
                    System.out.print("Input the name of player " + (i + 1) + ": ");
                    name = sc.nextLine();
                    StringBuffer str = new StringBuffer(name);
                    player[i].setName(str);
                }
                break;

                case 4: for (int i = 0; i < NUM_PLAYER; i++) {
                    player[i] = new Player();
                    System.out.print("Input the color that player " + (i + 1));
                    System.out.print(" has chosen(BLUE = 0, YELLOW = 1, RED = 2): ");
                    playerChoice = sc.nextInt();
                    while ((playerChoice < 0) || (playerChoice > 2)) {
                        System.out.print("Input valid. Please choose again(BLUE = 0, YELLOW = 1, RED = 2): ");
                        playerChoice = sc.nextInt();
                    }
                    player[i].setColor(playerChoice);
                    for (int j = 0; j < 5; j++) {
                        System.out.print("Input position in the row " + (j + 1) + " wants to choose: ");
                        playerChoice = sc.nextInt() - 1;
                        while ((playerChoice < 0) || (playerChoice > 2)) {
                            System.out.print("Invalid input. Please input from 1 to 3: ");
                            playerChoice = sc.nextInt() - 1;
                        }
                        int check = matrix.getColorMatrix(j, playerChoice);
                        if (player[i].getColor() == check) {
                            System.out.println("Correct!!!");
                            player[i].scoreIncrease();
                        }
                        else {
                            System.out.println("Wrong choose.");
                            break;
                        } System.out.println("Your score is: " + player[i].getScore());
                    }
                }
                break;

                case 5:
                    System.out.printf("%-20s%-10s%-10s\n", "Name", "Color", "Score");
                    for (Player i : player) {
                        System.out.printf("%-20s%-12s%-10d\n", i.getName(), i, i.getScore());
                    }
                    break;
                case 6:
                    int highest = 0;
                    int lowest = 0;
                    for (int i = 0; i < NUM_PLAYER; i ++){
                        int score = player[i].getScore();
                        if (score > highest) {
                            highest = score;
                        }
                        if (score < lowest) {
                            lowest = score;
                        }
                    }

                    System.out.print("Player have the highest score is: ");
                    for (int i = 0; i < NUM_PLAYER; i++)
                        if (highest == player[i].getScore())
                            System.out.println(player[i].getName());

                    System.out.print("Player have the lowest score is: ");
                    for (int i = 0; i < NUM_PLAYER; i++)
                        if (lowest == player[i].getScore())
                            System.out.println(player[i].getName());
                    break;
                default:
                    System.out.println("There is no choice " + choice);
                    System.out.println("Please Enter from 1 to 7.");
                    break;
            }
            System.out.println();
        } while (choice != 7);
    }
}

我该如何解决这个问题? 更新:这是Scanner 问题,我应该用BufferedReader 替换它,但我不熟悉它,我该怎么办?

【问题讨论】:

  • 请发布一个可重复的最小示例stackoverflow.com/help/minimal-reproducible-example,以便我们更好地帮助您。
  • 作为这里的新用户,您可能不知道:在您发布问题后的最初几个小时内密切关注您的问题以查看任何问题,这是一个非常的好主意要求澄清、深入、其他信息等。及时回复此类请求是其他用户愿意并能够帮助您的最佳机会。在最初的一个小时左右之后,对这个问题的兴趣可能会很快消退。

标签: java class


【解决方案1】:

你的问题在这里:

                player[i] = new Player();
                sc.nextLine(); // <-- Why??!
                System.out.print("Input the name of player " + (i + 1) + ": ");
                name = sc.nextLine();
                StringBuffer str = new StringBuffer(name);
                player[i].setName(str);

在我引用的第二个代码行中对sc.nextLine() 的调用会导致您的程序等待用户按 Enter 键。

在循环中,第一次通过nextLine() 只是消耗用户在输入选择后按下的 Enter (3)。每次后续循环时,用户都需要在此处再次按 Enter。所以你可能会说,如果你只有一个玩家,那不会有任何问题。

一个快速的解决方法是将该行向上移动到输入选择的行之后:

        choice = sc.nextInt();
        sc.nextLine(); // Consume the Enter that the user pressed after the choice

链接: 一个相关问题将为您提供有关Scanner 工作原理的更多信息:Scanner is skipping nextLine() after using next() or nextFoo()?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-14
    • 1970-01-01
    • 1970-01-01
    • 2015-05-02
    • 2016-07-21
    • 2016-10-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多