【问题标题】: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?while 循环中的最后一行在第二个循环的每次迭代后打印。如何在收到给定命令后打印一次?
【发布时间】:2021-03-13 18:42:34
【问题描述】:
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

【问题讨论】:

    标签: java loops for-loop while-loop nested


    【解决方案1】:

    我正在阅读您的问题,询问如何仅在锦标赛结束后打印赢/输数字。要完成此操作,请在您的 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
    

    【讨论】:

    • 它在for循环中打印统计信息,然后程序继续结束。
    • 我已经更新了我的答案,以更明确地区分比赛统计数据和锦标赛赢/输统计数据。您能否确认我的答案中的输出符合您的期望。
    • 无效。正如我上面提到的,它不会打印最终统计数据(锦标赛赢/输)。还有其他建议吗?
    • 这很奇怪,因为它会在我这边打印赢/输数据。您能否提供包含上述建议的代码更新版本?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-22
    • 1970-01-01
    • 2015-10-20
    • 1970-01-01
    • 2016-03-18
    • 2016-04-23
    • 2017-10-07
    相关资源
    最近更新 更多