Tony
Tony

Reputation: 39

Java rock-paper-scissors

I know this java assignment has been asked here time and time again, however instead of copying other methods (as there seems to be tons of ways in order to write this sort of code), I would rather like help on mines so I could more-easily understand the concepts. Also, the professor could probably easily check if I cheated, haha.

There are no errors with this code, however it doesn't work like it is supposed to. The outputs are just really... random.

I feel like I'm missing a whole section of the code, but I've read my textbook again and again and can't seem to discover the origin of error. :| Please help!

edit: I updated my post a lot with the fixed changes.

import java.util.Scanner;

public class RockPaperScissors
{
   public static void main(String[] args)
     {  
      String playerTwo = "";\\to be honest I'm not sure what this is even for

      Scanner keyboard = new Scanner(System.in);
      System.out.println("Lets play rock-paper-scissors! Please enter your move:");

      int Choice = (int) (Math.random()*2);
         if (Choice == 0)
            playerTwo = "ROCK";\\rock = 0
         else if (Choice == 1)
            playerTwo = "PAPER";\\paper = 1
         else if (Choice == 2)
            playerTwo = "SCISSORS";\\scissors = 2

  String playerOne = keyboard.nextLine();     
     playerOne = playerOne.toUpperCase();
        if (playerOne.equals(playerTwo)){
              System.out.println("It's a tie, so nobody wins!");
              }

        if (playerOne.equals("ROCK")){
              if (playerTwo.equals("PAPER")){
                 System.out.println("Your rock got covered by my paper. You lose!");
                 }
              if (playerTwo.equals("SCISSORS")){
                 System.out.println("Your rock smashes my scissors. You win!");
                 }
              }

        if (playerOne.equals("PAPER")){
              if (playerTwo.equals("ROCK")){
                 System.out.println("Your paper covers my rock. You win!");
                 }
              if (playerTwo.equals("SCISSORS")){
                 System.out.println("Your paper got cut by my scissors. You lose!");
                 }
              }

        else if (playerOne.equals("SCISSORS")){
              if (playerTwo.equals("ROCK")){
                 System.out.println("Your scissors got smashed by my rock. You lose!");
                 }
              if (playerTwo.equals("PAPER")){
                 System.out.println("Your scissors cut my paper. You win!");
                 }
              }

            else
                  System.out.println("Error. Please restart and enter either: \"rock\", \"paper\", or \"scissors\".");
   }
}

Upvotes: 1

Views: 2353

Answers (3)

NewIsAlwaysBetter
NewIsAlwaysBetter

Reputation: 106

if (playerOne.equals("ROCK")){}
              else if (playerTwo.equals("PAPER")){
                 System.out.println("Your rock got covered by my paper. You lose!");
                 }
              else if (playerTwo.equals("SCISSORS")){
                 System.out.println("Your scissors got smashed by my rock. You win!");
                 }

Your conditional blocks haven't been written correctly. If a certain condition is true, the following { } block will be executed. In your code, you're checking for what playerOne has input but you're not doing anything when the input matches "ROCK". The block is just blank.

The else if condition will be checked next, (assuming playerTwo.equals("ROCK")) the System.out.println("Your rock ...."); will be executed only based on the fact that playerOne's input doesn't equal "ROCK".

You may want to go over your notes to understand how and when an else if statement works.

Instead, what you want is to check that playerOne.equals("ROCK"), and then check that playerTwo.equals("PAPER") and do something.

This applies to each case where you're checking playerOne's input.

Upvotes: 0

jeanluc
jeanluc

Reputation: 1708

Before I figure out whats wrong with your code, here are some suggestions:

Math.round() is not necessary. If you are casting to type int then it will automatically round to the nearest integer.

You should declare an Enum with the three types of outcomes. Then you can refer to them by name instead of numbers or strings:

public enum Outcome { ROCK, PAPER, SCISSOR };

Now you can assign variables and also use switch statements:

Outcome player1 = Outcome.ROCK;
// switch statement example
switch (player1) {
    case ROCK:
        // do something
    case PAPER:
        // do something
    case SCISSOR:
        // do something

Also, comments are done with two FORWARD slashes, not BACK slashes

Now for the WTF moment in your code. What the heck is this:

if (playerOne.equals("ROCK")){}
              else if (playerTwo.equals("PAPER")){
                 System.out.println("Your rock got covered by my paper. You lose!");
                 }
              else if (playerTwo.equals("SCISSORS")){
                 System.out.println("Your scissors got smashed by my rock. You win!");
                 }

Basically, you just told the computer that if playerOne chose rock, do nothing. The syntax of the if statement is like this:

if (condition) {
     // do something 
}

If you want to have multiple if statements inside an if statement do this:

if (condition) {
    if (condition) { // do something }
    else if (condition) { // do something else if first did not work }
    else { // do something if nothing worked }
}

In your code you typed this:

if (condition) {} // this does nothing if condition is true
else if (condition) {...} // if the first if statement is true, this will not be reached
else if (condition) {...} // neither will this!

So to fix your problem, your code snippet needs to look like this:

if (playerOne.equals("ROCK")) {
              if (playerTwo.equals("PAPER")) {
                 System.out.println("Your rock got covered by my paper. You lose!");
              }
              else if (playerTwo.equals("SCISSORS")) {
                 System.out.println("Your scissors got smashed by my rock. You win!");
              }
} // terminates outer if

If you have any more questions, comment on this answer!

Upvotes: 1

Feek
Feek

Reputation: 650

I'm not going to write your program for you, instead I am going to point out flaws in your logic.

if (playerOne.equals("ROCK"))**{}** //notice you aren't actually executing any logic here?
     **else** if (playerTwo.equals("PAPER")){ //this line is being called if playerOne does **not** equal rock.
     System.out.println("Your rock got covered by my paper. You lose!");
}

This same logic applies to your other blocks of code as well.

Instead it should be something along the lines of

   if (playerOne.equals("ROCK")){
       if (playerTwo.equals("PAPER")){ 
         System.out.println("Your rock got covered by my paper. You lose!");
       }
    }        

Upvotes: 0

Related Questions