The code is below. My program runs smoothly except for two issues. It does not play for just three rounds and I need it to play for just 3 rounds and determine a winner based off of whoever won at least once during the three rounds despite ties (if it comes down to that scenario randomly). It plays until there is a 2 to 1 winning ratio for either the player or the computer. I do not want it to run continuously like this. The other issue is that once a final winner is declared, the line “Enter rock, paper, or scissors” appears again, and it is not needed there. How do I go about fixing these two issues? Thank you.
import java.util.Scanner;
import java.util.Random;
/**
*
* #author Chloe Harris
*
*/
public class RockPaperScissorsGame {
public static void main(String[] args) {
// TODO code application logic here
//Prompt user to enter rock, paper, or scissors
//Compare random value
while(true){
int wins = 0;
int losses = 0;
int rnd = 0;
int USER = 0;
System.out.print("Welcome to Rock Paper Scissors! Best 2 out of 3! \n"
+ "Enter \"Rock\", \"Paper\" or \"Scissors\" \n");
// Plays 3 rounds before terminating
while(rnd<3) {
Random GAME = new Random();
int PC = 1+GAME.nextInt(3);
Scanner keyboard = new Scanner (System.in);
int SCISSOR, ROCK, PAPER;
ROCK = 1;
PAPER = 2;
SCISSOR= 3;
String USER_Input = keyboard.next();
if(USER_Input.equals("Rock")) {
USER = 1;
}
if(USER_Input.equals("Paper")) {
USER = 2;
}
if(USER_Input.equals("Scissors")) {
USER = 3;
}
//If the user enters a value greater then 3 or less than 1 it will terminate the program
//and display an error message
while (USER > 3 || USER < 1) {
System.err.println("Incorrect value entered.");
System.exit(0);
break;
}
if(USER == PC){
if(USER == SCISSOR){
System.out.println("Scissors v Scissors! Tie!");
}
if(USER == ROCK){
System.out.println("Rock v Rock! Tie!");
}
if(USER == PAPER){
System.out.println("Paper v Paper! Tie!");
}
System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
System.out.print("Enter \"Rock\", \"Paper\" or \"Scissors\" \n");
}
//Player wins
if(USER == SCISSOR)
if(PC == PAPER){
System.out.println("Scissors v Paper! Player Wins!");
wins++;
rnd++;
System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
System.out.print("Enter \"Rock\", \"Paper\" or \"Scissors\" \n");
}
//Computer wins
else if(PC == ROCK){
System.out.println("Scissors v Rock! Computer Wins!");
losses++;
rnd++;
System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
System.out.print("Enter \"Rock\", \"Paper\" or \"Scissors\" \n");
}
//Player wins
if(USER == ROCK)
if(PC == SCISSOR ){
System.out.println("Rock v Scissor! Player Wins!");
wins++;
rnd++;
System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
System.out.print("Enter \"Rock\", \"Paper\" or \"Scissors\" \n");
}
//Computer wins
else if (PC == PAPER){
System.out.println("Rock v Paper! Computer Wins!");
losses++;
rnd++;
System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
System.out.print("Enter \"Rock\", \"Paper\" or \"Scissors\" \n");
}
//Player Wins
if(USER == PAPER)
if(PC == ROCK){
System.out.println("Paper v Rock! Player Wins!");
wins++;
rnd++;
System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
System.out.print("Enter \"Rock\", \"Paper\" or \"Scissors\" \n");
}
//Computer Wins
else if (PC == SCISSOR){
System.out.println("Paper v Scissors! Computer Wins!");
losses++;
rnd++;
System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
System.out.print("Enter \"Rock\", \"Paper\" or \"Scissors\" \n");
}
}
if(wins>losses){
System.out.println("The Player Wins!");
}if(losses>wins){
System.out.println("The Computer Wins!");
}
System.out.println("Play again? \"Yes\" or \"No\"");
Scanner YN = new Scanner(System.in);
String YN_String = YN.next();
if(YN_String.equals("Yes") || YN_String.equals("yes")){
}if(YN_String.equals("No") || YN_String.equals("no")) {
System.out.println ("Goodbye!");
break;
}
}
}
}
The code looks like it should work the way you want it to, aside from the fact that in the case of ties, you aren't incrementing the round counter:
if(USER == PC){
if(USER == SCISSOR){
System.out.println("Scissors v Scissors! Tie!");
rnd++;
}
if(USER == ROCK){
System.out.println("Rock v Rock! Tie!");
rnd++;
}
if(USER == PAPER){
System.out.println("Paper v Paper! Tie!");
rnd++;
}
System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
System.out.print("Enter \"Rock\", \"Paper\" or \"Scissors\" \n");
}
As a side note, you could take out the rnd++; from each individual result, and have it increase every time you get input from the user, since you want it to increase no matter what.
Your second issue stems from the fact that you're prompting for new input every time you receive input, after you tell the user the result, including the ending result. You could fix this by removing the prompt command from the individual results and placing it at the beginning of your while loop instead. Note that if you do this, you may also want to remove the prompt from your welcome message as well, or it will prompt twice at the beginning: once in the welcome message, and again when the while loop starts immediately after.
import java.util.Scanner;
import java.util.Random;
/**
*
* #author Chloe Harris
*
*/
public class RockPaperScissorsGame {
public static void main(String[] args) {
// TODO code application logic here
//Set integers for wins, losses, rounds, and user
while(true){
int wins = 0;
int losses = 0;
int round = 0;
int Player = 0;
//Plays 3 rounds before terminating
//Prompt user to input Rock Paper Scissors
System.out.print("Welcome to Rock Paper Scissors! Best 2 out of 3! \n");
Scanner keyboard = new Scanner (System.in);
while(round<3) {
System.out.println("Enter \"Rock\", \"Paper\" or \"Scissors\"");
Random Game = new Random();
int Computer = 1+Game.nextInt(3);
int Scissors, Rock, Paper;
Rock = 1;
Paper = 2;
Scissors= 3;
String UserInput = keyboard.next();
if(UserInput.equals("Rock")) {
Player = 1;
}
if(UserInput.equals("Paper")) {
Player = 2;
}
if(UserInput.equals("Scissors")) {
Player = 3;
}
//If the user enters a value greater then 3 (Scissors) or less than 1 (Rock)
//it will terminate the program and display an error message
while (Player > 3 || Player < 1) {
losses++;
round++;
System.out.println("Not a valid input! Computer wins");
System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
}
//Establish tie scenarios using if statements
if(Player == Computer){
if(Player == Scissors){
System.out.println("Scissors v Scissors! Tie!");
round++;
}
if(Player == Rock){
System.out.println("Rock v Rock! Tie!");
round++;
}
if(Player == Paper){
System.out.println("Paper v Paper! Tie!");
round++;
}
System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
}
//Establish the various winning scenarios using if and else if statements
//Player wins
if(Player == Scissors)
if(Computer == Paper){
System.out.println("Scissors v Paper! Player Wins!");
wins++;
round++;
System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
}
//Computer wins
else if(Computer == Rock){
System.out.println("Scissors v Rock! Computer Wins!");
losses++;
round++;
System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
}
//Player wins
if(Player == Rock)
if(Computer == Scissors ){
System.out.println("Rock v Scissors! Player Wins!");
wins++;
round++;
System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
}
//Computer wins
else if (Computer == Paper){
System.out.println("Rock v Paper! Computer Wins!");
losses++;
round++;
System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
}
//Player Wins
if(Player == Paper)
if(Computer == Rock){
System.out.println("Paper v Rock! Player Wins!");
wins++;
round++;
System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
}
//Computer Wins
else if (Computer == Scissors){
System.out.println("Paper v Scissors! Computer Wins!");
losses++;
round++;
System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
}
}
//Determine final winner using if statements
//Ask if player would like to play again by setting up string
if(wins>losses){
System.out.println("The Player Wins!");
}if(losses>wins){
System.out.println("The Computer Wins!");
}
System.out.println("Play again? \"Yes\" or \"No\"");
Scanner YesNo = new Scanner(System.in);
String YesNo_String = YesNo.next();
if(YesNo_String.equalsIgnoreCase("yes")) {
}if(YesNo_String.equalsIgnoreCase("No")) {
System.out.println ("Goodbye!");
}
}
}
}
Related
I'm trying to make a rock-paper-scissors program that is a best two out of three where the computer randomly rolls a 0-2 and each of those are assigned to rock, paper, or scissors, and then it compares the userInput and counts a win for computer or player then adds it up.
BUT, I can't figure out how to make it that if user were to enter "scissors" the program would know that it's also assigned to 2 (For comparison purposes).
public static void main(String[] args) {
Random r = new Random();
int gameCount = 0;
int computerWins = 0;
int playerWins = 0;
int rock = 0;
int paper = 1;
int scissors = 2;
int playerChoice;
int computerChoice = r.nextInt(3);
System.out.println("Welcome to Rock Paper Scissors! Best 2 out of 3!");
while (gameCount >= 0 && gameCount < 3)
{
System.out.println("Enter \"Rock\", \"Paper\", or \"Scissors\"");
break;
}
playerChoice = userInput.nextInt()
//If player enters anything besides rock, paper, or scissors
if (playerChoice < 0 || playerChoice >= 3) {
System.out.println("That wasn't an option");
computerWins++;
gameCount++;
//The game goes on, and the winners are added up!
} else if (playerChoice == 0 && computerChoice == 1) {
computerWins++;
gameCount++;
System.out.println("Rock v Paper! Computer Wins!\n" +
"Player has won " + playerWins + " times and the computer " +
"has won " + computerWins + " times");
} else if (playerChoice == 1 && computerChoice == 0) {
playerWins++;
gameCount++;
System.out.println("Paper v Rock! Player Wins!\n" +
"Player has won " + playerWins + " times and the computer " +
"has won " + computerWins + " times");
} else if (playerChoice == 1 && computerChoice == 2) {
computerWins++;
gameCount++;
System.out.println("Paper v Scissors! Computer Wins!\n" +
"Player has won " + playerWins + " times and the computer " +
"has won " + computerWins + " times");
} else if (playerChoice == 2 && computerChoice == 1) {
playerWins++;
gameCount++;
System.out.println("Scissors v Paper! Player Wins!\n" +
"Player has won " + playerWins + " times and the computer " +
"has won " + computerWins + " times");
} else if (playerChoice == 2 && computerChoice == 0) {
computerWins++;
gameCount++;
System.out.println("Scissors v Rock! Computer Wins!\n" +
"Player has won " + playerWins + " times and the computer " +
"has won " + computerWins + " times");
} else if (playerChoice == 0 && computerChoice == 2) {
playerWins++;
gameCount++;
System.out.println("Rock v Scissors! Player Wins!\n" +
"Player has won " + playerWins + " times and the computer " +
"has won " + computerWins + " times");
} else if (playerChoice == 0 && computerChoice == 0) {
gameCount++;
System.out.println("Rock v Rock! Tie!\n" +
"Player has won " + playerWins + " times and the computer " +
"has won " + computerWins + " times");
} else if (playerChoice == 1 && computerChoice == 1) {
gameCount++;
System.out.println("Paper v Paper! Tie!\n" +
"Player has won " + playerWins + " times and the computer " +
"has won " + computerWins + " times");
} else if (playerChoice == 2 && computerChoice == 2) {
gameCount++;
System.out.println("Paper v Paper! Tie!\n" +
"Player has won " + playerWins + " times and the computer " +
"has won " + computerWins + " times");
}
//Check if game count reaches max games then chooses a winner
if (gameCount == 3 && computerWins > playerWins) {
System.out.println("The Computer Wins!");
} else if (gameCount == 3 && computerWins < playerWins) {
System.out.println("The Player Wins!");
} else if (gameCount == 3 && computerWins == playerWins) {
System.out.println("The game is a tie!");
}
}
}
So instead of playerChoice = userInput.nextInt(); try this:
Scanner sc = new Scanner(System.in);
String input = sc.nextLine();
try {
playerChoice = Integer.parseInt(input);
} catch (NumberFormatException e) {
if (input.equalsIgnoreCase("rock")) {
playerChoice = rock;
} else if (input.equalsIgnoreCase("paper")) {
playerChoice = paper;
} else if (input.equalsIgnoreCase("scissors")) {
playerChoice = scissors;
} else {
// if input is invalid
playerChoice = -1;
}
}
Since you're using userInput.nextInt() and playerChoice is an int and can only hold ints, you need to parse your user's input. In this case, Integer.parseInt(input) will try to find an int in the user input. If it can't it will return an exception; that's why there's a try-catch block. If it isn't an int, it will look for each string and assign the associated int value to playerChoice or -1 if the input is invalid. Then the rest of your code should be able to appropriately handle the playerChoice after that.
This question already has answers here:
Why do we need break after case statements?
(17 answers)
Closed 7 years ago.
first time poster, and newbie coder. I apologize for the mess of this code, still needs cleaning. However, I am having major issues with my switch. If I select case 1, it runs through case 1, then after completion, automatically runs through case 2, then case 3. Likewise, if I select case 2, after completion of case 2, it runs through case 3. Finally, my default isn't working either. When selecting anything other than 1, 2, or 3, all sorts of errors appear. Any help would be greatly appreciated!
Had a hard time searching for this complex issue!
switch(input3)
{
case 1 :
{
JOptionPane.showMessageDialog(null,"You have selected the Rock, Paper, Scissors Game\nThe program is now loading...\nProgram loaded successfully! Please press OK.");
JOptionPane.showMessageDialog(null,"Rock, Paper, Scissors Game... 3 Rounds!");
while(round<3) {
String UserInput = JOptionPane.showInputDialog("Rock, Paper, Scissors Game: Type Rock, Paper, or Scissors");
Random Game = new Random();
int Computer = 1+Game.nextInt(3);
int Scissors, Rock, Paper;
Rock = 1;
Paper = 2;
Scissors= 3;
if(UserInput.equals("Rock")) {
Player = 1;
}
if(UserInput.equals("Paper")) {
Player = 2;
}
if(UserInput.equals("Scissors")) {
Player = 3;
}
int random = (int)(Math.random() * 3);
String computerInput; // Computer's choice
if (random == 0)
computerInput = "rock";
else if (random == 1)
computerInput = "paper";
else
computerInput = "scissor";
while (Player > 3 || Player < 1) {
losses++;
round++;
System.out.println("Not a valid input! Computer wins");
System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
}
//Establish tie scenarios using if statements
if(UserInput== computerInput){
JOptionPane.showMessageDialog(null,"Player has won " + wins + " times and the computer has won " + losses + " times");
round++;
}
//Winning scenerios
if(Player == Scissors)
if(Computer == Paper){
JOptionPane.showMessageDialog(null,"Scissors v Paper! Player Wins!");
wins++;
round++;
JOptionPane.showMessageDialog(null,"Player has won " + wins + " times and the computer has won " + losses + " times");
}
//Computer wins
else if(Computer == Rock){
JOptionPane.showMessageDialog(null,"Scissors v Rock! Computer Wins!");
losses++;
round++;
JOptionPane.showMessageDialog(null,"Player has won " + wins + " times and the computer has won " + losses + " times");
}
//Player wins
if(Player == Rock)
if(Computer == Scissors ){
JOptionPane.showMessageDialog(null,"Rock v Scissors! Player Wins!");
wins++;
round++;
JOptionPane.showMessageDialog(null,"Player has won " + wins + " times and the computer has won " + losses + " times");
}
//Computer wins
else if (Computer == Paper){
JOptionPane.showMessageDialog(null,"Rock v Paper! Computer Wins!");
losses++;
round++;
JOptionPane.showMessageDialog(null,"Player has won " + wins + " times and the computer has won " + losses + " times");
}
//Player Wins
if(Player == Paper)
if(Computer == Rock){
JOptionPane.showMessageDialog(null,"Paper v Rock! Player Wins!");
wins++;
round++;
JOptionPane.showMessageDialog(null,"Player has won " + wins + " times and the computer has won " + losses + " times");
}
//Computer Wins
else if (Computer == Scissors){
JOptionPane.showMessageDialog(null,"Paper v Scissors! Computer Wins!");
losses++;
round++;
JOptionPane.showMessageDialog(null,"Player has won " + wins + " times and the computer has won " + losses + " times");
}
}
if(wins>losses){
JOptionPane.showMessageDialog(null,"The Player Wins!");
}if(losses>wins){
JOptionPane.showMessageDialog(null,"The Computer Wins!");
}
}
case 2 :
{
JOptionPane.showMessageDialog(null,"You have selected the game's rules.\nOpening the rules...\nLoaded successfully! Please press OK.");
JOptionPane.showMessageDialog(null,"Here are the rules for the game:\nIn total, there are 3 rounds for this game, including Ties.\nPaper vs Rock => Paper is the Winner. Add 1 to the winner.\nPaper vs Scissors => Scissors is the Winner. Add 1 to the winner.\nRock vs Scissors => Rock is the Winner. Add 1 to the winner.\nRock vs Rock => Tie. No score.\nScissors vs Scissors => Ties. No score.\nPaper vs Paper => Tie. No score.");
}
case 3 :
{
JOptionPane.showMessageDialog(null,"3");
}
default :
JOptionPane.showMessageDialog(null,"You entered an invalid operation. Please select 1, 2, or 3.");
String input2 = JOptionPane.showInputDialog("Would you like to return to the main menu? Please respond with Yes or No.");
if (input2.equalsIgnoreCase ("No"))
JOptionPane.showMessageDialog(null,"You have selected to exit the program. Closing the program... please press OK.");
{
i=1;
}
}
}
You need to use break at the end of every case. Syntax is like:
switch(variable){
case 1:
//Do your operations.
break;
case 2:
//Do your operations.
break;
.
.
.
}
Let me know if this helps.
PROMPT
Welcome to play the game of guessing who is winning!
Please enter your game 1 board (* to exit) > XXOOOXXOO
Your game 1 is as follows:
XXO
OOX
XOO
Your game 1: Tie
Please enter your game 2 board (* to exit) > XXXOOXXOO
Your game 2 is as follows:
XXX
OOX
XOO
Your game 2: X won the game by row 1.
etc.
How could I make it read a tie game?
package PA6;
import java.util.Scanner;
public class PA6 {
private static char [ ] [ ] ttt = new char [4] [4] ;
private static int gameNum = 1;
public static void main(String[] args)
{//MAIN OPEN
System.out.println("Welcome to play the game of guessing who is winning!");
Scanner scan = new Scanner (System.in);
String gameString;
System.out.println("Please enter your game " + gameNum + " board (* to exit) > ");
gameString = scan.nextLine();
int n;
while (!gameString.equals("*"))
{//WHILE LOOP OPEN
System.out.println("Your game " + gameNum + " is as follows: ");
n = 0;
ttt = new char [4][4];
for(int i = 1 ; i < 4; i++)
{//FOR LOOP OPEN
for(int j = 1; j < 4; j++)
{//NESTED FOR LOOP OPEN
ttt [i][j]= gameString.charAt(n);
n++;
}//NESTED FOR LOOP CLOSED
}//FOR LOOP CLOSED
System.out.println(ttt[1][1] + "" + ttt[1][2] + "" + ttt[1][3]);
System.out.println(ttt[2][1] + "" + ttt[2][2] + "" + ttt[2][3]);
System.out.println(ttt[3][1] + "" + ttt[3][2] + "" + ttt[3][3]);
if(winRow1('O' , ttt))
System.out.println("Your game " + gameNum + ": O won the game by row 1");
if(winRow2('O' , ttt))
System.out.println("Your game " + gameNum + ": O won the game by row 2");
if(winRow3('O' , ttt))
System.out.println("Your game " + gameNum + ": O won the game by row 3");
if(winRow1('X' , ttt))
System.out.println("Your game " + gameNum + ": X won the game by row 1");
if(winRow2('X' , ttt))
System.out.println("Your game " + gameNum + ": X won the game by row 2");
if(winRow3('X' , ttt))
System.out.println("Your game " + gameNum + ": X won the game by row 3");
if(winColumn1('O' , ttt))
System.out.println("Your game " + gameNum + ": O won the game by Column 1");
if(winColumn2('O' , ttt))
System.out.println("Your game " + gameNum + ": O won the game by Column 2");
if(winColumn3('O' , ttt))
System.out.println("Your game " + gameNum + ": O won the game by Column 3");
if(winColumn1('X' , ttt))
System.out.println("Your game " + gameNum + ": X won the game by Column 1");
if(winColumn2('X' , ttt))
System.out.println("Your game " + gameNum + ": X won the game by Column 2");
if(winColumn3('X' , ttt))
System.out.println("Your game " + gameNum + ": X won the game by Column 3");
if(winDiagonal1('O' , ttt))
System.out.println("Your game " + gameNum + ": O won the game by diagonal 1");
if(winDiagonal1('X' , ttt))
System.out.println("Your game " + gameNum + ": X won the game by diagonal 1");
if(winDiagonal2('O' , ttt))
System.out.println("Your game " + gameNum + ": O won the game by diagonal 2");
if(winDiagonal2('X' , ttt))
System.out.println("Your game " + gameNum + ": X won the game by diagonal 2");
gameNum++;
System.out.print("Please enter your game " + gameNum + " board (* to exit) > ");
gameString = scan.nextLine();
}//WHILE LOOP CLOSED
}//MAIN CLOSED
public static boolean winDiagonal1( char player, char a [][])
{
if ( ttt[1][1] == player && ttt[2][2] == player && ttt[3][3] == player )
return true;
return false;
}
public static boolean winDiagonal2 (char player, char a [][])
{
if( ttt[1][3] == player && ttt[2][2] == player && ttt[3][1] == player )
return true;
return false;
}
public static boolean winRow1 (char player, char a [][])
{
if (ttt[1][1] == player && ttt[1][2] == player && ttt[1][3] == player)
return true;
return false;
}
public static boolean winRow2 (char player, char a [][])
{
if (ttt[2][1] == player && ttt[2][2] == player && ttt[2][3] == player)
return true;
return false;
}
public static boolean winRow3 (char player, char a [][])
{
if (ttt[3][1] == player && ttt[3][2] == player && ttt[3][3] == player)
return true;
return false;
}
public static boolean winColumn1 (char player, char a [][])
{
if (ttt[1][1] == player && ttt[2][1] == player && ttt [3][1] == player)
return true;
return false;
}
public static boolean winColumn2 (char player, char a [][])
{
if (ttt[1][2] == player && ttt[2][2] == player && ttt [3][2] == player)
return true;
return false;
}
public static boolean winColumn3 (char player, char a [][])
{
if (ttt[1][3] == player && ttt[2][3] == player && ttt [3][3] == player)
return true;
return false;
}
}
I you convert your if statements to combined if else if statements than what is left in your else case will be the tie.
if(winRow1('O' , ttt))
System.out.println("Your game " + gameNum + ": O won the game by row 1");
else if(winRow2('O' , ttt))
System.out.println("Your game " + gameNum + ": O won the game by row 2");
.
.
.
else
System.out.println("Your game " + gameNum + ": It is a tie");
By the way,this way you could avoid multiple messages like when there are one row and one column fulticked at the same time ;).
i had a quick question, so i finished up writing my program which is a simple Rock, Paper Scissors game but at the moment it only displays 0,1,2 which 0 is scissors, 1 is rock, and 2 is paper. The problem im having is that im not quite sure how to give the users input as well as what the computer outputs go from displaying numbers to displaying scissors, rock, paper.
import java.util.Scanner;
import java.lang.Math;
public class Lab3
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Hello user! lets play ");
System.out.println("Rock, Paper, Scissors.");
System.out.println();
System.out.print("Type in 0 for Scissors, 1 for Rock, or 2 for Paper ");
int userInput = in.nextInt();
int opponentsHand = (int)(Math.random()*3);
if (userInput == opponentsHand)
{
System.out.print("Darth Vader has played " + opponentsHand);
System.out.println(" Despite your efforts of playing " + userInput + ", this battle has ended in a draw!");
}
if (userInput < opponentsHand && opponentsHand != 2)
{
System.out.print("Darth vader has played "+ opponentsHand);
System.out.println(", You played " + userInput + "You have Lost");
}
else if (userInput < opponentsHand && opponentsHand == 2)
{
System.out.print("Darth Vader has played " + opponentsHand);
System.out.println(" You played " + userInput + " You have won");
}
if (userInput > opponentsHand && opponentsHand != 0)
{
System.out.print("Darth Vader has played " + opponentsHand);
System.out.println(" You have played " + userInput + " You have won");
}
else if (userInput > opponentsHand && opponentsHand == 0)
{
System.out.print("Darth Vader has played " + opponentsHand);
System.out.println(" You have played " + userInput + " You have lost");
}
}
}
thank you
One way is to simply use an array to store the names of the hands:
String[] hands = {"scissors", "rock", "paper"};
When you print, you do the following:
System.out.print("Darth Vader has played " + hands[opponentsHand]);
I just started learning Java in a class. The assignment was to make a Rock, Paper and Scissors game, here is my source code. I turned this in already but have not received a grade.
package rockpaperscissors;
import java.util.Random;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// Create scanner input
Scanner input = new Scanner (System.in);
// Introduce user to game
System.out.println("Welcome to the Rock, Paper and Scissors game");
// Ask the user for their move
System.out.println("\nPlease select a move\nEnter 0 for Scissors, 1 "
+ "for Rock and 2 for Paper");
int userMove = input.nextInt();
// Ensure that userMove is between 0 and 2
if (userMove > 2 || userMove < 0) {
System.out.println("Invalid entry the result of this game will not "
+ "be accurate! Please retry using either 0 for Scissors, 1"
+ " for Rock and 2 for Paper\n");
}
// Generate computerMove using java.util.Random
// Create instance first
Random MyRandom = new Random();
//Now generate number
int computerMove = MyRandom.nextInt(3);
System.out.print("The computer played " + computerMove + "\nRemember"
+ " 0 stands for Scissors, 1 is for Rock and 2 is for paper\n");
// Determine draw result
if (userMove == computerMove) {
System.out.println("\nYou played " + userMove + ", The computer "
+ "played " + computerMove + " The game is a draw!");
}
// Determine computer win
else if(userMove == 0 && computerMove == 1) {
System.out.println("\nYou played " + userMove + ", The computer "
+ "played " + computerMove + " The Computer Wins!");
}
else if(userMove == 2 && computerMove == 0) {
System.out.println("\nYou played " + userMove + ", The computer "
+ "played " + computerMove + " The Computer Wins!");
}
else if(userMove == 1 && computerMove == 2) {
System.out.println("\nYou played " + userMove + ", The computer "
+ "played " + computerMove + " The Computer Wins!");
}
/* Determine User win, as no other moves can result in computer win all
other combinations will result in user win
*/
else {
System.out.print("\nYou played " + userMove + ", The computer "
+ "played " + computerMove + " You Win!");
}
}
}
I am having trouble on getting the program to re-ask for a number if the number is above 2 or below 0, it will say "Invalid entry the result will not be valid..." but it will still preform the rest of the program with an invalid number.
How can I make the program stop and prompt the user for a valid number and then preform the program? Additionally, this is the third program I have ever made so any other advice would be appreciated.
Thanks
What you want is to not do all the other code if your conditions are met.
Right now you are giving an error message when your conditions are met - but not stopping execution of the rest of the function.
you can just stop.
So - without telling you the exact answer... how do you exit from a function?
Do that just after the error message gets displayed and you will be fine.
However - if you want to continue fetching the user input until you meet the conditions... that requires a loop around the part that fetches and checks.
what loops do you know?
do you know one that lets you loop until you've met some condition?
you've got your condition already... now you just need to adapt a loop to let you do the code that you want - repeatedly - until that condition is met.
in pseudo code (ie this is not real java - you must translate):
some_loop do
fetch user input
give error message if its not ok
until (user input is ok)