Error in Rock Paper Scissors program [closed] - java

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
The original prompt had us making a rock paper scissors game I think my code should work so far but I am getting the error "cannot find symbol" in line 64 when I try to track if a tie happens. Does anyone know what I am doing wrong?
import java.util.*;
public class RPS
{
public static void main(String[] args)
{
Scanner myScanner = new Scanner(System.in);
Random myRandom = new Random();
boolean n = false;
System.out.println("Welcome to the Rock, Paper, Scissors game!!");
System.out.println("When you play, your choices are rock, paper, or scissors");
do
{
int compChoice = myRandom.nextInt(3);
String rock = "0";
String paper = "1";
String scissors = "2";
Integer.parseInt(rock);
Integer.parseInt(paper);
Integer.parseInt(scissors);
int compWins = 0;
int humanWins = 0;
int ties = 0;
System.out.print("Enter your choice: ");
int choice = Integer.parseInt(myScanner.nextLine());
for(int i = 0; i < 3; i++)
{
if(choice == 0 && compChoice == 1)
{
System.out.println("I choose paper: I win this turn");
i++;
compWins++;
} else if(choice == 0 && compChoice == 2)
{
System.out.println("I choose scissors: You win this turn");
i++;
humanWins++;
} else if(choice == 1 && compChoice == 0)
{
System.out.println("I choose rock: You win this turn");
i++;
humanWins++;
} else if(choice == 1 && compChoice == 2)
{
System.out.println("I choose scissors: I win this turn");
i++;
compWins++;
} else if(choice == 2 && compChoice == 0)
{
System.out.println("I choose rock: I win this turn");
i++;
compWins++;
} else if(choice == 2 && compChoice == 1)
{
System.out.println("I choose paper: You win this turn");
i++;
humanWins++;
} else if(choice == compchoice)
{
System.out.println("This round is a tie");
i++;
ties++;
}
System.out.println("Score: me: " + compWins + "you: " + humanWins + "ties: " + ties);
}
if(compWins > humanWins)
{
System.out.println("Game over: I win!");
} else if(humanWins > compWins)
{
System.out.println("Game over: You win!");
}else
System.out.println("Game over: No one wins!");
System.out.print("Play again? (y/n)");
String ending = myScanner.nextLine();
if(ending == n)
{
n = true;
}
}
while(!n);
System.out.println("Thank you for playing!");
System.exit(0);
}
}

compchoice is not camel case. You declared it as compChoice.

