问题
I am stuck at a part where I am supposed to declare a string variable called "phrase", where it shouldn't loop, all the way through.
to give you an idea my task is: Similar to Option 1 except the user enters 'N' (instead of 'Q') when they are done entering results for the first team. Then, the program inputs a second team name and its results until 'Q' is entered. Outputs two statements, like the statements in option 1 followed by a third statement that says which team is in first place (based on the number of points)
Sample input:
2
Toronto
W
W
L
O
W
O
W
N
Montreal // how would I make this appear in the same while loop?
L
L
O
L
L
W
L
L
Q
Sample output:
Toronto has played 7 games and has earned 10 points
Montreal has played 8 games and has earned 3 points
Toronto is in first place by 7 points
UPDATE:
My code:
else if (option == 2){
int counter = 0;
int totalpoints = 0;
String phrase = keyboard.next();
while(go){
String letter = keyboard.next();
if (letter.equals("W")){
pointsW++;
}
else if (letter.equals("L")){
pointsL++;
}
else if (letter.equals("O")){
pointsO++;
}
counter++;
if (letter.equals("N")){
totalpoints = pointsW + pointsL + pointsO;
counter--;
go = false;
}
}
int counter2 = 0;
int totalpoints2 = 0;
pointsW = 2;
pointsL = 0;
pointsO = 1;
String phrase2 = keyboard.next();
while (go2){
String letter2 = keyboard.next();
if (letter2.equals("W")){
pointsW++;
}
else if (letter2.equals("L")){
pointsL++;
}
else if (letter2.equals("O")){
pointsO++;
}
counter2++;
if (letter2.equals("Q")){
counter2--;
totalpoints2 = pointsW + pointsL + pointsO;
go2 = false;
}
}
System.out.println(phrase + " has played "+counter+" games and has earned "+totalpoints+" points");
System.out.println(phrase2 + " has played "+counter2+" games and has earned "+totalpoints2+" points");
if (totalpoints > totalpoints2){
System.out.println(phrase + " is in first place by "+(totalpoints - totalpoints2) + " points");
}else{
System.out.println(phrase2 + " is in first place by "+(totalpoints2 - totalpoints) + " points");
}
}
Sample input:
2
Toronto
W
W
L
O
W
O
W
N
Montreal
L
L
O
L
L
W
L
L
Q
The issue: This is the output I am getting "Montreal played 8 games and has earned 11 points" where instead it should be "Montreal has played 8 games and has earned 3 points"
The output I am getting
回答1:
- You can reuse the same variables for individual points i.e.
pointsW
andpointsO
because you do not want to retain their values till the end where you are publishing the results. The same is the case with the variable for the loop condition i.e.go
and the variable used for inputting win/loss i.e.letter
. You will need arrays or different variables for storing total points, countings, and team name.
import java.util.Scanner; public class Standings { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); int option = keyboard.nextInt(); int pointsW = 0; int pointsO = 0; String letter; boolean go = true; if (option == 2) { // Variables for total points, counting, and name for the first team int playedGamesTeamOne = 0; int teamOnePoints = 0; String teamOneName = keyboard.next(); while (go) { letter = keyboard.next(); if (letter.equals("W")) { pointsW += 2; } else if (letter.equals("O")) { pointsO++; } playedGamesTeamOne++; if (letter.equals("N")) { teamOnePoints = pointsW + pointsO; playedGamesTeamOne--; go = false; } } // Reset common variables go = true; pointsW = 0; pointsO = 0; // Variables for total points, counting, and name for the second team int playedGamesTeamTwo = 0; int teamTwoPoints = 0; String teamTwoName = keyboard.next(); while (go) { letter = keyboard.next(); if (letter.equals("W")) { pointsW += 2; } else if (letter.equals("O")) { pointsO++; } playedGamesTeamTwo++; if (letter.equals("Q")) { teamTwoPoints = pointsW + pointsO; playedGamesTeamTwo--; go = false; } } System.out.println(teamOneName + " has played " + playedGamesTeamOne + " games and has earned " + teamOnePoints + " points"); System.out.println(teamTwoName + " has played " + playedGamesTeamTwo + " games and has earned " + teamTwoPoints + " points"); if (teamOnePoints > teamTwoPoints) { System.out .println(teamOneName + " is in first place by " + (teamOnePoints - teamTwoPoints) + " points"); } else { System.out .println(teamTwoName + " is in first place by " + (teamTwoPoints - teamOnePoints) + " points"); } } } }
A sample run:
2 Toronto W W L O W O W N Montreal L L O L L W L L Q Toronto has played 7 games and has earned 10 points Montreal has played 8 games and has earned 3 points Toronto is in first place by 7 points
回答2:
you can use this code for option two
Scanner keyboard = new Scanner(System.in);
int teamCounter = 1;
//String[] teamsNames = new String[2];
String teamOneName="";
String teamTwoName="";
//int[] playedGames = new int[2];
int playedGamesTeamOne = 0;
int playedGamesTeamTwo = 0;
//int[] points = new int[2];
int teamOnePoints = 0;
int teamTwoPoints = 0;
boolean firstTimeTeam1 = true;
boolean firstTimeTeam2 = true;
while (teamCounter <= 2) {
if (teamCounter == 1) {
if (firstTimeTeam1) {
teamOneName = keyboard.nextLine();
firstTimeTeam1 = false;
}
String letter = keyboard.next();
if (letter.equals("W")) {
teamOnePoints += 2;
playedGamesTeamOne++;
} else if (letter.equals("L")) {
playedGamesTeamOne++;
} else if (letter.equals("O")) {
teamOnePoints += 1;
playedGamesTeamOne++;
} else if (letter.equals("N")) {
teamCounter++;
}
} else {
if (firstTimeTeam2) {
teamTwoName = keyboard.next();
firstTimeTeam2 = false;
}
String letter = keyboard.next();
if (letter.equals("W")) {
teamTwoPoints += 2;
playedGamesTeamTwo++;
} else if (letter.equals("L")) {
playedGamesTeamTwo++;
} else if (letter.equals("O")) {
teamTwoPoints += 1;
playedGamesTeamTwo++;
} else if (letter.equals("Q")) {
teamCounter++;
}
}
}
System.out.println(teamOneName + " has played " + playedGamesTeamOne + " games and has earned " + teamOnePoints + " points");
System.out.println(teamTwoName + " has played " + playedGamesTeamTwo + " games and has earned " + teamTwoPoints + " points");
if (teamOnePoints > teamTwoPoints) {
System.out.println(teamOneName + " is in first place by " + (teamOnePoints-teamTwoPoints) + " points");
} else {
System.out.println(teamTwoName + " is in first place by " + (teamTwoPoints-teamOnePoints) + " points");
}
来源:https://stackoverflow.com/questions/62354397/declaring-string-variables-within-a-while-loop-without-it-looping-all-throughout