Not equal to conditions in while loop [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
I am new to Java and am building a Rock, Paper, Scissors game. I am trying to figure out why my while statement will not recognize the correct input on the first try. Thank you for any help in advance.
`
import java.util.Scanner;
import java.util.Random;
public class RPC
{
public static void main (String[] args)
{
//Utilities
Scanner in = new Scanner (System.in);
Random r = new Random ();
//variables
int userwins =0;
int computerwins =0;
int numberofgames = 0;
int randomnumber = 0;
String userpick = "";
String computerpick = "";
// Welcome statement
System.out.println("Welcome to Rock, Paper, Scissors!");
//Get the number of games from the user
System.out.println("Please enter the number of rounds you would like to play:");
numberofgames = in.nextInt();
//Flush the buffer
in.nextLine();
// Game play for the number of times the user specified.
for (int i=1; i <= numberofgames; i++)
{
System.out.println("Rock, Paper, or Scissors?:");
userpick = in.nextLine();
while (!userpick.equalsIgnoreCase("rock") && !userpick.equalsIgnoreCase("paper") && !userpick.equalsIgnoreCase("scissors"));
{
System.out.println("Sorry," +userpick+ " is not a valid entry.Please enter Rock, Paper, or Scissors.");
userpick = in.nextLine();
}
//Make a random choice
randomnumber = r.nextInt(3)+1;
if (randomnumber == 1)
{
computerpick = ("rock");
}
if (randomnumber == 2)
{
computerpick = ("paper");
}
if (randomnumber == 3)
{
computerpick = ("scissors");
}
//Decide who wins the round
if (userpick.equalsIgnoreCase("rock")) {
if (computerpick.equalsIgnoreCase("scissors"))
System.out.println("Rock beats scissors. You win!");
else if (computerpick.equalsIgnoreCase("paper"))
System.out.println("Paper beats rock. The computer wins!");
else
System.out.println("Computer chooses rock. It's a tie.");
}
else if (userpick.equalsIgnoreCase("paper")) {
if (computerpick.equalsIgnoreCase("scissors"))
System.out.println("Scissors beats paper. The computer wins!");
else if (computerpick.equalsIgnoreCase("rock"))
System.out.println("Paper beats rock. You win!");
else
System.out.println("Computer chooses paper. It's a tie.");
}
else if (userpick.equalsIgnoreCase("scissors")) {
if (computerpick.equalsIgnoreCase("rock"))
System.out.println("Rock beats scissors. The computer wins!");
else if (computerpick.equalsIgnoreCase("paper"))
System.out.println("Scissors beats paper. You win!");
else
System.out.println("Computer chooses scissors. It's a tie.");
}
}
}
}`

Remove the semi-colon which is terminating the while statement
while (...);
^

Remove semicolon after the for loop. Semicolon after while/for/if means its a null statement which means the very next block of codes with in braces acts just as a block which will always gets evaluated irrespective of whether the condition holds true or not.

The semicolon generally ends a line in java. In order to make the while loop to run you need to remove the semi column.

Related

Java rock-paper-scissors program does not print right output

I want my program to display what the computer chose.
But instead, it sometimes does not, or sometimes it does display
The computer chose Rock
The computer chose Paper
The computer chose Scissors
This happens even if i follow the same input pattern.
Output when the user inputs 1 , 2 , 3 in order-
Similarly, output when the user again inputs 1 , 2 , 3 in order-
Output when the user inputs randomly-
Code-
import java.util.Random;
import java.util.Scanner;
public class RockPaperScissors {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Random play= new Random();
System.out.println("Best of three of Rock paper scissors!");
int win=0;
int i =1;
while(i<=3){
int number=play.nextInt(3);
System.out.println("Press 1 for Rock");
System.out.println("Press 2 for Paper");
System.out.println("Press 3 for Scissor");
int ch=scanner.nextInt();
if(number==1)
System.out.println("The computer chose Rock");
if(number==2)
System.out.println("The computer chose Paper");
if(number==3)
System.out.println("The computer chose Scissor");
if(ch==1 && number==1)
System.out.println("Draw");
else if(ch==1 && number==2)
System.out.println("Lost");
else if(ch==1 && number==3){
System.out.println("Won");
win++;}
else if(ch==2 && number==1){
System.out.println("Won");
win++;}
else if(ch==2 && number==2)
System.out.println("Draw");
else if(ch==2 && number==3)
System.out.println("Lost");
else if(ch==3 && number==1)
System.out.println("Lost");
else if(ch==3 && number==2){
System.out.println("Won");
win++;}
else if(ch==3 && number==3)
System.out.println("Draw");
i++;
}
if(win==3 || win==2)
System.out.println("You won the game!");
else
System.out.println("Aww you lost the game.");
}
}
The issue is with int number = play.nextInt(3). This method returns either 0, 1, or 2 (documentation here), whereas your code expects 1, 2, 3.
A simple fix would be to do int number = play.nextInt(3) + 1.

