Declaring String Variables within a while loop without it looping all throughout - java

后端 未结 2 467
庸人自扰
庸人自扰 2021-01-25 06:26

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 tas

2条回答
  •  既然无缘
    2021-01-25 06:48

    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");
        }
    

提交回复
热议问题