You have 2 errors:
Java is case-sensitive, and you are using compchoice which is not declared anywhere. I guess you wanted to use compChoice instead:
} else if (choice == compChoice) {
You are comparing a String with a boolean in the line
if (ending == n) {
which is not a valid comparison because they are from different types. You may want to check your logic there.

Actually Java is case sinsitive, when you declared a variable you have to stick with that declaration.
So try to change :
} else if(choice == compChoice) // here you need to change compchoice to compChoice
{
System.out.println("This round is a tie");
i++;
ties++;
}
System.out.println("Score: me: " + compWins + "you: " + humanWins + "ties: " + ties);
Also You comparing a String with boolean, you need to parse the String to boolean then compare:
boolean ending = Boolean.valueOf(myScanner.nextLine());
if(ending == n)
{
n = true;
}

Related

Method returns proper value but if loop does not act accordingly

I've written out all the code for an assignment for my programming class. The assignment requires us to create a program that allows the user to play rock paper scissors against a computer. We're required to have individual methods to obtain the computer's choice, the user's choice, check if the user's choice is valid, and also to determine the winner of the match. If a match doesn't end in a tie we need to print why the match was won, eg. "Scissors cuts Paper," and print who wins. Everything works properly, except when the user wins it's never counted or announced. For example instead of printing:
The computer's choice was rock. The user's choice was paper. Paper covers Rock. The user wins!
it prints:
The computer's choice was rock. The user's choice was paper. Paper covers Rock.
import java.util.Scanner;
import java.util.Random;
public class FinalRockPaperScissors {
//Computer's Choice
public static int computersChoice (int options) {
Random randGen = new Random();
int computerValue = randGen.nextInt(options)+1;
System.out.println(computerValue); //FOR TESTING ONLY
return computerValue;
}
//Player's Choice
public static int usersChoice () {
Scanner scnr = new Scanner(System.in);
System.out.print("Enter 1 for rock, 2 for paper, or 3 for scissors: ");
int userValue = scnr.nextInt();
if (isValid(userValue) == true) {
return userValue;
}
else {
userValue = 0;
return userValue;
}
}
//Check for valid user input
public static boolean isValid (int userInput){
if (userInput == 1 || userInput == 2 || userInput == 3) {
return true;
}
else {
return false;
}
}
//Checking winner
public static char determineWinner () {
char win;
int computerValue = computersChoice(3);
int userValue = usersChoice();
//print computer choices
if (computerValue == 1) {
System.out.println("The computer's choice was rock.");
}
else if (computerValue == 2) {
System.out.println("The computer's choice was paper.");
}
else if (computerValue == 3){
System.out.println("The computer's choice was scissors.");
}
//print user choices
if (userValue == 1) {
System.out.println("The user's choice was rock.");
}
else if (userValue == 2) {
System.out.println("The user's choice was paper.");
}
else if (userValue == 3){
System.out.println("The user's choice was scissors.");
}
//check who won
if (computerValue == 1) { //rock vs
if (userValue == 2) { //paper
System.out.println("Paper wraps Rock.");
return win = 'b';
}
else if (userValue == 3) { //scissors
System.out.println("Rock smashes Scissors.");
return win = 'a';
}
else if (userValue == 1){ //rock
return win = 'c';
}
else {
System.out.println("The user chose an invalid number. This round will be ignored.");
return win = 'd';
}
}
else if (computerValue == 2) { //paper vs
if (userValue == 2) { //paper
return win = 'c';
}
else if (userValue == 3) { //scissors
System.out.println("Scissors cuts Paper.");
return win = 'b';
}
else if (userValue == 1){ //rock
System.out.println("Paper wraps Rock.");
return win = 'a';
}
else {
System.out.println("The user chose an invalid number. This round will be ignored.");
return win = 'd';
}
}
else { //scissors vs
if (userValue == 2) { //paper
System.out.println("Scissors cuts Paper.");
return win = 'a';
}
else if (userValue == 3) { //scissors
return win = 'c';
}
else if (userValue == 1){ //rock
System.out.println("Rock smashes Scissors.");
return win = 'b';
}
else {
System.out.println("The user chose an invalid number. This round will be ignored.");
return win = 'd';
}
}
}
public static void main(String[] args) {
int userWins = 0;
int computerWins = 0;
int ties = 0;
int error = 0;
//for (int i = 0; i < 1; i++) { //5 for testing purposes
if (determineWinner() == 'a') {
System.out.println("The computer wins!");
System.out.println("");
computerWins++;
}
else if (determineWinner() == 'b') {
System.out.println("The user wins!");
System.out.println("");
userWins++;
}
else if (determineWinner() == 'c'){
System.out.println("The game is tied!");
System.out.println("");
ties++;
}
else {
error++;
}
//}
System.out.println("The number of ties is " + ties);
System.out.println("The number of user wins is " + userWins);
System.out.println("The number of computer wins is " + computerWins);
//output final winner
if (computerWins > userWins) {
System.out.println("Computer is the winner.");
}
else if (userWins > computerWins) {
System.out.println("User is the winner.");
}
else {
if (userWins == computerWins) {
System.out.println("User is the winner.");
}
else if (computerWins == ties) {
System.out.println("Computer is the winner.");
}
}
}
}
After some testing I've discovered that the problem may be with my userchoice() method. If I disable this method and give a set value for the user, everything works as it should. The problem is I don't know why it doesn't work, and as a result I can't fix it.
I think you're so new to Java and also let's start to show you your mistake in this code, you called determineWinner() multiple times which shouldn't be like that because you repeat the game four times to determine the result once, so you should call it once and get the returned value and check the value, like:-
char result = determineWinner(); //Play the game and get the result
// use it in conditions
if (result == 'a') {
System.out.println("The computer wins!");
System.out.println("");
computerWins++;
}
else if (result == 'b') {
System.out.println("The user wins!");
System.out.println("");
userWins++;
}
else if (result == 'c'){
System.out.println("The game is tied!");
System.out.println("");
ties++;
}
else {
error++;
}
In your main function You are calling determineWinner() every time you test your if condition. The proper way would be to store the returned value in a variable and then check if that variable is 'a', 'b' or 'c'. For example:
char result = determineWinner();
if (result == 'a') {
System.out.println("The computer wins!");
System.out.println("");
computerWins++;
}

excess the limit of random numbers in java?

I am actually working on a jackpot game where it generates random numbers and asks user to guess the random number, if it meets the criteria it prints win and if not it moves to other conditional statements.
I have almost finished writing my code and just I got confused on this step. My query is:
I have set a limit a on randInt(max) i.e random numbers but I want to know that if it exceeds its limit, it must show a message which I can't help myself out.
Please share your ideas if anyone can.
public class Jackpot {
static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
int mode;
System.out.println("Choose the difficulty level mode");
System.out.println("1: Easy (0-15)");
System.out.println("2: Medium (0-30)");
System.out.println("3: Difficult (0-50)");
System.out.println("or type another number to quit");
mode = input.nextInt();
if(mode == 1){
Start_Game(randInt(15));
}else if(mode == 2){
Start_Game(randInt(30));
}else if(mode == 3){
Start_Game(randInt(50));
}else if(mode <= 4 || mode >= 0){
System.out.println("Quit");
Restart_Game();
}
}
public static int randInt(int max){
int randomNum = (int) (Math.random() * ((max) + 1));
return randomNum;
}
public static int Start_Game(int max){
System.out.println("Please enter your guessed number");
int GuessNum , life = 5;;
do{
GuessNum = input.nextInt();
if(GuessNum == max){
System.out.println("You WIN");
break;
}else if(GuessNum > max){
System.out.println("BIG");
life--;
System.out.println("life: " + life);
}else if(GuessNum < max){
System.out.println("SMALL");
life--;
System.out.println("life: " + life);
}
}while(life != 0);
Restart_Game();
return 0;
}
public static void Restart_Game(){
System.out.println("if you want to restart the game \nPress Y to continue or N to exit");
char restart = input.next().charAt(0);
if(restart == 'Y' || restart == 'y'){
//Start_Game(randInt(10));
int mode;
System.out.println("Choose the difficulty level mode");
System.out.println("1: Easy (0-15)");
System.out.println("2: Medium (0-30)");
System.out.println("3: Difficult (0-50)");
System.out.println("or type another number to quit");
mode = input.nextInt();
if(mode == 1){
Start_Game(randInt(15));
}else if(mode == 2){
Start_Game(randInt(30));
}else if(mode == 3){
Start_Game(randInt(50));
}else if(mode <= 4 || mode >= 0){
System.out.println("Quit");
Restart_Game();
}
} else if(restart == 'n'|| restart == 'N'){
System.out.println("Thank you for playing");
System.exit(0);
}
}
private static int randIt(int max) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
Your "Game" don't have a clue of the actual range, even if you have a parameter call "max", it is only the value to found, you could renamed it or you could really send the maximum value accepted :
Start_Game(15);
And let the game generate a random value based on that. You have the max value and the random value to found. Easy to check your case now.
public static int Start_Game(int max){
int toFind = randInt(max);
do{
GuessNum = input.nextInt();
if(GuessNum > max){
System.out.println("Out of range");
} else if(GuessNum == toFind ){
System.out.println("You WIN");
break;
}else if(GuessNum > toFind ){
System.out.println("BIG");
life--;
System.out.println("life: " + life);
}else if(GuessNum < toFind ){
System.out.println("SMALL");
life--;
System.out.println("life: " + life);
}
}while(life != 0);
}
Note : You should rename your method and variable, JAVA has a convention (like other language), don't name a variable starting with a uppercase for instance.
public static int startGame(int max){
or
int guessNum;

Why isn't my for loop working as I want it to?

I am trying to make the loop terminate after either the computer or the player wins 5 games. I tried to do that with the for loop, but it isn't stopping at all. I tried to debug and figure it out but I am lost.
Can you please help me figure out where I went wrong? Any suggestions to make the code more efficient would be very appreciated too.
import java.util.Scanner;
import java.util.Random;
public class RockPaperScissor {
public static void main (String [] args){
System.out.println("Lets play Rock, Paper, Scissors!");
Scanner scanner = new Scanner(System.in);
int computerwins=0;
int playerwins=0;
Random random = new Random();
for (int totalGame = 0; playerwins<=5 || computerwins<=5; ++totalGame){
int gamenumber = totalGame + 1;
System.out.print("Game " + gamenumber + ". " + "Pick one! 1 = Rock, 2 = Paper, 3 = Scissor : ");
int input = scanner.nextInt();
Random rand = new Random();
int computerResponse = rand.nextInt(3) + 1;
String compResponse = "something";
switch (computerResponse) {
case 1: compResponse = "Rock";
break;
case 2: compResponse = "Paper";
break;
case 3: compResponse = "Scissor";
break; }
String you = "something";
switch (input) {
case 1: you = "Rock";
break;
case 2: you = "Paper";
break;
case 3: you = "Scissor";
break;
}
System.out.println("You: " + you + ", Computer: " + compResponse);
if(input == computerResponse) {
System.out.println("It's a tie!"); }
else if (input == 1) {
if (computerResponse == 2) {
System.out.println("Paper eats rock. You lose!");
++computerwins; }
else if (computerResponse == 3) {
System.out.println("Rock crushes scissors. You win!");
++playerwins; } }
else if (input == 2) {
if (computerResponse == 1) {
System.out.println("Paper eats rock. You win!");
++playerwins; }
else if (computerResponse == 3) {
System.out.println("Scissor cuts paper. You lose!");
++computerwins; } }
else if (input ==3) {
if (computerResponse == 1) {
System.out.println("Rock crushes scissors. You lose!");
++computerwins; }
else if (computerResponse == 2) {
System.out.println("Scissor cuts paper. You win!");
++playerwins; } }
System.out.println("Score Now: You = " + playerwins + ", Computer = " + computerwins );
System.out.println(" "); }
if (playerwins == computerwins) {
System.out.println("Aww! It's a tie."); }
if (playerwins < computerwins) {
System.out.println("Aww! You lost!"); }
if (playerwins > computerwins) {
System.out.println("Yay! You won!");}
} }
Thank you!!
In your for loop termination condition
playerwins<=5 || computerwins<=5
This continues as long as EITHER condition is true. You want to change it to && to continue until one condition becomes false.

For Loops In Java [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 7 years ago.
So I'm creating a simple rock paper scissors and I have made a mistake recently. I'm just beginning to learn and I made a mistake and lost track of where. I'd really really appreciate any pointers of where I made a mistake. When it prints out the prompt, I type in what I want, and it just prints it out again. Thank you!
import java.util.Scanner;
import java.util.Random;
import java.io.*;
public class rock_Paper_Scissor {
public static void main (String[] args) {
String playerhand;
boolean x = true;
Scanner input = new Scanner(System.in);
Random num = new Random();
int rand = num.nextInt(2) + 1;
System.out.println("I challenge you to Rock Paper Scissor");
System.out.println("If you want to quit, type exit twice");
System.out.println("Type Rock, Paper, or scissor");
playerhand = input.nextLine();
String hands = playerhand.toLowerCase();
while (x == true) {
if (hands == "rock") {
if (rand == 1) {
System.out.println("Rock vs. Rock: TIE");
} else if (rand == 2) {
System.out.println("Rock vs. Scissor: YOU WIN");
} else if (rand == 3) {
System.out.println("Rock vs. Paper: YOU LOSE");
}
}
else if (hands == "paper") {
if (rand == 1) {
System.out.println("Paper vs. Rock: YOU WIN");
} else if (rand == 2) {
System.out.println("Paper vs. Scissor: YOU LOSE");
} else if (rand == 3) {
System.out.println("Paper vs. Paper: TIE");
}
}
else if (hands == "scissor") {
if (rand == 1) {
System.out.println("Scissor vs. Rock: YOU LOSE");
} else if (rand == 2) {
System.out.println("Scissor vs. Scissor: TIE");
} else if (rand == 3) {
System.out.println("Scissor vs. Paper: YOU WIN");
}
}
else if (hands == "exit") {
System.out.println("Thank you for playing!");
x = false;
}
System.out.println("Please type your hand to play again: ");
hands = input.nextLine();
}
}
}
In your all if condition try to use eqauls() method instead of == like this:
if ("rock".equals(hands)) {
...
else if ("paper".equals(hands)) {
...
else if ("scissor".equals(hands )) {
...
else if ("exit".equals(hands)) {
.equals() is used to compare strings values.
But == compare with references strings.

Java rock paper scissors loop

I'm having to make a paper rock scissors program that has the user enter in a choice, then tests it against the computer's choice. After every game, it should ask the player if they want to continue, and they should enter in 'Y' or 'N' to continue or quit. The best I could think was a while loop, and everything works fine except the very last bit.
import java.util.Scanner;
public class rockpaperscissors {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
char cont = 'y';
while (cont == 'y'){
int com = (int)(Math.random() * 3);
System.out.println("Paper (0), Rock (1), or Scizzors (2)?");
int hum = input.nextInt();
if (com==(hum))
System.out.println("It's a tie!");
else if (hum == 0)
{
if (com == 1)
System.out.println ("You chose paper, computer chose rock You Win!");
else if (com == 2)
System.out.println ("You chose paper, Computer chose scissors You Lose!");
}
else if (hum == 1)
{
if (com == 2)
System.out.println ("You chose Rock, computer chose scissors You Win!");
else if (com == 0)
System.out.println ("You chose Rock, Computer chose paper You Lose!");
}
else if (hum == 2)
{
if (com == 0)
System.out.println ("You chose Scissors, computer chose paper You Win!");
else if (com == 1)
System.out.println ("You chose Scissors, Computer chose rock You Lose!");
}
System.out.println("Would you like to continue? (Y/N)");
cont = input.nextLine().charAt(0);
}
}
}
When I run it, the loop runs fine, the game is played, but then I get a 'string out of index range' error. Any idea how to resolve this?
Your nextInt() just reads the number from the input buffer, leaving the new line in it. So when you call input.nextLine() you're getting an empty line - the rest of the first line after the number. You should read the next-line and make sure it's not empty. If it is, just read it again.
Incidentally, your code that figures out who won is a bit cumbersome. If I were you, I would try to make it a little more general and clean. Think about a solution that can handle a more complex game, such as Rock Paper Scissors Lizard Spock without adding too much code.
When you get the answer from the user, you don't read the next line so the scanner still has a new line character. Then when you read the nextline you read that new line, and therefore there is no charat(0).
Change:
cont = input.nextLine().charAt(0);
to:
cont = input.next().charAt(0);
package rockpaper;
import java.util.Scanner;
/**
*
* #author Allen E.
*/
public class RockPaper {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
int rock = 0;
int paper = 1;
int Scissors = 2;
int user = 0;
int computer = 0;
int gamesplayed = 0;
Scanner scan = new Scanner(System.in);
while (gamesplayed < 3)
{
System.out.println("Rock = 0 , Paper = 1, Scissors = 2");
String userinput = scan.nextLine();
int convertinput = Integer.valueOf(userinput);
int Computerinput = (int)(Math.random()*3);
if (Computerinput == 1 && convertinput == 0)
{
System.out.println("Paper beats Rock " +
"\nThe computer won");
gamesplayed++;
computer++;
}
else if (convertinput == 1 && Computerinput == 0)
{
System.out.println("Paper beats Rock " +
"\nYou Win!");
gamesplayed++;
user++;
}
if (Computerinput == 0 && convertinput == 2)
{
System.out.println("Rock beats Scissors " +
"\nThe computer won");
gamesplayed++;
computer++;
}
else if (convertinput == 0 && Computerinput == 2)
{
System.out.println("Rock beats Scissors " +
"\nYou Win!");
gamesplayed++;
user++;
}
if (Computerinput == 2 && convertinput == 1)
{
System.out.println("Scissors beats Paper " +
"\nThe computer won");
gamesplayed++;
computer++;
}
else if (convertinput == 2 && Computerinput == 1 )
{
System.out.println("Scissors beats Paper " +
"\nYou Win");
gamesplayed++;
user++;
}
/*************************************************
* *
* *
* Handling a tie *
* *
* *
*************************************************/
if (Computerinput == 0 && convertinput == 0)
{
System.out.println("Rock ties Rock " +
"\nTie");
}
if (Computerinput == 1 && convertinput == 1)
{
System.out.println("Paper ties Paper " +
"\nTie");
}
if (Computerinput == 2 && convertinput == 2)
{
System.out.println("Scissors ties Scissors " +
"\nTie");
}/*End of While Loop*/
}
}
}

Categories

Resources