0

to start off I am still new to programming. My assignment is to make a rock paper scissors program with no arrays. Depending on our previous choices, the program will choose a corresponding weapon. At the end of the program, if the user does not want to continue, we display their overall scores. All of this is supposed to be in another method that we call inside of our main.

As I stated earlier, I am very new to programming, so outside of arrays, if else statements are the only way I know how to do this (and lots of em).

import java.util.Scanner;

public class ASSIGNMENT
{
  public static void main (String [] args)
  { 
     game();

  }//end main

  public static void game ()
  {
     Scanner kb = new Scanner(System.in);

     //assigning variables
     int rock = 1; 
     int scissors = 2; 
     int paper = 3; 
     int weaponAI = 0;
     int weaponP = 0;
     int wins = 0;
     int losses = 0;
     int ties = 0;
     int rockNum = 0;
     int scissorsNum = 0;
     int paperNum = 0;
     String yorn;

     do
     {
        //generate random numbe to decide weapon
        double weaponNum = (Math.random() * 3)+ 1;
        System.out.println(weaponNum);
        if (weaponNum >= 1 && weaponNum < 2)
        {  
           if (rockNum > scissorsNum && rockNum > paperNum)
              weaponAI = paper;
           else if (paperNum > scissorsNum && paperNum > rockNum)
              weaponAI = scissors;
           else
              weaponAI = rock;
        }
        else if (weaponNum >= 2 && weaponNum < 3)
        {
           if (scissorsNum > rockNum && scissorsNum > paperNum)
              weaponAI = rock;
           else if (paperNum > scissorsNum && paperNum > rockNum)
              weaponAI = scissors;
           else
              weaponAI = paper;
        }
        else if (weaponNum >= 3)
        {
           if (scissorsNum > rockNum && scissorsNum > paperNum)
              weaponAI = rock;
           else if (rockNum > scissorsNum && rockNum > paperNum)
              weaponAI = paper;
           else
              weaponAI = scissors;
        }
        //prompting for guess and assigning
        System.out.println("BATTLE START!");
        System.out.println("Choose your weapon: ");
        String weaponStr = kb.nextLine();
        if (weaponStr == "Rock" || weaponStr == "rock")
        {
           weaponP = rock;
           ++rockNum;
        }
        else if (weaponStr == "Scissors" || weaponStr == "scissors")
        {
           weaponP = scissors;
           ++scissorsNum;
        }
        else if (weaponStr == "Paper" || weaponStr == "paper")   
        {
           weaponP = paper;
           ++paperNum;
        }
        else if (weaponStr =="quit" || weaponStr == "Quit")
           System.exit(-1);

        //comparing AI and Players weapon
        //tied
        if (weaponAI == rock && weaponP == rock)
        {
           System.out.println("You both chose rock");
           ++ties;
        }
        else if (weaponAI == paper && weaponP == paper)
        {
           System.out.println("You both chose paper");
           ++ties;
        }   
        else if (weaponAI == scissors && weaponP == scissors)
        {
           ++ties;
           System.out.println("You both chose scissors");
        }
        //AI wins
        else if (weaponAI == rock && weaponP == scissors)
        {
           ++losses;
           System.out.println("AI wins");
        }
        else if (weaponAI == paper && weaponP == rock)
        {
           ++losses;
           System.out.println("AI wins");
        }
        else if (weaponAI == scissors && weaponP == paper)
        {
           ++losses;
           System.out.println("AI wins");
        }
        //Player wins
        else if (weaponAI == scissors && weaponP == rock)
        {
           ++wins;
           System.out.println("You win!");
        }
        else if (weaponAI == rock && weaponP == paper)
        {
           ++wins;
           System.out.println("You win!");
        }
        else if (weaponAI == paper && weaponP == scissors)
        {
           ++wins;
           System.out.println("You win!");
        }
        //prompting user to play again
        System.out.println("Would you like to play again? Y or N?");
        yorn = kb.nextLine();

     }//end do
     while (yorn == "y" || yorn == "Y" || yorn == "Yes" || yorn == "yes");

     //Displaying scores
     System.out.println("Game over.");
     System.out.println("You had " + wins + " wins.");
     System.out.println("You had " + losses + " losses.");
     System.out.println("You had " + ties + " ties.");
     System.out.println("You chose rock " + rockNum + " times.");
     System.out.println("You chose paper " + paperNum + " times.");
     System.out.println("You chose scissors " + scissorsNum + " times.");

  }//end game
}//end class

I am unsure why my output is the following when I enter "rock" and "y"

1.5102368482522261

BATTLE START!

Choose your weapon:

rock

Would you like to play again? Y or N?

y

Game over.

You had 0 wins.

You had 0 losses.

You had 0 ties.

You chose rock 0 times.

You chose paper 0 times.

You chose scissors 0 times.

It is almost like the program is ignoring all of my if else statements. I am sure it is something simple I am not understanding...

MaVRoSCy
  • 17,747
  • 15
  • 82
  • 125
user3547815
  • 51
  • 1
  • 1
  • 1
  • 1
    Aside from anything else, you should also strongly consider looking into `enum`s, as they're a better fit for this. An enum would allow you to put the logic for "what beats what" in a single place, separating it from the user interaction. – Jon Skeet Apr 24 '14 at 06:00
  • This is like the 5th flag for "comparing strings in java" I've had today... – takendarkk Apr 24 '14 at 06:23

1 Answers1

1

In order to compare String contents you have to replace == with equalsIgnoreCase().

You cannot compare two String contents with the == operator. The == operator in java compares String references (if one Object is some as another)

For your code to work you need something like:

if (weaponStr.equalsIgnoreCase("Rock")){
  ...
}

To see how String comparison is done in Java you can see also :

Community
  • 1
  • 1
MaVRoSCy
  • 17,747
  • 15
  • 82
  • 125
  • Well you *can*, but it will compare string references rather than their contents. – Jon Skeet Apr 24 '14 at 05:53
  • @JonSkeet yes of course you can, but as i understand the user wants to compare string contents – MaVRoSCy Apr 24 '14 at 05:57
  • 1
    Right, so make your answer say that. "You can't compare string contents using the `==` operator; that just compares whether the two string *references* are equal, i.e. whether they refer to the same object." That's more accurate and more helpful, IMO. – Jon Skeet Apr 24 '14 at 05:59
  • @JonSkeet Yes you are right. I 've already done that – MaVRoSCy Apr 24 '14 at 06:09
  • Well you've still not included the vital (IMO) word "contents" in your claim that you can't compare strings with the `==` operator. Up to you of course, but I think it would be a better answer with that in. Mind you, this question is a duplicate anyway... – Jon Skeet Apr 24 '14 at 06:19