簡體   English   中英

while循環中的最后一行在第二個循環的每次迭代后打印。如何在收到給定命令后打印一次?

[英]The last line within the while loop prints after each iteration of the second loop.How to print it once after receiving the given command?

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

我正在閱讀您的問題,詢問如何在錦標賽結束后才打印贏/輸數字。 要完成此操作,請在 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.
 

output

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM