Any help with this would be appreciated. Basically I need to setup a function that will track both the

getUserChoice/GetCompChoice scores for each of the 10 Games, however lets say out of 8/10 games the user has won 6 so therefore the user cannot come back to win the most games or tie so the game should end before the 10 limit.

Any help on setting up this initial tracker for the display function would be appreciated.

Below is my code in its current state, minus the counter I'm using for all 10 games.

// Create object for reading from the keyboard.
    static Scanner input = new Scanner(System.in);
    // Class Contastant to simplify comparisons.
    static final String ROCK = "Rock";
    static final String PAPER = "Paper";
    static final String SCISSORS = "Scissors";
    

    public static void main(String[] args) {
        // Get choices
        String computerChoice = getComputerChoice();
        String humanChoice = getHumanChoice();

        // Display the choices that were made
        System.out.println("Computer Chose: " + computerChoice);
        System.out.println("Human Chose: " + humanChoice);

        // Computer the winner
        String winner = computeWinner(humanChoice, computerChoice);

        // Display the winner
        System.out.println("The Winner is: " + winner);
    }

    // Calculate the winner from the computer and human choices.
    private static String computeWinner(String humanChoice, String computerChoice) {
        String winner;

        // A chain of nested if/else statements.  Notice that the tie is only
        // checked once. (If the choices are the same, it doesn't matter what
        // was specifically chosen!)
        if (humanChoice.equalsIgnoreCase(computerChoice)) {
            winner = "Game is a Tie!";
        } else if (humanChoice.equalsIgnoreCase(ROCK)) {
            if (computerChoice.equals(SCISSORS)) {
                winner = "Human";
            } else {
                winner = "Computer";
            }
        } else if (humanChoice.equalsIgnoreCase(PAPER)) {
            if (computerChoice.equals(ROCK)) {
                winner = "Human";
            } else {
                winner = "Computer";
            }
        } else if (humanChoice.equalsIgnoreCase(SCISSORS)) {
            if (computerChoice.equals(PAPER)) {
                winner = "Human";
            } else {
                winner = "Computer";
            }
        } else {
            winner = "There are no winners with invalid input!";
        }

        return winner;
    }

    // Get the computer's choice
    private static String getComputerChoice() {
        // Get a random number between 0 and 2
        int randomValue = (int) (Math.random() * 3.0);

        String computerChoice;

        if (randomValue == 0) {
            computerChoice = ROCK;
        } else if (randomValue == 1) {
            computerChoice = PAPER;
        } else {
            computerChoice = SCISSORS;
        }

        return computerChoice;
    }

    // Get the human's choice
    private static String getHumanChoice() {
    System.out.print("Please enter Rock, Scissor, or Paper: ");
    String inString = input.nextLine();
    if (inString.equalsIgnoreCase("rock") || inString.equalsIgnoreCase("paper") ||
          inString.equalsIgnoreCase("scissor")){
        return inString;
    }
    else{
        System.out.print("Invalid entry, ");
        return getHumanChoice();
    }
}
	}

Edited 5 Years Ago by stephenk291: n/a

however lets say out of 8/10 games the user has won 6 so therefore the user cannot come back to win the most games or tie so the game should end before the 10 limit.

Can you rephrase this? I for one don't understand what you mean here.

Can you rephrase this? I for one don't understand what you mean here.

Ok lets say I add a condition for my display function, so..there's 10 games total...the computer wins 6 in a row..so if you look at the big picture the "winner" will be the computer because he will have won 6 out of the 10 games..so theres no point to play anymore. thats the type of condition I need to create for this display function for the final scores..to basically track the wins and losses..and if a ratio like that occurs the games will end and the final score will display.

static void displayGameWinner(int userScore,
                                  int compScore) {

        System.out.println("\n\nFinal Score:");
        System.out.println("       User=" + userScore + 
                           "   Computer=" + compScore);
        System.out.println();

        if (userScore > compScore) {
            System.out.println("The Winner is: The User!!");
        }
        else if (compScore > userScore) {
            System.out.println("The Winner is: The Computer!");
        }
        else {
            System.out.println("Its A Tie: Nobody Wins!");

Gotcha. Well I guess the test would be whether the "spread" is greater than the games remaining, then why bother to continue to play. You can't implement any of this until you actually start keeping track of wins, losses, and ties, which you don't do, so that would be the first thing to do.

In order to do THAT, you have to make a major re-design one way or the other. Right now everything in your program is static. That means no class variables. Generally that's not the way things are done in Java since Java is object oriented. To make it Object Oriented, you might create a class called Game, which handles one round of Paper Rock Scissor. You might have a Tournament class that keeps track of a whole bunch of Games. You might create a Score class to help your Tournament class.

That's if you go the Object Oriented route, which is the whole point of Java in the first place. If you are just trying to make a regular old Java program that does all of this, that's certainly possible. Instead of creating variables like humanScore, computerscore, and numTies as class variables, you'd make them variables inside of some static function. The point is that you have to have some variables keeping track of the past games SOMEWHERE and right now you don't.

This article has been dead for over six months. Start a new discussion instead.