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

laximzn5  于 2021-07-07  发布在  Java
关注(0)|答案(1)|浏览(421)
  1. package toto.com;
  2. import java.util.Scanner;
  3. public class redactor {
  4. public static void main(String[] args) {
  5. Scanner scanner = new Scanner(System.in);
  6. String tournament = scanner.nextLine();
  7. double won = 0;
  8. double lost = 0;
  9. double receptacle = 0;
  10. //Input
  11. //Ballers
  12. //3
  13. //87
  14. //63
  15. //56
  16. //65
  17. //75
  18. //64
  19. //Sharks
  20. //4
  21. //64
  22. //76
  23. //65
  24. //86
  25. //68
  26. //99
  27. //45
  28. //78
  29. //End of tournaments
  30. // the output is at the bottom
  31. while (!tournament.equals("End of tournaments")) {
  32. int matches = Integer.parseInt(scanner.nextLine());
  33. receptacle += matches;
  34. for (int i = 0; i < matches; i++) {//TODO redact of needed to 0 and <
  35. int points1 = Integer.parseInt(scanner.nextLine());
  36. int points2 = Integer.parseInt(scanner.nextLine());
  37. if (points1 > points2) {
  38. won++;
  39. System.out.printf("%nGame %d of tournament %s: win with %d points.", i, tournament, points1 - points2);
  40. } else {
  41. lost++;
  42. System.out.printf("%nGame %d of tournament %s: lost with %d points.", i, tournament, points2 - points1);
  43. }
  44. }
  45. System.out.printf("%n%.2f%% matches win%n" +
  46. "%.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.
  47. tournament = scanner.nextLine();
  48. }
  49. }
  50. }
  51. //Desired Output
  52. //Game 1 of tournament Ballers: win with 24 points.
  53. //Game 2 of tournament Ballers: lost with 9 points.
  54. //Game 3 of tournament Ballers: win with 11 points.
  55. //Game 1 of tournament Sharks: lost with 12 points.
  56. //Game 2 of tournament Sharks: lost with 21 points.
  57. //Game 3 of tournament Sharks: lost with 31 points.
  58. //Game 4 of tournament Sharks: lost with 33 points.
  59. //28.57% matches win
  60. //71.43% matches lost
vojdkbi0

vojdkbi01#

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

  1. while (!tournament.equals("End of tournaments")) {
  2. int matches = Integer.parseInt(scanner.nextLine());
  3. receptacle += matches;
  4. for (int i = 0; i < matches; i++) {//TODO redact of needed to 0 and <
  5. int points1 = Integer.parseInt(scanner.nextLine());
  6. int points2 = Integer.parseInt(scanner.nextLine());
  7. if (points1 > points2) {
  8. won++;
  9. System.out.printf("%nGame %d of tournament %s: win with %d points.", i, tournament, points1 - points2);
  10. } else {
  11. lost++;
  12. System.out.printf("%nGame %d of tournament %s: lost with %d points.", i, tournament, points2 - points1);
  13. }
  14. }
  15. // Location of line to move from
  16. tournament = scanner.nextLine();
  17. }
  18. // Location of print statment to move to
  19. System.out.printf("%n%.2f%% matches win%n" +
  20. "%.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.

输出

  1. Game 0 of tournament Ballers: win with 24 points.
  2. Game 1 of tournament Ballers: lost with 9 points.
  3. Game 2 of tournament Ballers: win with 11 points.
  4. Game 0 of tournament Sharks: lost with 12 points.
  5. Game 1 of tournament Sharks: lost with 21 points.
  6. Game 2 of tournament Sharks: lost with 41 points.
  7. Game 3 of tournament Sharks: lost with 33 points.
  8. 28.57% matches win
  9. 71.43% matches lost
展开查看全部

相关问题