Quantcast
Viewing latest article 2
Browse Latest Browse All 3

Answer by Eric Stein for Rock, Paper, Scissors 3-part

Naming

Java classes start with a capital letter and do not use underscores, so rock_paper should be RockPaper. Java uses camelCase, not snake_case, for all variable names except constants (static finals), so user_choice should be userChoice.

Spacing

The convention in java is to include whitespace before and after operators such as +, =, != or ==, so count_win=0 should be countWin = 0. Curly braces should be on the same line as an if or else.

Variable Declarations

You declare a new Random() inside your loop, so you might wind up making 19 Random objects, when you only need one. Declare it outside the loop. Conversely, you declare user_choice and computer_choice outside the loop, but they're only used inside. They should have the smallest possible scope.

Branching

You're branching (using if conditional statements) a lot to look up the name of a numeric selection. If you've learned about arrays, you could use a String[] of choices to simplify that lookup. There are a ton more branches to determine who wins. You only need one comparison to determine if there's a tie, and then only three specific checks to see if the user wins. Otherwise, the computer wins.

Making all those changes might look something like this:

import java.util.Random;import java.util.Scanner;public final class RockPaperScissors {    private static final String[] CHOICES = { "rock", "paper", "scissors" };    public static void main(final String[] argv) {        final Random random = new Random();        final Scanner keyboard = new Scanner(System.in);        int computerWins = 0;        int userWins = 0;        System.out.println("\n Let us play Rock Paper Scissors \n");        while (computerWins <= 10 && userWins <= 10) {            /* Read user input and determine choice */                System.out.println("\n Enter your choice 0 for rock, 1 for paper, or 2 for scissors \n");            final int userChoice = keyboard.nextInt();            if (userChoice < 0 || userChoice > 2) {                System.out.println("Incorrect choice");                continue;            } else {                System.out.println("\n Your choice was \n" + CHOICES[userChoice]);            }            /* Randomly generate computer input */            final int computerChoice = random.nextInt(3) + 1;            System.out.println("\n The computer choice was \n" + CHOICES[computerChoice]);            if (userChoice == computerChoice) {                System.out.println("Tie");                continue;            }            if (userChoice == 0 && computerChoice == 1                    || userChoice == 1 && computerChoice == 2                    || userChoice == 2 && computerChoice == 0) {                System.out.println("\nThe user wins!");                userWins++;            } else {                System.out.println("\nThe computer wins!");                computerWins++;            }        }    }}

Review Note: I intentionally don't close the Scanner because I'm assuming that hasn't been covered yet.


Viewing latest article 2
Browse Latest Browse All 3

Trending Articles