*UPDATE* now my program compiles but doesn't run? - java

I am writing a program that prompts the user to play rock paper scissors against the computer. I'm having trouble calling the int rpsls method from the main method. I am not sure how to call it from the main method because I need the value of user input from the main method to be inputted into the rpsls method. The following is what I have so far, and yes it's full of errors, but I am mainly concerned with calling the method.
UPDATE - I added some more code, and now the program will compile. However, when I try to run it it just freezes. Can anyone help find a solution to make the program run?
import java.util.Scanner;
public class Rpsls
{
public static void main (String [] args) {
int wins = 0;
int ties = 0;
int losses = 0;
int retVal = 0;
int output = rpsls(retVal);
//while loop
Scanner input = new Scanner (System.in);
int x = input.nextInt();
while (x > 0 && x <= 5) {
//input gesture
System.out.println(" ROCK PAPER SCISSORS LIZARD SPOCK");
System.out.println(" (1) (2) (3) (4) (5)");
System.out.print("Enter your choice, or 0 to end: ");
//call rpsls for inputted gesture
int gesture = input.nextInt();
rpsls(gesture);
//returned values: loss, win, or tie
if (output == -1 ) {
losses++;
}
else if (output == 0) {
ties++;
}
else {
wins++;
}
//count wins and losses
//end while loop
//print sum of wins and losses.
}
System.out.println("You won " + wins + " games, lost " + losses + " games, and tied " + ties + " games.");
}
public static int rpsls(int gesture) {
//generate random gesture for computer
int attack = (int)(Math.random()*5);
//decide who won
int wins = 0; int losses = 0; int ties = 0;
int retVal = 0;
if (gesture == 1 && attack == 1) {
System.out.println("You chose ROCK.");
System.out.println("Computer chose ROCK.");
System.out.println("It's a tie!");
retVal = 0;
}
if (gesture == 1 && attack == 2) {
System.out.println("You chose ROCK.");
System.out.println("Computer chose PAPER.");
System.out.println("PAPER covers ROCK.");
System.out.println("Computer wins!");
retVal = -1;
}
if (gesture == 1 && attack == 3) {
System.out.println("You chose ROCK.");
System.out.println("Computer chose SCISSORS.");
System.out.println("ROCK crushes SCISSORS.");
System.out.println("You win!");
retVal = 1;
}
if (gesture == 1 && attack == 4) {
System.out.println("You chose ROCK.");
System.out.println("Computer chose LIZARD.");
System.out.println("ROCK crushes LIZARD.");
System.out.println("You win!");
retVal = 1;
}
if (gesture == 1 && attack == 5) {
System.out.println("You chose ROCK.");
System.out.println("Computer chose SPOCK.");
System.out.println("SPOCK vaporizes ROCK.");
System.out.println("Computer wins!");
retVal = -1;
}
if (gesture == 2 && attack == 1) {
System.out.println("You chose PAPER.");
System.out.println("Computer chose ROCK.");
System.out.println("PAPER covers ROCK.");
System.out.println("You win!");
retVal = 1;
}
if (gesture == 2 && attack == 2) {
System.out.println("You chose PAPER.");
System.out.println("Computer chose PAPER.");
System.out.println("It's a tie!");
retVal = 0;
}
if (gesture == 2 && attack == 3) {
System.out.println("You chose PAPER.");
System.out.println("Computer chose SCISSORS.");
System.out.println("SCISSORS cut PAPER.");
System.out.println("Computer wins!");
retVal = -1;
}
if (gesture == 2 && attack == 4) {
System.out.println("You chose PAPER.");
System.out.println("Computer chose LIZARD.");
System.out.println("LIZARD eats PAPER.");
System.out.println("Computer wins!");
retVal = -1;
}
if (gesture == 2 && attack == 5) {
System.out.println("You chose PAPER.");
System.out.println("Computer chose SPOCK.");
System.out.println("PAPER disproves SPOCK.");
System.out.println("You win!");
retVal = 1;
}
if (gesture == 3 && attack == 1) {
System.out.println("You chose SCISSORS.");
System.out.println("Computer chose ROCK.");
System.out.println("ROCK crushes SCISSORS.");
System.out.println("Computer wins!");
retVal = -1;
}
if (gesture == 3 && attack == 2) {
System.out.println("You chose SCISSORS.");
System.out.println("Computer chose PAPER.");
System.out.println("SCISSORS cut PAPER.");
System.out.println("You win!");
retVal = 1;
}
if (gesture == 3 && attack == 3) {
System.out.println("You chose SCISSORS.");
System.out.println("Computer chose SCISSORS.");
System.out.println("It's a tie!");
retVal = 0;
}
if (gesture == 3 && attack == 4) {
System.out.println("You chose SCISSORS.");
System.out.println("Computer chose LIZARD.");
System.out.println("SCISSORS decapitate LIZARD.");
System.out.println("You win!");
retVal = 1;
}
if (gesture == 3 && attack == 5) {
System.out.println("You chose SCISSORS.");
System.out.println("Computer chose SPOCK.");
System.out.println("SPOCK smashes SCISSORS.");
System.out.println("Computer wins!");
retVal = -1;
}
if (gesture == 4 && attack == 1) {
System.out.println("You chose LIZARD.");
System.out.println("Computer chose ROCK.");
System.out.println("ROCK crushes LIZARD.");
System.out.println("Computer wins!");
retVal = -1;
}
if (gesture == 4 && attack == 2) {
System.out.println("You chose LIZARD.");
System.out.println("Computer chose PAPER.");
System.out.println("LIZARD eats PAPER.");
System.out.println("You win!");
retVal = 1;
}
if (gesture == 4 && attack == 3) {
System.out.println("You chose LIZARD.");
System.out.println("Computer chose SCISSORS.");
System.out.println("SCISSORS decapitate LIZARD.");
System.out.println("Computer wins!");
retVal = -1;
}
if (gesture == 4 && attack == 4) {
System.out.println("You chose LIZARD.");
System.out.println("Computer chose LIZARD.");
System.out.println("It's a tie!");
retVal = 0;
}
if (gesture == 4 && attack == 5) {
System.out.println("You chose LIZARD.");
System.out.println("Computer chose SPOCK.");
System.out.println("LIZARD poisons SPOCK.");
System.out.println("You win!");
retVal = 1;
}
if (gesture == 5 && attack == 1) {
System.out.println("You chose SPOCK.");
System.out.println("Computer chose ROCK.");
System.out.println("SPOCK vaporizes ROCK.");
System.out.println("You win!");
retVal = 1;
}
if (gesture == 5 && attack == 2) {
System.out.println("You chose SPOCK.");
System.out.println("Computer chose PAPER.");
System.out.println("PAPER disproves SPOCK.");
System.out.println("Computer wins!");
retVal = -1;
}
if (gesture == 5 && attack == 3) {
System.out.println("You chose SPOCK.");
System.out.println("Computer chose SCISSORS.");
System.out.println("SPOCK smashes SCISSORS.");
System.out.println("You win!");
retVal = 1;
}
if (gesture == 5 && attack == 4) {
System.out.println("You chose SPOCK.");
System.out.println("Computer chose LIZARD.");
System.out.println("LIZARD poisons SPOCK.");
System.out.println("Computer wins!");
retVal = -1;
}
if (gesture == 5 && attack == 5) {
System.out.println("You chose SPOCK.");
System.out.println("Computer chose SPOCK.");
System.out.println("It's a tie!");
retVal = 0;
}
return retVal;
}
}

