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.
Related
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;
}
}
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.
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
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();
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 ==