Rock Paper Scissors does not work as expected [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I'm doing this Rock, Paper, Scissors Java program for an introduction to programming class and for some reason, the program is automatically terminating after the first user input.
I have a feeling it has to do with the
int randomNumber = rnd.nextInt(3) + 1;
statement but I'm not sure what I'm doing wrong here.
public static void main(String[] args) {
char userChar;
Scanner sc = new Scanner(System.in);
Random rnd = new Random();
// Intro/directions/prompting for user input
System.out.println("Welcome to Rock, Paper, Scissors by Rancid!");
System.out.println("Choose R for Rock, P for Paper, S for Scissors, or Q to Quit, and press Enter: ");
userChar = sc.next().charAt(0);
// Prompting computer to generate a random number
int randomNumber = rnd.nextInt(3) + 1;
// If computer generates 1 (Rock)
if (randomNumber == 1) {
if (userChar == 'r' || userChar == 'R') {
System.out.println("Rock vs. Rock! It's a tie!");
} else if (userChar == 'p' || userChar == 'P') {
System.out.println("Paper covers Rock, you win!");
} else if (userChar == 's' || userChar == 'S') {
System.out.println("Rock breaks Scissors, you lose!");
}
// If computer generates 2 (Paper)
if (randomNumber == 2) {
if (userChar == 'r' || userChar == 'R') {
System.out.println("Paper covers Rock, you lose!");
} else if (userChar == 'p' || userChar == 'P') {
System.out.println("Paper vs. Paper! It's a tie!");
} else if (userChar == 's' || userChar == 'S') {
System.out.println("Scissors cuts Paper, you win!");
}
// If computer generates 3 (Scissors)
if (randomNumber == 3) {
if (userChar == 'r' || userChar == 'R') {
System.out.println("Rock breaks Scissors, you win!");
} else if (userChar == 'p' || userChar == 'P') {
System.out.println("Scissors cuts Paper, you lose!");
} else if (userChar == 's' || userChar == 'S') {
System.out.println("Scissors vs. Scissors! It's a tie!");
}
// If player chooses to quit
if (userChar == 'q' || userChar == 'Q') {
System.out.println("Player chose to quit. Goodbye!");
}
// If player types an invalid character
else {
System.out.println("Invalid input! Please enter a valid character.");
}
}
}
}
}
}

Welcome to stackoverflow.
It seems like you're not using any loop inside your main method, so your program simply closes after it executed the last statement.
You want to add something like:
while (!"q".equals(userChar) && !"Q".equals(userChar)) {
System.out.println("Choose R for Rock, P for Paper, S for Scissors, or Q to Quit, and press Enter: ");
userChar = sc.next().charAt(0);
// Add your code where you check the randomNumber and the userChar here
}
Moreover you want to check your curly braces as the comments already state.

Related

How do I count the playerwins for my code

