java—while循环中的最后一行在第二个循环的每次迭代后打印如何在收到给定命令后打印一次?

laximzn5  于 2021-07-07  发布在  Java
关注(0)|答案(1)|浏览(380)
package toto.com;

import java.util.Scanner;

public class redactor {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        String tournament = scanner.nextLine();

        double won = 0;
        double lost = 0;
        double receptacle = 0;

        //Input
          //Ballers
          //3
          //87
          //63
          //56
          //65
          //75
          //64
          //Sharks
          //4
          //64
          //76
          //65
          //86
          //68
          //99
          //45
          //78
          //End of tournaments
          // the output is at the bottom

        while (!tournament.equals("End of tournaments")) {
            int matches = Integer.parseInt(scanner.nextLine());
            receptacle += matches;

            for (int i = 0; i < matches; i++) {//TODO redact of needed to 0 and <
                int points1 = Integer.parseInt(scanner.nextLine());
                int points2 = Integer.parseInt(scanner.nextLine());

                if (points1 > points2) {
                    won++;
                    System.out.printf("%nGame %d of tournament %s: win with %d points.", i, tournament, points1 - points2);
                } else {
                    lost++;
                    System.out.printf("%nGame %d of tournament %s: lost with %d points.", i, tournament, points2 - points1);
                }

            }

            System.out.printf("%n%.2f%% matches win%n" +
                    "%.2f%% matches lost", (won / receptacle) * 100, (lost / receptacle) * 100); // As far as I'm concerned printing it outside of the loop using if statement doesnt't seem to work because the condition within is always true. 
            tournament = scanner.nextLine();

        }
    }

}

   //Desired Output
    //Game 1 of tournament Ballers: win with 24 points.
    //Game 2 of tournament Ballers: lost with 9 points.
    //Game 3 of tournament Ballers: win with 11 points.
    //Game 1 of tournament Sharks: lost with 12 points.
    //Game 2 of tournament Sharks: lost with 21 points.
    //Game 3 of tournament Sharks: lost with 31 points.
    //Game 4 of tournament Sharks: lost with 33 points.
    //28.57% matches win
    //71.43% matches lost
vojdkbi0

vojdkbi01#

我把你的问题理解为问如何在比赛结束后才打印输赢数字。要完成此操作,请在while循环之后移动最后一个printf语句。那么它只会在你的比赛结束后运行一次。

while (!tournament.equals("End of tournaments")) {
    int matches = Integer.parseInt(scanner.nextLine());
    receptacle += matches;

    for (int i = 0; i < matches; i++) {//TODO redact of needed to 0 and <
        int points1 = Integer.parseInt(scanner.nextLine());
        int points2 = Integer.parseInt(scanner.nextLine());

        if (points1 > points2) {
            won++;
            System.out.printf("%nGame %d of tournament %s: win with %d points.", i, tournament, points1 - points2);
        } else {
            lost++;
            System.out.printf("%nGame %d of tournament %s: lost with %d points.", i, tournament, points2 - points1);
        }

    }

    // Location of line to move from
    tournament = scanner.nextLine();

}
// Location of print statment to move to
System.out.printf("%n%.2f%% matches win%n" +
        "%.2f%% matches lost", (won / receptacle) * 100, (lost / receptacle) * 100); // As far as I'm concerned printing it outside of the loop using if statement doesnt't seem to work because the condition within is always true.

输出

Game 0 of tournament Ballers: win with 24 points.
Game 1 of tournament Ballers: lost with 9 points.
Game 2 of tournament Ballers: win with 11 points.
Game 0 of tournament Sharks: lost with 12 points.
Game 1 of tournament Sharks: lost with 21 points.
Game 2 of tournament Sharks: lost with 41 points.
Game 3 of tournament Sharks: lost with 33 points.
28.57% matches win
71.43% matches lost

相关问题