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.
Related
I am trying to get the "press q to quit" function to work properly, but I'm having some trouble. The "invalid Character" function is also not working properly. How would I go about fixing this? My professor suggested putting the "press q to quit" function in the beginning of my code, but hasn't given me any further instruction.
public static void main(String[] args) {
char userChar = 0;
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, then press Enter: ");
// If player chooses to quit
if (userChar == 'q' || userChar == 'Q') {
System.out.println("Player chose to quit. Goodbye!");
}
// Start of loop
while (userChar != 'q' && userChar != 'Q') {
// 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)
else 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)
else 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 types an invalid character
else {
System.out.println("Invalid input! Please enter a valid character.");
}
userChar = sc.next().charAt(0);
}
}
}
In order to really take care of invalid input you need to do it first thing in the loop. Moreover you can't do it in the else block of your if-else statements because in those if-else you check the randomNumber, not the input, so it doesn't make sense and it will never work. In addition you check the input as what you get from sc.next().charAt(0) but for example if the user insert a string like "R_invalid_something_without_space" you only look at the first letter which is R and you take it as a valid input. I modified your code so that you read a line in each loop iteration and if this line is not q / r / p / s it will consider as invalid input and if its q the program will finish.
public static void main(String[] args) {
char userChar = 0;
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, then press Enter: ");
// Start of loop
while (true) {
// what I changed
// **********************************************************
String line = sc.nextLine().toLowerCase();
if(line.equals("q")){
System.out.println("Player chose to quit. Goodbye!");
break;
}
if(!line.equals("r") && !line.equals("s") && !line.equals("p")) {
System.out.println("Invalid input! Please enter a valid character.");
continue;
}
userChar = line.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)
else 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)
else 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!");
}
}
}
}
Well, firstly your condition to quit is outside of scope of while loop which means it will never execute in this given context.
Secondly, condition in your while loop will not be true unless userChar is not q and Q simultaneously, which doesn't make sense.
What I suggest is to refactor your while loop little bit, as such:
while (true) {
//Your logic here
if (userChar == 'q' || userChar == 'Q') {
System.out.println("Player chose to quit. Goodbye!");
break;
}
//Your logic here
}
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++;
}
I am supposed to be making a simple rock paper scissors code but most of my println's aren't working. I tried adding another one to see if it was just my variable assignment for choose and rand but it doesn't output anything either. Any help is much appreciated!
import java.util.Scanner;
public class project4 {
public static void main(String[] args) {
Random in = new Random();
Scanner key = new Scanner(System.in);
System.out.println("Please select one of [R/P/S]: ");
String choice = key.nextLine();
int rand = in.nextInt(3) + 1;
int choose = 0;
if (choice.equals("r") || choice.equals("R")) {
choose = 1;
System.out.println("You chose Rock");
} else {
if (choice.equals("P") || choice.equals("p")) {
choose = 2;
System.out.println("You chose Paper");
} else {
if (choice.equals("s") || choice.equals("S")) {
choose = 3;
System.out.println("You chose Scissors");
}
}
System.out.println("rand= " + rand + "choose =" + choose);
System.out.flush();
}
if (rand == 1) {
System.out.println("I chose Rock");
} else {
if (rand == 2) {
System.out.println("I chose Paper");
} else {
System.out.println("I chose Scissors");
}
if (choose == rand) {
System.out.println("Its a Tie!");
} else {
if (choose == 1 && rand == 2) {
System.out.println("Paper beats Rock, You lose!");
} else {
if (choose == 1 && rand == 3) {
System.out.println("Rock beats Scissors, You win!");
} else {
if (choose == 2 && rand == 1) {
System.out.println("Paper beats Rock, You win!");
} else {
if (choose == 2 && rand == 3) {
System.out.println("Scissors beats Paper, You lose!");
} else {
if (choose == 3 && rand == 1) {
System.out.println("Rock beats Scissors, You lose!");
} else {
System.out.println("Scissors beats Paper, You win!");
}
}
}
}
}
}
}
}
}
Your System.out.println are being skipped because you have put some of them inside the 'else' conditions. Due to this when the 'IF' condition is true the else block is skipped and so is the sysout's. The below code should work!
Also I would suggest using else-if in place of the nested-if conditions as it make is more readable and less error prone.
package project4;
import java.util.Random;
import java.util.Scanner;
public class project4 {
public static void main(String[] args) {
Random in = new Random();
Scanner key = new Scanner(System.in);
System.out.println("Please select one of [R/P/S]: ");
String choice = key.nextLine();
int rand = in.nextInt(3) + 1;
int choose = 0;
if (choice.equals("r") || choice.equals("R")) {
choose = 1;
System.out.println("You chose Rock");
} else {
if (choice.equals("P") || choice.equals("p")) {
choose = 2;
System.out.println("You chose Paper");
} else {
if (choice.equals("s") || choice.equals("S")) {
choose = 3;
System.out.println("You chose Scissors");
}
}
}
System.out.println("rand= " + rand + "choose =" + choose);
System.out.flush();
if (rand == 1) {
System.out.println("I chose Rock");
} else {
if (rand == 2) {
System.out.println("I chose Paper");
} else {
System.out.println("I chose Scissors");
}
}
if (choose == rand) {
System.out.println("Its a Tie!");
} else {
if (choose == 1 && rand == 2) {
System.out.println("Paper beats Rock, You lose!");
} else {
if (choose == 1 && rand == 3) {
System.out.println("Rock beats Scissors, You win!");
} else {
if (choose == 2 && rand == 1) {
System.out.println("Paper beats Rock, You win!");
} else {
if (choose == 2 && rand == 3) {
System.out.println("Scissors beats Paper, You lose!");
} else {
if (choose == 3 && rand == 1) {
System.out.println("Rock beats Scissors, You lose!");
} else {
System.out.println("Scissors beats Paper, You win!");
}
}
}
}
}
}
}
}
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 6 years ago.
I'm a new programmer and trying to teach myself Java by doing random projects. Below is a "Rock, Paper, Scissors" game and the issue that I'm facing is after printing "a", the program ends and does not continue onto the if else statements below. Any help that can be given would be greatly appreciated.
package com.company;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
System.out.println("Hello & Welcome to Rock, Paper, Scissors. What's your name?");
Scanner scan = new Scanner(System.in);
String userChoice = scan.nextLine();
System.out.println("Hello, " + userChoice + ". Let's start a game!");
Scanner scan = new Scanner(System.in);
System.out.println("Choose one: Rock, Paper, Scissors");
String userFirstChoice = scan.nextLine();
System.out.println("You chose " + userFirstChoice);
double a = Math.random();
System.out.println(a);
if (a >= 0.00 && a<= 0.3){
if ( userFirstChoice== "Rock"){
System.out.println("Rock vs Rock: TIE");
}
else if (userFirstChoice == "Paper"){
System.out.println("Rock vs Paper: YOU LOSE!");
}
else if (userFirstChoice == "Scissors"){
System.out.println("Rock vs Scissors: YOU WIN!");
}
}
else if (a>=0.3 && a<=0.6){
if(userFirstChoice == "Paper"){
System.out.println("Paper vs Paper: TIE!");
}
else if (userFirstChoice == "Rock"){
System.out.println("Rock vs Paper: YOU LOSE!");
}
else if(userFirstChoice == "Scissors"){
System.out.println("Scissors vs Paper: YOU WIN!");
}
}
else if (userFirstChoice == "Scissors"){
System.out.println("Scissors vs Scissors: TIE!");
}
else if (userFirstChoice == "Paper"){
System.out.println("Paper vs Scissors: YOU LOSE!");
}
else if (userFirstChoice == "Rock") {
System.out.println("Rock vs Scissors: YOU WIN!");
}
}
}
You can't use == to compare strings in Java. (== compares the references, and the operands to == could be references to different strings even though the string contents are identical.)
Use userFirstChoice.equals("Scissors") etc. instead.
Your use of the relational operators on the double types is correct.
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;
}