Aye, so I have this assignment to make a rock, paper, scissor game. I did mostly everything right (maybe), but I can't figure out how to count the playerwins at the end when stopping a game of rock, paper, scissors. It is the one thing I am missing when running and stopping the code.
Playerwins are at the very bottom and the very top. The code is long as hell, but im new at coding and don't know how to make it less redundant.
import java.util.*;
import java.io.*;
public class RockPaperScissors {
public static void main(String[] args) {
Scanner dodongo = new Scanner(System.in);
Random hamster = new Random();
System.out.println("Rock, Paper, Scissors!");
System.out.println("Press 1 for Rock, 2 for Paper, or 3 for Scissors");
while (!dodongo.hasNextInt(4)) {
System.out.println("Only numbers 1-3!");
System.out.println("Press 1 for Rock, 2 for Paper, or 3 for Scissors");
dodongo.next();
}
int player = dodongo.nextInt();
int rock = 1;
int paper = 2;
int scissors = 3;
int playerwins = 0;
System.out.println("YOU: Rock, Paper, Scissors! ");
System.out.println();
if ((player >= 0) && (player <= 1)) {
System.out.println("Rock!");
} else if ((player >= 1) && (player <= 2)) {
System.out.println("Paper!");
} else if ((player >= 2) && (player <= 3)) {
System.out.println("Scissors!");
}
System.out.println("Computer is playing...");
int shoot = -1;
System.out.println("COM: Rock, Paper, Scissors!");
do {
int keyblade = hamster.nextInt(3) + 1;
if ((keyblade >= 0) && (keyblade <= 1)) {
System.out.println("Rock!");
} else if ((keyblade >= 1) && (keyblade <= 2)) {
System.out.println("Paper!");
} else if ((keyblade >= 2) && (keyblade <= 3)) {
System.out.println("Scissors!");
}
shoot++;
if ((player == rock) && (keyblade == paper)) {
System.out.println("COM Win!");
System.out.println("You Lose! Paper beats rock, because a piece of paper can cover a rock!");
} else if ((player == rock) && (keyblade == scissors)) {
System.out.println("You Win! Rock beats scissors, because a rock can break a pair of scissors!");
} else if ((player == paper) && (keyblade == rock)) {
System.out.println("You Win! Paper beats rock, because a piece of paper can cover a rock!");
} else if ((player == paper) && (keyblade == scissors)) {
System.out.println("COM Win!");
System.out.println("You Lose! Scissors beats paper, because scissors can cut paper!");
} else if ((player == scissors) && (keyblade == rock)) {
System.out.println("COM Win!");
System.out.println("You Lose! Rock beats scissors, because a rock can break a pair of scissors!");
} else if ((player == scissors) && (keyblade == paper)) {
System.out.println("You Win! Scissors beats paper, because scissors can cut paper!");
} else {
System.out.println("Tie!");
}
playerwins++;
} while (shoot != 0);
System.out.println("Want to play again?(Press Y or N)");
String tryagain = dodongo.next();
if (tryagain.equalsIgnoreCase("Y")) {
main(null);
} else {
System.out.println("Adios...");
System.out.println("Total Wins: " + playerwins);
}
System.out.println();
}
}
I broke your code into methods. For the most part, this just organized the code a little better. I was able to use the writeInput method twice, so I also slightly reduced the duplication.
Once I created methods, the code in the main method was reduced enough that I could make a game loop using a do-while loop and remove the recursion.
I also corrected the playerwins count so it increments only when the player wins. The corrected code is in the compareInputs method.
Here's your code after adding methods.
import java.util.Random;
import java.util.Scanner;
public class RockPaperScissors {
public static void main(String[] args) {
Scanner dodongo = new Scanner(System.in);
Random hamster = new Random();
int playerwins = 0;
String tryagain;
do {
int player = readPlayerInput(dodongo);;
System.out.println("YOU: Rock, Paper, Scissors! ");
System.out.println();
writeInput(player);
System.out.println("Computer is playing...");
System.out.println("COM: Rock, Paper, Scissors!");
int keyblade = hamster.nextInt(3) + 1;
writeInput(keyblade);
playerwins += compareInputs(player, keyblade);
System.out.println("Want to play again?(Press Y or N)");
tryagain = dodongo.next();
} while (tryagain.equalsIgnoreCase("Y"));
System.out.println("Adios...");
System.out.println("Total Wins: " + playerwins);
System.out.println();
}
private static int readPlayerInput(Scanner dodongo) {
System.out.println("Rock, Paper, Scissors!");
System.out.println("Press 1 for Rock, 2 for Paper, or 3 for Scissors");
while (!dodongo.hasNextInt(4)) {
System.out.println("Only numbers 1-3!");
System.out.println("Press 1 for Rock, 2 for Paper, or 3 for Scissors");
dodongo.next();
}
return dodongo.nextInt();
}
private static void writeInput(int input) {
if ((input >= 0) && (input <= 1)) {
System.out.println("Rock!");
} else if ((input >= 1) && (input <= 2)) {
System.out.println("Paper!");
} else if ((input >= 2) && (input <= 3)) {
System.out.println("Scissors!");
}
}
private static int compareInputs(int player, int keyblade) {
int rock = 1;
int paper = 2;
int scissors = 3;
int playerWins = 0;
if ((player == rock) && (keyblade == paper)) {
System.out.println("COM Win!");
System.out.println("You Lose! Paper beats rock, because a "
+ "piece of paper can cover a rock!");
} else if ((player == rock) && (keyblade == scissors)) {
System.out.println("You Win! Rock beats scissors, because a "
+ "rock can break a pair of scissors!");
playerWins = 1;
} else if ((player == paper) && (keyblade == rock)) {
System.out.println("You Win! Paper beats rock, because a "
+ "piece of paper can cover a rock!");
playerWins = 1;
} else if ((player == paper) && (keyblade == scissors)) {
System.out.println("COM Win!");
System.out.println("You Lose! Scissors beats paper, because "
+ "scissors can cut paper!");
} else if ((player == scissors) && (keyblade == rock)) {
System.out.println("COM Win!");
System.out.println("You Lose! Rock beats scissors, because a "
+ "rock can break a pair of scissors!");
} else if ((player == scissors) && (keyblade == paper)) {
System.out.println("You Win! Scissors beats paper, because "
+ "scissors can cut paper!");
playerWins = 1;
} else {
System.out.println("Tie!");
}
return playerWins;
}
}
The first thing you need when you program any game is that you need a game loop, which runs the game until the user exits it. Interestingly, you opt for recursively calling main, which is also possible (although it could theoretically overload stack, which probably won't happen in this simple case but could be a real problem in a game that loops many times per second for a long time).
So the easiest fix would be to move the playerwins variable out of the main function (then you need to make it static as well) but the correct code that builds better habits for later work would be to use another while loop instead of recursively calling main.
Normally, beginners start with iterative code and discover recursion later, so it is nice that you discover it so early, but unfortunately that is not the correct case for it, but keep it in your pocket for other occasions.

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.

if-else Rock Paper Scissors Game

I can't see why my program terminates. Example is if I stated the user input is rock, it will sometimes state its a tie or nothing happens/terminates. I tried running bits and pieces of it in different class area but still when I input a value it won't output the results I want in the if/else statements.
I know that the computer choice and user input code is correct. I believe it is the if/else that I am messing up. Correct me if I am wrong..
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int compVal = (int) (3*Math.random()) +1;
char compChoice;
if (compVal == 1) {
compChoice = 'R';
} else if (compVal == 2) {
compChoice = 'P';
} else {
compChoice = 'S';
}
System.out.println("Rock, Paper, Scissors-Enter a choice R/P/S: ");
String mine = keyboard.nextLine();
char myChoice = mine.charAt(0);
myChoice = Character.toUpperCase(myChoice);
if (myChoice == (compChoice)) {
System.out.println("We both chose the same item-try again");
} else if (myChoice == ('R')) {
if (compChoice == 'P') {
System.out.println("I chose Paper and you chose Rock: Paper covers rock, so I win");
}
} else if (myChoice == ('R')) {
if (compChoice == ('S')) {
System.out.println("I chose Scissors and you choise Rock: Rock breaks Scissors, so you win");
}
} else if (myChoice == ('P')) {
if (compChoice == ('S')) {
System.out.println("I chose Scissors and you chose Paper: Scissors cuts Paper, so I win");
}
} else if (myChoice == ('P')) {
if (compChoice == ('R')) {
System.out.println("I chose Rock and you chose Paper: Paper covers Rock, so you win");
}
} else if (myChoice == ('S')) {
if (compChoice == ('P')) {
System.out.println("I chose Paper and you chose Scissors: Scissors cuts Paper, so you win");
}
} else if (myChoice == ('S')) {
if (compChoice == ('R')) {
System.out.println("I chose Rock and you chose Scissors: Rock breaks Scissors, so I win");
}
}
}
You have multiple if/else statements which can never be executed.
Consider this code
if (myChoice == ('R')) {
if (compChoice == 'P') {
System.out.println("I chose Paper and you chose Rock: Paper covers rock, so I win");
}
} else if (myChoice == ('R')) { // this will ONLY execute if the previous if statement is false.
Every second if clause cannot execute because you have already checked for that condition. i.e. the choise cannot be R because if it was it would have executed the previous block. The simplest solution is to delete the duplicates as you don't need them.
} else if (myChoice == ('R')) {
if (compChoice == 'P') {
System.out.println("I chose Paper and you chose Rock: Paper covers rock, so I win");
}
if (compChoice == ('S')) {
System.out.println("I chose Scissors and you choise Rock: Rock breaks Scissors, so you win");
}
or even
} else if (myChoice == 'R') {
if (compChoice == 'P')
System.out.println("I chose Paper and you chose Rock: Paper covers rock, so I win");
else
System.out.println("I chose Scissors and you choise Rock: Rock breaks Scissors, so you win");
} else

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

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();