RPS game making it say invalid error

I made this Rock Paper scissors game but i cnt figure out how to make it show invalid error when the user enters something other than R,P,S. It would be helpful if any once could tell me how to make it so that it does this. Im a relatively new coder. Thanks in advance for all your help
import java.util.Random;
import java.util.Scanner;
public class RPS {
public static void main(String[] args)
{
String userPlay; //User's play -- "R", "P", or "S"
String computerPlay = ""; //Computer's play -- "R", "P", or "S"
int computerInt;
String response;
Scanner scan = new Scanner(System.in);
Random generator = new Random();
System.out.println("Lets play Rock, Paper, Scissors!\n" +
"Choose your move.\n" + "Rock = R, Paper" +
"= P, and Scissors = S.");
System.out.println();
//Generate computer's play (0,1,2)
computerInt = generator.nextInt(3)+1;
//Translate computer's randomly generated play to
//string using if //statements
if (computerInt == 1)
computerPlay = "R";
else if (computerInt == 2)
computerPlay = "P";
else if (computerInt == 3)
computerPlay = "S";
//Get player's play from input-- note that this is r
System.out.println("Enter your play: ");
userPlay = scan.next();
//Make player's play uppercase
userPlay = userPlay.toUpperCase();
//Print computer's play
System.out.println("Your opponents play is: " + computerPlay);
//See who won.
if (userPlay.equals(computerPlay))
System.out.println("It's a tie!");
else if (userPlay.equals("R"))
if (computerPlay.equals("S"))
System.out.println("Rock breaks scissors. You win!!");
else if (computerPlay.equals("P"))
System.out.println("Paper covers rock. You lose!!");
else if (userPlay.equals("P"))
if (computerPlay.equals("S"))
System.out.println("Scissor cuts paper. You lose!!");
else if (computerPlay.equals("R"))
System.out.println("Paper covers rock. You win!!");
else if (userPlay.equals("S"))
if (computerPlay.equals("P"))
System.out.println("Scissor cuts paper. You win!!");
else if (computerPlay.equals("R"))
System.out.println("Rock breaks scissors. You lose!!");
else
System.out.println("Invalid user input.");
}
}
I think the best way to do it is with loop. You ask to enter player's move, if it is unacceptable you ask again. You continue it until user enters valid string. In code it will look like this
//Get player's play from input
boolean moveOk = false;
while (moveOk == false) {
System.out.println("Enter your play: ");
userPlay = scan.next();
//Make player's play uppercase
userPlay = userPlay.toUpperCase();
// check that the input is ok
if ("R".equals(userPlay) ||
"P".equals(userPlay) ||
"S".equals(userPlay)) {
moveOk = true;
} else {
System.out.println("Bad input, try again!");
}
}
If you want to show error and then stop the program, instead of creating the loop you can use simple if with similar condition and when input is incorrect print error message and use return.
Also you could make your code easier to understand if you divide your conditions in parts, for example by user input. See example below.
//See who won.
String result = "";
if (userPlay.equals(computerPlay)) {
result = "It's a tie!";
}
if (userPlay.equals("R")) {
if (computerPlay.equals("S")) {
result = "Rock breaks scissors. You win!!";
}
if (computerPlay.equals("P")) {
result = "Paper covers rock. You lose!!";
}
}
if (userPlay.equals("P")) {
if (computerPlay.equals("S")) {
result = "Scissor cuts paper. You lose!!";
}
if (computerPlay.equals("R")) {
result = "Paper covers rock. You win!!";
}
}
if (userPlay.equals("S")) {
if (computerPlay.equals("P")) {
result = "Scissor cuts paper. You win!!";
}
if (computerPlay.equals("R")) {
result = "Rock breaks scissors. You lose!!";
}
}
System.out.println(result);
Also you could use switch statement.

