If myMove is equal to opponentMove, then it is a tie. equals() to check equality between two strings. If the opponent’s move was the same as your move, then it is a tie. Step 4: Use conditionals to figure out if you won, lost, or tied. Hint: Use conditionals to perform the following check: if the random number is 0, then the opponent’s move will be rock if the random number is 1, then the opponent’s move will be paper if the random number is 2, then the opponent’s move will be scissors. Now we need to translate our random number, which can be 0, 1, or 2, into a String opponentMove which is either rock, paper, or scissors. We can then cast this value to an int so that our random numbers only include 0, 1, and 2 – no decimals in between. Hint: We can use Math.random() * 3 to generate a random number in between 0 and 3, including 0 and excluding 3. But, since there are 3 possible moves in rock, paper, scissors, we need 3 possible random numbers! How can we do this? Math.random() only generates a decimal in between 0 and 1, including 0 and excluding 1. In order to randomly generate the opponent’s move, we can use Math.random(). Step 3: Randomly generate the opponent’s move. If it isn’t equal to rock, paper, or scissors, then we can print out a message to the user stating that their move wasn’t valid! Use a conditional to check whether myMove is equal to rock, paper, or scissors. Step 2: Check if the user entered a valid move. We store their input in a String called myMove.Next, we print out a message asking the user to type in rock, paper, or scissors using ().First, we import the Scanner class to help us get input from the user, by adding import to the top of our program.Step 1: Ask the user to enter in their move. Use a loop to continue asking the user for their move.Use conditionals to figure out if you won, lost, or tied.Check if the user entered a valid move.If you type in something random, then the program prints “Your move isn’t valid!”.The program keeps asking the user to input their move after each round until they type in quit, which is when the program ends.The opponent’s move is chosen randomly and the program prints out whether you won, tied, or lost.The user inputs their move by typing either rock, paper, or scissors.
You can also view my project solution code if you get stuck.
Some other projects you can try first for more practice with beginner Java are our Java tic tac toe game and code a Maze Runner in Java tutorials.įor learning outcomes, you'll get a lot of practice with conditionals, loops, and random numbers. You should review variables, input and output, and conditionals beforehand to get the most out of this project. This beginner Java tutorial is for students that want a easy challenge project, about 50~75 lines long. This project falls under our Juni Java Level 1 coding class for kids. This project info and learning outcomes summary will help you decide if this project is right for you.