How to loop a JOptionsPane

Alright well, I semi figured it out with some searching, but now I have another issue
When the second box pops up and I click 'no' the first box still runs and if I cancel it I get an error. What am I doing wrong?
import javax.swing.*;
import java.util.*;
import java.util.Scanner;
public class RPS {
public static void main(String[] args) {
Scanner sc = new Scanner (System.in);
String input;
int user;
int computer;
while (true){//here we go
input = JOptionPane.showInputDialog("What'll it be? Rock, paper, or scissors?\n" +
"1 for rock, 2 for paper, and 3 for scissors: ",JOptionPane.YES_NO_OPTION);
user = Integer.parseInt(input);
Random randomnum = new Random ();
computer = randomnum.nextInt(3);
if (user == 1 && computer == 0){System.out.println ("You played Rock! You have tied");
JOptionPane.showMessageDialog(null, "Tie!");}
else if (user == 1 && computer == 1){System.out.println ("You have played Rock! You have lost");
JOptionPane.showMessageDialog(null, "Paper beats rock. You lose!");}
else if (user == 1 && computer == 2){System.out.println ("You have played Rock! You have won");
JOptionPane.showMessageDialog(null, "Rock beats scissors. You win!");}
else if (user == 2 && computer == 0){System.out.println ("You have played Paper! You have won");
JOptionPane.showMessageDialog(null, "Paper beats rock. You win!");}
else if (user == 2 && computer == 1){System.out.println ("You have played Paper! You have tied");
JOptionPane.showMessageDialog(null, "Tie!");}
else if (user == 2 && computer == 2){System.out.println ("You have played Paper! You have lost");
JOptionPane.showMessageDialog(null, "Scissors beats paper. You lose!");}
else if (user == 3 && computer == 0){System.out.println ("You have played Scissors! You have lost");
JOptionPane.showMessageDialog(null, "Rock beats Scissors. You lose!");}
else if (user == 3 && computer == 1){System.out.println ("You have played Scissors! You have won");
JOptionPane.showMessageDialog(null, "Scissors beats paper. You win!");}
else if (user == 3 && computer == 2){System.out.println ("You have played Scissors! You have tied");
JOptionPane.showMessageDialog(null, "Tie!");}
int n = JOptionPane.showConfirmDialog(null,"Would you like to play again?", "Confirmation",JOptionPane.YES_NO_OPTION);
if(n == JOptionPane.YES_OPTION) {
JOptionPane.showMessageDialog(null,"Let's start");
} else {
JOptionPane.showMessageDialog(null,"Goodbye");
}
}
}
}
Put a return; statement after JOptionPane.showMessageDialog(null,"Goodbye");. otherwise it doesn't know to end the while loop.
The error when you press cancel would happen also if you chose cancel right away. It's because parseInt doesn't know how to read the resulting input. You should check the return value of showInputDialog to see that it's valid output. I would suggest checking for "1", "2", or "3" (or "rock", "paper", or "scizzors") instead of using parseInt, and then if it doesn't match any of them to come up with an error message of some sort.

Categories