This line:
int rpsls = gesture(x);
...creates an int varaible called rpsls in your main function, calls a method called gesture, and passes x into it.
You may have meant:
int gesture = input.nextInt();
rpsls(gesture);
...which calls nextInt on your Scanner to get a number from the user, remembers it as gesture, then calls your existing rpsls method, passing in gesture.

You are calling gesture(int) and you were probably trying to invoke int rpsls(int gesture)
int retVal = rpsls(gesture);
Some variables are initialized and declared inside code blocks that affects its lifetime, for example input is inside the while block so it will not work outside that;
int rpsls(int gesture) is poorly designed: the computer should shoose a random value for rock/paper/scissors and then compare it with the user;

Your code doesn't work, you need to at least:
move int x = input.nextInt();after while (x > 0 && x <= 5)statement, without this it will not use you input for game,
move Scanner declaration before first use of it, couse it will not compile,
change all 'x' in rpsls method on 'gesture', the rpsls method don't have an access to 'x' variable form main method;
initialize 'retVal' variable (give it a value), for exaple 0, to avoid compilation error;
change int rpsls = gesture(x);for for example int rpsls = rpsls(x);by this, it will proceed on your input
After those changes, it works fine in my opinion.
EDIT:
You should remove int output = rpsls(retVal); and retVal declaration from main, couse it is useless i think, and add int output = rpsls(x); after x = input.nextInt(); then it works properly.
Also replace int x = input.nextInt(); with int gesture = input.nextInt();

