对于Java中的第二次循环停顿

时间:2017-04-27 12:01:15

标签: java loops for-loop

我正在尝试制作一些应该跟踪赢/输比率的代码。我有一个for循环,检查一个名字是否存在于一个单独的变量中,这是有效的,但是当我再次尝试这样做时,它会停止。

System.out.println("\nEnter Name 1");
scan.nextLine();
String name1 = scan.nextLine();
for (int i = 0; i < numPlayers; i++) {
    if (name1.equals(players[i].name)) {
        players[i].win++;
        break;
    }
}
System.out.println("\nEnter Name 2");
scan.nextLine();
String name2 = scan.nextLine();
for (int i = 0; i < numPlayers; i++) {
    if (name2.equals(players[i].name)) {
        players[i].loss++;
        break;
    }
}

在代码之后它应该返回到while循环,但它只是停止了。如果我注释掉名称2部分,代码可以工作,但我需要两个部分。

编辑1: 下面是整个代码

public class Counter {

    @SuppressWarnings("resource")
    public static void main(String[] args) {

        int intTemp;
        String stringTemp;

        int keepGoing = 1;
        int numPlayers = 0;
        Player[] players = new Player[999];

        Scanner scan = new Scanner(System.in);
        System.out.println("Melee Score Tracker");

        while (keepGoing == 1) {
            System.out.println("\nPrint Scores\t1\nNew Match\t2\nNew Player\t3\nExit\t\t4");
            intTemp = scan.nextInt();

            // Print Scores
            if (intTemp == 1) {

                intTemp = 0;
                System.out.print("\n");
                for (int i = 0; i < numPlayers; i++) {
                    players[i].print();
                }

            }

            // New Match
            if (intTemp == 2) {

                intTemp = 0;
                System.out.println("\nEnter Name 1");
                scan.nextLine();
                String name1 = scan.nextLine();
                for (int i = 0; i < numPlayers; i++) {
                    if (name1.equals(players[i].name)) {
                        players[i].win++;
                        break;
                    }
                }
                System.out.println("\nEnter Name 2");
                scan.nextLine();
                String name2 = scan.nextLine();
                for (int i = 0; i < numPlayers; i++) {
                    if (name2.equals(players[i].name)) {
                        players[i].loss++;
                        break;
                    }
                }
            }

            // New Player
            if (intTemp == 3) {

                intTemp = 0;
                System.out.println("\nWhat's the player's name?");
                scan.nextLine();
                stringTemp = scan.nextLine();
                players[numPlayers] = new Player();
                players[numPlayers].name = stringTemp;
                numPlayers++;
                System.out.println(numPlayers);

            }

            // Exit
            if (intTemp == 4) {

                System.exit(0);

            }
        }

    }
}

1 个答案:

答案 0 :(得分:0)

因此而停滞不前;

scan.nextLine();
scan.nextLine();
String name2 = scan.nextLine();

等待输入3次。您只想输入一次,所以用;替换它;

String name2 = scan.nextLine();

将来,通过在循环中打印出一条消息来检查代码是否实际进入for循环,可以很容易地调试这样的事情。

String name2 = scan.nextLine();
for (int i = 0; i < numPlayers; i++) {
    System.out.println("We have entered the loop.");
    if (name2.equals(players[i].name)) {
        players[i].loss++;
        break;
    }
}

如果您在控制台中看到输出,那么您知道问题不在于循环本身,而在于它之前。