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...