Related

while loop is skipping over my if/else statements

I have this while loop of a Rock Paper Scissors game and I have if/else if statements that tell you which player won, but the while loop is skipping it and going straight to the end of the loop where it asks if you want to play another game. How would I be able to change this?
Scanner scan = new Scanner(System.in);
String play = "";
System.out.print("Please enter Play if you want to play the game or anything else to Stop");
play = scan.nextLine();
while (play.equalsIgnoreCase("play")) {
System.out.println("Game " + gameCount + " Rock, Paper, Scissors - Play!");
System.out.print("Choose your weapon [R]ock, [P]aper, or [S]cissors: ");
String rps = scan.nextLine();
while (rps.equals('R') || rps.equals('P') || rps.equals('S')) {
System.out.println("You chose: " + rps);
}
int rand = (int)(Math.random() * 3);
String myMove = "";
if(rand == 0) {
myMove = "Rock";
}
else if(rand == 1) {
myMove = "Paper";
}
else {
myMove = "Scissors";
}
System.out.println("I chose: " + myMove);
if(rps.equals(myMove)) {
System.out.println("Tie!");
tieCount++;
}
else if(rps.equals('P') && myMove.equals("Scissors")) {
System.out.println("Scissors beats paper, a win for me!");
myCount++;
}
else if(rps.equals('S') && myMove.equals("Rock")) {
System.out.println("Rock beats scissors, a win for me!");
myCount++;
}
else if(rps.equals('R') && myMove.equals("Paper")) {
System.out.println("Paper beats rock, a win for me!");
myCount++;
}
else if(rps.equals('S') && myMove.equals("Paper")) {
System.out.println("Scissors beats paper, a win for you!");
userCount++;
}
else if(rps.equals('R') && myMove.equals("Scissors")) {
System.out.println("Rock beats scissors, a win for you!");
userCount++;
}
else if(rps.equals('S') && myMove.equals("Paper")) {
System.out.println("Paper beats rock, a win for you!");
userCount++;
}
gameCount++;
System.out.println("Please enter Play if you want to play the game again or anything else to Stop.");
play = scan.nextLine();
}
'''
Try this out
while (true) {
System.out.println("Insert question code:");
String question = scanner.nextLine();
if(question.equals("quit")){
break;
}
System.out.println("Insert answer code:");
String answer = scanner.nextLine();
if(answer.equals("quit")){
break;
}
}

How can I assign user input (words) an integer value to be used in the code

I'm attempting to create a game of "Rock Paper Scissors Lizard Spock", and want the user to be able to put in either the integers assigned to the variables (Rock=0, Paper=1, Scissors=2, Lizard=3 and Spock=4) and also be able to enter the words "Rock", "Paper", "Scissors", "Lizard" or "Spock". Could you help me include a part of the code where I can assign the string inputs to integers. I also do not want to change the main framework of the code. I also know that the website says not to paste the entire file, but I cannot think of another way to show my problem. Please note that I have been using a website called SoloLearn. Any help would be greatly appreciated.
import java.util.Scanner;
public class RockPaperScissorsLizardSpock {
final static int ROCK = 0;
final static int PAPER = 1;
final static int SCISSORS = 2;
final static int LIZARD = 3;
final static int SPOCK = 4;
public static void main(String[] args) {
double r = Math.random();
int computerChoice = (int)(3.0 * r);
Scanner input = new Scanner(System.in);
System.out.print("Enter 0 for Rock, 1 for Paper, 2 for Scissors, 3 for Lizard, 4 for Spock: ");
int playerChoice = input.nextInt();
System.out.println(computerChoice);
int playerChoice = 0;
switch (playerChoice) {
case "Rock":
playerChoice = 0;
break;
case "Paper":
playerChoice = 1;
break;
case "Scissors":
playerChoice = 2;
case "Lizard":
playerChoice = 3;
case "Spock":
playerChoice = 4;
if (computerChoice == playerChoice) {
System.out.println("Tie");
}
else if (computerChoice == ROCK && playerChoice == SCISSORS) {
System.out.println("I chose Rock,You chose Scissors, Rock crushes Scissors, You lose.");
}
else if (computerChoice == SCISSORS && playerChoice == PAPER) {
System.out.println("I chose Scissors, You chose Paper, Scissors cut Paper, You lose.");
}
else if (computerChoice == PAPER && playerChoice == ROCK) {
System.out.println("I chose Paper,You chose Rock, Paper covers Rock, You lose.");
}
else if (computerChoice == LIZARD && playerChoice == PAPER) {
System.out.println("I chose Lizard, You chose Paper, Lizard eats Paper, You lose.");
}
else if (computerChoice == SPOCK && playerChoice == SCISSORS) {
System.out.println("I chose Spock, You chose Scissors, Spock smashes Scissors, You lose.");
}
else if (computerChoice == ROCK && playerChoice == LIZARD) {
System.out.println("I chose Rock, You chose Lizard, Rock crushes Lizard, You lose.");
}
else if (computerChoice == SCISSORS && playerChoice == LIZARD) {
System.out.println("I chose Scissors, You chose Lizard, Scissors decapitates Lizard, You lose.");
}
else if (computerChoice == SPOCK && playerChoice == PAPER) {
System.out.println("I chose Spock, You chose Paper, Paper disproves Spock, You lose.");
}
else if (computerChoice == SPOCK && playerChoice == ROCK) {
System.out.println("I chose Spock, You chose Rock, Spock vaporizes Rock, You lose.");
}
else if (computerChoice == SCISSORS && playerChoice == ROCK) {
System.out.println("I chose Scissors, You chose Rock, Rock crushes Scissors, You win.");
}
else if (computerChoice == PAPER && playerChoice == SCISSORS) {
System.out.println("I chose Paper, You chose Scissors, Scissors cut Paper, You win.");
}
else if (computerChoice == ROCK && playerChoice == PAPER) {
System.out.println("I chose Rock, You chose Paper, Paper covers Rock,You win.");
}
else if (computerChoice == PAPER && playerChoice == LIZARD) {
System.out.println("I chose Paper, You chose Lizard, Lizard eats Paper, You win.");
}
else if (computerChoice == SCISSORS && playerChoice == SPOCK) {
System.out.println("I chose Scissors, You chose Spock, Spock smashes Scissors, You win.");
}
else if (computerChoice == LIZARD && playerChoice == ROCK) {
System.out.println("I chose Lizard, You chose Rock, Rock crushes Lizard, You win.");
}
else if (computerChoice == LIZARD && playerChoice == SCISSORS) {
System.out.println("I chose Lizard, You chose Scissors, Scissors decapitates Lizard, win.");
}
else if (computerChoice == PAPER && playerChoice == SPOCK) {
System.out.println("I chose Paper, You chose Spock, Paper disproves Spock, You win.");
}
else if (computerChoice == ROCK && playerChoice == SPOCK) {
System.out.println("I chose Rock, You chose Spock, Spock vaporizes Rock, You win.");
}else{
System.out.println("Error");
}
}
}
}
If you want that your user can input 2 Values for 1 condition you can convert the value from playerChoice to String for a temp String and then make a switch case for it like the following:
String tempString = Integer.toString(playerChoice);
switch (tempString) {
case "0":
case "Rock":
playerChoice = 0;
break;
case "1":
case "Paper":
playerChoice = 1;
break;
case "2":
case "Scissors":
playerChoice = 2;
break;
case "3":
case "Lizard":
playerChoice = 3;
break;
case "4":
case "Spock":
playerChoice = 4;
break;
}
You could put your allowed inputs in a map, key being the text and value being the associated number:
private static final Map<String, Integer> ALLOWED_INPUTS = Collections.unmodifiableMap(Map.of(
"ROCK", 0,
"PAPER", 1,
"SCISSORS", 2,
"LIZARD", 3,
"SPOCK", 4
));
Then, in your main function, check if the map contains a key that corresponds to the user input, if so get the corresponding value. If not, check if the user entered a number. Keep looping while the resulting number either way isn't in the map's values.
Scanner in = new Scanner(System.in);
int playerChoice = -1;
String input;
while(!ALLOWED_INPUTS.containsValue(playerChoice)) {
input = in.nextLine();
if (ALLOWED_INPUTS.containsKey(input))
playerChoice = ALLOWED_INPUTS.get(input);
else {
try {
playerChoice = Integer.parseInt(input);
} catch(NumberFormatException e) {}
}
}
System.out.println("You choosed choice number " + playerChoice);
Assuming : Either Words or Integers are entered as inputs.
Steps for this solution:
Always Entering String Input: String choice = input.next();
Checking if choice contains only digits, if yes,convert this to Integer else don't do anything.
if entered input is words, it will go to switch first,then set playerChoice and run properly.
if entered input is digit, it will assign converted integer to playerChoice first, then it will go to switch but as no default exists, it will run properly.
String choice = input.next();
if (choice.matches("\\d+"))
playerChoice = Integer.parseInt(choice);
switch (choice.toUpperCase()) {
case "ROCK":
playerChoice = 0;
break;
case "PAPER":
playerChoice = 1;
break;
case "SCISSORS":
playerChoice = 2;
break;
case "LIZARD":
playerChoice = 3;
break;
case "SPOCK":
playerChoice = 4;
break;
}

How do I solve the loop issues? It is the place of the brackets but I cannot figure out where they should be placed

I am trying to code Rock, paper, scissors, lizard, Spock. My code is attached and the loops keep repeating. I believe the curly braces are placed wrong but I cannot figure out where without getting an error message.
public class RockPaperScissors_v2 {
public static void main(String[] args) {
//scanner
Scanner inputReader = new Scanner(System.in);
//randomizer for comp
Random rand = new Random();
//variables
int rock = 1;
int paper = 2;
int scissors = 3;
int lizard = 4;
int spock = 5;
int round;
int choice;
int compChoice = 1;
int countOfWins = 0;
int countOfLosses = 0;
int countOfTies = 0;
//ask how many rounds to play; max 10 --> if more than 10, print error message and quit.
do {
System.out.println("How many rounds do you want to play?");
//output
round = inputReader.nextInt();
if (round < 1 || round > 10) {
System.out.println("Error Message: Please type a number between 1 and 10.");
return;
}
} while (round != 0);
System.out.println("Please select a number from the follow:");
System.out.println("1) Rock"
+ "\n 2) Paper"
+ "\n 3) Scissors"
+ "\n 4) Lizard"
+ "\n 5) Spock");
compChoice = rand.nextInt(5 - 2 + 2) + 1;
choice = inputReader.nextInt();
//if statements for all scenarios
if (compChoice == 1) {
if (choice == 1) {
System.out.println("Tie game.");
countOfTies++;
} else if (choice == 2) {
System.out.println("Rock gets covered by paper. You win!");
countOfWins++;
} else if (choice == 3) {
System.out.println("Rock crushes scissors. You lose.");
countOfLosses++;
} else if (choice == 4) {
System.out.println("Rock crushes lizard. You lose.");
countOfLosses++;
} else if (choice == 5) {
System.out.println("Spock vaporizes rock. You win!");
countOfWins++;
}
} else if (compChoice == 2) {
if (choice == 1) {
System.out.println("Paper covers rock. You lose.");
countOfLosses++;
} else if (choice == 2) {
System.out.println("Tie game.");
countOfTies++;
} else if (choice == 3) {
System.out.println("Scissors cuts paper. You win!");
countOfWins++;
} else if (choice == 4) {
System.out.println("Lizard eats paper. You win!");
countOfWins++;
} else if (choice == 5) {
System.out.println("Paper disproves Spock. You win!");
countOfWins++;
}
} else if (compChoice == 3) {
if (choice == 1) {
System.out.println("Rock crushes scissors. You lose.");
countOfLosses++;
} else if (choice == 2) {
System.out.println("Scissors cuts paper. You lose.");
countOfLosses++;
} else if (choice == 3) {
System.out.println("Tie game.");
countOfTies++;
} else if (choice == 4) {
System.out.println("Scissors decapitates lizard. You lose.");
countOfLosses++;
} else if (choice == 5) {
System.out.println("Spock smashes scissors. You win!");
countOfWins++;
}
} else if (compChoice == 4) {
if (choice == 1) {
System.out.println("Rock crushes lizard. You win!");
countOfWins++;
} else if (choice == 2) {
System.out.println("Lizard eats paper. You lose.");
countOfLosses++;
} else if (choice == 3) {
System.out.println("Scissors decapitates lizard. You win!");
countOfWins++;
} else if (choice == 4) {
System.out.println("Tie game.");
countOfTies++;
} else if (choice == 5) {
System.out.println("Lizard poisons Spock. You lose.");
countOfLosses++;
}
} else if (compChoice == 5) {
if (choice == 1) {
System.out.println("Rock crushes lizard. You lose.");
countOfLosses++;
} else if (choice == 2) {
System.out.println("Lizard eats paper. You win!");
countOfWins++;
} else if (choice == 3) {
System.out.println("Scissors decapitates lizard. You lose.");
countOfLosses++;
} else if (choice == 4) {
System.out.println("Lizard poisons Spock. You win!");
countOfWins++;
} else if (choice == 5) {
System.out.println("Tie game.");
countOfTies++;
}
}
//print out wins, losses, and ties
System.out.println("Number of Wins:" + countOfWins);
System.out.println("Number of Losses: " + countOfLosses);
System.out.println("Number of Ties: " + countOfTies);
//ask user if wants to play again
System.out.println("Do you want to play again (Y/N)?");
String playAgain = inputReader.next();
if (playAgain.equals("Y")) {
} else {
System.out.println("Thank you for playing.");
System.exit(0);
}
}
}
With while (round != 0); the loop will exit only when you are inserting 0, which seems to be wrong and also will never happen since in that case you are closing the program with return.
I suggest to remove the return and to change the while condition with while (round < 1 || round > 10);, which will terminate the loop only when a value between 1 and 10 is inserted.

Infinite loop in rock-paper-scissors loop game

I am currently struggling to implement the while loop in my game as it always gives me an infinity loop. The idea is to continue the game except only when user types "-1"
// declare variables
int player, computer;
int counter = 0;
// player input
System.out.println("Rock, Paper, Scissors!");
System.out.print("Enter 0 for paper, 1 for Scissors, or 2 for Rock (-1 to quit) : ");
player = sc.nextInt();
while (player != -1) {
// switch statement for player
switch (player) {
case 0:
System.out.println("Player picks Paper");
break;
case 1:
System.out.println("Player picks Scissors");
break;
case 2:
System.out.println("Player picks Rock");
break;
case -1:
System.exit(-1);
break;
default:
System.out.println("Invalid input");
}
// generate a random number for computer
Random randomGen = new Random();
computer = randomGen.nextInt(3);
// switch statement for computer
switch (computer) {
case 0:
System.out.println("Computer picks Paper");
break;
case 1:
System.out.println("Computer picks Scissors");
break;
case 2:
System.out.println("Computer picks Rock");
break;
default:
}
// output for each condition
if (player == 2 && computer == 1)
System.out.println("Player Wins!");
else if (player == 1 && computer == 0)
System.out.println("Player Wins!");
else if (player == 0 && computer == 2)
System.out.println("Player Wins!");
else if (computer == 1 && player == 0)
System.out.println("Computer Wins!");
else if (player == 0 && computer == 2)
System.out.println("Computer Wins!");
else if (player == 1 && computer == 2)
System.out.println("Computer Wins!");
else
System.out.println("Draw");
sc.close();
}
You never update your player value. Add player = sc.nextInt() to the end of the loop body. Or, do something like
int player;
while ((player = sc.nextInt()) != -1) {
And, I believe you should also remove the call to sc.close();

Why is my program not displaying the correct output? [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
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...
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 :
How do I compare strings in Java?
Java String.equals versus ==

Categories