how to stop a loop in java if a value is incremented consecutively for three times?

i'm new to java. i'm writing a sample of rock paper scissors game. the user input 0,1,2 for rock, paper, scissors respectively. the program randomly generated the result. i almost managed to get it work but the only thing i'm stuck at is how to STOP THE FOR LOOP IF ONE OF THE SIDE WINS CONSECUTIVELY FOR THREE TIMES.
THIS IS THE ORIGINAL QUESTION
Write a program that plays scissor-rock-paper game. The rule is that a scissor wins against a paper, a
rock wins against a scissor, and a paper wins against a rock. The program represent scissor as 0, rock
as 1, and paper as 2.
The game is to be played between the computer and a player. The program will prompt the user to
enter the number of rounds to win. For example, if the user enter 4 then the winner has to win at
least 3 out of the 4 rounds. If either party win 3 times consecutively, the game ends early without the
fourth round. If the user enter 5 rounds, the winner has to win at least 3 out of the 5 rounds. The
same rule of consecutive 3-wins also apply. After the user has entered the number of rounds, the
computer randomly generates a number between 0 and 2. The program then prompts the user to
enter a number 0, 1, or 2. After the last round (subject to the above mentioned early-winner-rule),
the program display a message indicating whether the computer or the user wins, loses, or draws.
THIS IS MY CODE.
package assignment1;
import java.util.Scanner;
import java.util.Random;
public class question1_9 {
// This program is used to play scissor-rock-paper game.
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
Random random = new Random();
int scissor = 0, rock = 1, paper = 2, round, userinput,comprand, userresult = 0, compresult = 0, control,j,k;
// Variables used in this program is declared and initialized.
/* Number of rounds wished to be play are obtained from user. */
System.out.println("WELCOME TO ROCK PAPER SCISSOR GAME. ");
System.out.println("PLEASE ENTER THE NUMBER OF ROUND YOU WANT TO PLAY: ");
round = scan.nextInt();
control = (round/2)+1;
for(int i = 0; i<round; i++)
{
if (compresult == control | userresult == control)
{
break;
}
System.out.println("ROUND " + (i+1));
System.out.println("PLEASE ENTER:\n 0 for scissor \n 1 for rock \n 2 for paper \n");
userinput = scan.nextInt();
comprand = random.nextInt(3);
if (userinput == 0)
{
if (comprand == 0)
{
System.out.println("COMPUTER IS SCISSOR");
System.out.println("DRAW!!!");
i--;
}
else if (comprand == 1)
{
System.out.println("COMPUTER IS ROCK");
System.out.println("COMPUTER WINS!!!");
compresult++;
}
else
{
System.out.println("COMPUTER IS PAPER");
System.out.println("YOU WIN!!!");
userresult++;
}
}
else if (userinput == 1)
{
if (comprand == 0)
{
System.out.println("COMPUTER IS SCISSOR");
System.out.println("COMPUTER WINS!!!");
compresult++;
}
else if (comprand == 1)
{
System.out.println("COMPUTER IS ROCK");
System.out.println("YOU WIN!!!");
userresult++;
}
else
{
System.out.println("COMPUTER IS PAPER");
System.out.println("DRAW!!!");
i--;
}
}
else
{
if (comprand == 0)
{
System.out.println("COMPUTER IS SCISSOR");
System.out.println("YOU WIN!!!");
userresult++;
}
else if (comprand == 1)
{
System.out.println("COMPUTER IS ROCK");
System.out.println("DRAW!!!");
i--;
}
else
{
System.out.println("COMPUTER IS PAPER");
System.out.println("COMPUTER WINS!!!");
compresult++;
}
}
}
if(compresult == userresult)
System.out.println("\n\nFINAL RESULT IS DRAW!!!");
else if (compresult > userresult)
System.out.println("\n\nFINAL RESULT IS COMPUTER WIN!!!");
else
System.out.println("\n\nFINAL RESULT IS YOU WIN!!!");
}
}
use break; statement when you want to get out of current loop.
At top of loop,
store the wins in an array
String[] results=new String[rounds];
store your results as "user" or "comp" for each round and at the end of loop do this
if((results[i].equals("user") && results[i-1].equals("user") && results[i-2].equals("user") || (results[i].equals("comp") && results[i-1].equals("comp") && results[i-2].equals("comp")))
{
break;
}
Like shreyas said, use a break statement when you want to exit a loop.
I would set compresult to zero whenever the player wins a round, and set userresult to zero whenever the computer wins. Then, at the top of the loop, right after the { add:
if(userResult == 3 || computerResult == 3 || round/2 > 10)
{
break;
}
Some code comes from shreyas's original post.

Please, how do I get the code to terminate after two computer or user wins

Please, how do I get my code to terminate after two wins by either the computer or the user? I am asked to write a program that plays the popular scissor-rock-paper game. (A scissor can cut a paper, a rock can knock a scissor, and a paper can wrap a rock.) The program randomly generates a number 0, 1, or 2 representing scissor, rock, and paper. The program prompts the user to enter a number 0, 1, or 2 and displays a message indicating whether the user or the computer wins, loses, or draws. The user is expected to continuously play until either the user or the computer wins more than two times. Unfortunately, the code game doesn't end after more than two wins.
Thank you.
package scissors;
import java.util.Scanner;
public class Scissors {
public static void main(String[] args) {
//generate random number
int count=0;
int computerWin=0;
int youWin= 0;
while (computerWin <2 || youWin<2){
int computer = (int)(Math.random()*3);
// prompt user for input
Scanner s = new Scanner(System.in);
System.out.println("Enter a number; scissor(0),rock(1), paper(2): ");
int you = s.nextInt();
switch(computer){
case 0:
if(you==0){
System.out.println("The computer is scissor and you are scissor. It's a Draw");
}
else if (you==1){
System.out.println("The computer is scissor and you are rock. You won");
youWin++;
}
else if (you==2){
System.out.println("The computer is scissor and you are paper. You lost");
computerWin++;
}
break;
case 1:
if(you==0){
System.out.println("The computer is rock and you are scissor. You lost");
computerWin++;
}
else if (you==1){
System.out.println("The computer is rock and you are rock. It's a Draw");
}
else if(you==2){
System.out.println("The computer is rock and you are paper. You won");
youWin++;
}
break;
case 2:
if (you==0){
System.out.println("The computer is paper and you are scissor. You won");
youWin++;
}
else if (you==1){
System.out.println("The computer is papare and you are rock. You lost");
computerWin++;
}
else if (you==2){
System.out.println("The computer is paper and you are paper. It's a draw");
}
break;
}
if (computerWin>youWin)
System.out.println("Computer wins");
else
System.out.println("You win");
}
}
}
Right now, the player and computer share a counter. If the player wins, the counter goes up. If the computer wins, it goes down. This does not track how many wins each one has.
You need a variable to count how many wins a player has for both the user and the computer.
int userPoints = 0, compPoints = 0;
while(userPoints < 2 || compPoints < 2) {
//..
}
if(compPoints > userPoints) {
//computer won
} else {
//user won
}
When the computer wins, add one to compPoints. When the user wins, add one to userPoints
The bug in your code is the while statement:
while (count<=2 || count<=-2)
In this case it runs anytime that count <= 2 because the second condition says count <= -2 instead of >= -2, but it looks like you want it to run only if count is between 2 and negative 2. Also, you probably don't want it to be inclusive (<=) since you want it to break when one person wins 2 more than the other person. So, you want your loop to definition to look like:
while (count<2 || count>-2)
Just change to while (computerWin <2 && youWin<2){.
Your code should iterate while computer and you have less then 2 wins (not or). When any of players have 2 or more wins iteration should be broken.

Rock Paper Scissors Game Using AI Java

For My Assignment, I am supposed to create a Rock, Paper, Scissors game using java. However, there is an added twist. The computer should select the weapon most likely to beat the user, based on the user’s previous choice of weapons. For instance, if the user has selected Paper 3 times but Rock and Scissors only 1 time each, the computer should choose Scissors as the weapon most likely to beat Paper, which is the user’s most frequent choice so far. Here is what I've got so far:
import java.util.Random;
import java.util.Scanner;
public class CSCD210HW3
{
public static void main(String[] args)
{
displayGreeting();
computerChoice();
gameCode();
}
public static void displayGreeting()
{
System.out.print("This is the classic Rock, Paper, Scissors game everyone has grown to know and love. The \nrules are the same. Paper beats rock, rock beats scissors, scissors beats paper. Good luck fool!");
System.out.println();
}
public static String computerChoice()
{
Random randomGenrator = new Random();
int randomNumber = randomGenrator.nextInt(3);
int cpuRock = 0;
int cpuPaper = 0;
int cpuScissors = 0;
String weapon = "nothing";
switch(randomNumber)
{
case 0:
weapon = "rock";
cpuRock++;
break;
case 1:
weapon = "paper";
cpuPaper++;
break;
case 2:
weapon = "scissors";
cpuScissors++;
break;
}
return weapon;
}
public static String playerChoice()
{
Scanner kb = new Scanner(System.in);
String input = "";
System.out.println();
System.out.print("Please Choose Your Weapon: ");
input = kb.next();
String inputLower = input.toLowerCase();
return inputLower;
}
public static void gameCode()
{
int ties = 0;
int playerWins = 0;
int compWins = 0;
int userScissors = 0;
int userRock = 0;
int userPaper = 0;
String player;
String comp;
do
{
player = playerChoice();
if(player == "scissors")
{
userScissors++;
}
else if(player == "rock")
{
userRock++;
}
else if(player == "paper")
{
userPaper++;
}
comp = computerChoice();
if(player.equals("rock")&&comp.equals("rock"))
{
System.out.println("You and the Computer Both Chose Rock. It's a Tie!");
ties++;
userRock++;
}
else if(player.equals("paper")&&comp.equals("paper"))
{
System.out.println("You and the Computer Both Chose Paper. It's a Tie!");
ties++;
userPaper++;
}
else if(player.equals("scissors")&&comp.equals("scissors"))
{
System.out.println("You and the Computer Both Chose Scissors. It's a Tie!");
ties++;
userScissors++;
}
else if (player.equals("rock") && comp.equals("scissors"))
{
System.out.println("You Chose Rock and the Computer Chose Scissors. You Win!");
playerWins++;
userRock++;
}
else if(comp.equals("rock") && player.equals("scissors"))
{
System.out.println("You Chose Scissors and the Computer Chose Rock. You Lose!");
compWins++;
userScissors++;
}
else if(player.equals("scissors")&& comp.equals("paper"))
{
System.out.println("You Chose Scissors and the Computer Chose Paper. You Win!");
playerWins ++;
userScissors++;
}
else if(comp.equals("scissors") && player.equals("paper"))
{
System.out.println("You Chose Paper and the Computer Chose Scissors. You Lose!");
compWins++;
userPaper++;
}
else if(player.equals("paper") && comp.equals("rock"))
{
System.out.println("You Chose Paper and the Computer Chose Rock. You Win!");
playerWins++;
userPaper++;
}
else if(comp.equals("paper")&& player.equals("rock"))
{
System.out.println("You Chose Paper and the Computer Chose Rock. You Lose!");
compWins++;
userRock++;
}
else
{
System.out.println("Invalid Input. Please Re-Enter. ");
System.out.println();
}
}while(!(player.equals("quit")));
System.out.println("Here are the results: ");
System.out.println("Ties: " + ties);
System.out.println("Computer Wins: " + compWins);
System.out.println("Player Wins: " + playerWins);
System.out.println();
System.out.println("Times Rock Chosen: "+userRock);
System.out.println("Times Paper Chosen: "+userPaper);
System.out.println("Times Scissors Chosen: "+userScissors);
return;
}//end
}
I've got no idea how to make the computer select the weapon most likely to beat the user. I've heard an AI might work, but I've never used one before. How would I go about doing that?
There is no way a computer may succeed better at guessing than a human if computer's opponent chooses rock, paper or scissors at random. However, as a human rarely does anything completely at random, there may be approaches to weigh a likeliness of an outcome given previous outcomes. So I think you could go with pattern recognition. For example, for each combination or rock, paper or scissors of length n (so there would be 3^n of those), you could remember how often has it appeared in the sequence produced by human player. So on each turn you remember n-1 previous turns, and after each turn you increment the counter (one of 3^n counters) associated with a combination of outcomes in the last n turns. You can easily see that the time and space required to solve the problem grow exponentially with n, so I suggest choosing a small n, like 4 or 5. So start off with your program guessing at random (33.3% chance for choosing each option), and then, after certain amount of statistics has been collected by playing against a human, start biasing each of three possible outcomes by consulting your counters.

Categories