【发布时间】: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