Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - Nested While Loop

Greetings Stack Overflow users, I come to you this evening for assistance on a Java program that I have created. I'm relatively new to Java so please excuse my ignorance on the topic. I have made a Java program that's a "Rock" "Paper" "Scissors" game and there seems to be an error in one of the statements.

import java.util.Scanner;

public class TheAntlers {
public static void main(String[] args) {

    int playerHumanWins = 0;
    int playerComputerWins = 0;
    int numberOfTies = 0;
    int computerResult;

    Scanner input = new Scanner(System.in);

    while(true) {

        String startGame;
        String playerHuman;
        String playerComputer = " ";

        System.out.print("Do you want to play \"Rock\", \"Paper\", \"Scissors\"? (Y/N): ");
        startGame = input.nextLine();

        startGame = startGame.toUpperCase();

        if(startGame.equals("N")) {
            System.out.println("NO!");
            break;
        }
        else if(! startGame.equals("Y")) {
            startGame = startGame.toLowerCase();
            System.out.println("Sorry, " + startGame + " is not a valid entry...");               
        }
        while(startGame.equals("Y")) {
            System.out.print("Please choose \"Rock\", \"Paper\", or \"Scissors\": ");
            playerHuman = input.nextLine();

            computerResult = (int)(Math.random() * 3);

            playerHuman = playerHuman.toUpperCase();

            if(computerResult == 1) {
                playerComputer = "ROCK";
            }
            else if(computerResult == 2) {
                playerComputer = "PAPER";
            }
            else if (computerResult == 3) {
                playerComputer = "SCISSORS";
            }

            switch (playerHuman) {
                case "ROCK" :
                    if(playerComputer.equals(playerHuman)) {
                    System.out.println("Tie you both picked \"ROCK\"");
                    numberOfTies++;
                }
                    else if(playerComputer.equals("PAPER")) {
                        System.out.println("Computer wins!");
                        playerComputerWins++;
                    }
                    else {
                        System.out.println("You win, \"ROCK\" beats " + "\"" + playerComputer + "\"");
                        playerHumanWins++;
                        return;
                    }
                    break;
                case "PAPER" :
                    if(playerComputer.equals(playerHuman)) {
                    System.out.println("Tie you both picked \"PAPER\"");
                    numberOfTies++;
                }
                    else if(playerComputer.equals("ROCK")) {
                        System.out.println("You win, \"PAPER\" beats " + "\"" + playerComputer + "\"");
                        playerHumanWins++;
                        return;
                    }
                    else {
                        System.out.println("Sorry, the computer won!");
                        playerComputerWins++;
                    }
                    break;
                case "SCISSORS" :
                    if(playerComputer.equals(playerHuman)) {
                    System.out.println("Tie you both picked \"SCISSORS\"");
                    numberOfTies++;
                }
                    else if(playerComputer.equals("PAPER")) {
                        System.out.println("You win, \"SCISSORS\" beats " + "\"" + playerComputer + "\"");
                        playerHumanWins++;
                        return;
                    }
                    else {
                        System.out.println("Sorry, the computer won!");
                        playerComputerWins++;
                    }
                    break;
                default:
                    playerHuman = playerHuman.toLowerCase();
                    System.out.println("Sorry, " + playerHuman + " is not a valid entry..."); 
                    break;
            } 
        }
    }         
}
}

The problem that I'm facing is related to the winning calculations. When I run the program and I enter rock repeatedly until I win, the output will be You win, "ROCK" beats " " but with any other option I get You win, "ROCK" beats "PAPER"

My question is, why am I getting an empty callback when playing rock?

*Also if you'd be willing to point out any other suggestions to help out a novice that would be great. *

like image 298
Jeffery Keel Avatar asked Oct 31 '13 04:10

Jeffery Keel


People also ask

What is nested while loop in Java?

A nested while loop is a while statement inside another while statement. In a nested while loop, one iteration of the outer loop is first executed, after which the inner loop is executed. The execution of the inner loop continues till the condition described in the inner loop is satisfied.

Can we write nested while loop in Java?

Nested loop means a loop statement inside another loop statement. That is why nested loops are also called as “loop inside loop“. Syntax for Nested Do-While loop: do{ do{ // statement of inside loop }while(condition); // statement of outer loop }while(condition);

Can you nest a for loop in a while loop Java?

We can also create nested loops with while and do... while in a similar way. Note: It is possible to use one type of loop inside the body of another loop. For example, we can put a for loop inside the while loop.

Can you have a nested Do-While loop?

A do-while loop inside another do-while loop is called nested do-while loop.


1 Answers

Math.random() * 3 is a number at least 0 and less than 3.

After casting it to an int, it is 0, 1, or 2.

        if(computerResult == 0) {
            playerComputer = "ROCK";
        }
        else if(computerResult == 1) {
            playerComputer = "PAPER";
        }
        else if (computerResult == 2) {
            playerComputer = "SCISSORS";
        }

Suggestions:

Be concise. You could change

String startGame;
startGame = input.nextLine();
startGame = startGame.toUpperCase();

to

String startGame = input.nextLine().toUpperCase();

It's more readable when you don't have to scroll and scroll.

Also, know that equalsIgnoreCase() exists.

like image 76
Paul Draper Avatar answered Oct 12 '22 18:10

Paul Draper