#java #javatutorial #javacourse
This is a coding project for beginners to help you learn how to code. We will use the following previous topics: arrays, loops, logical operators, random numbers, and string methods.
import java.util.Random;
import java.util.Scanner;
public class Main {
public static void main(String[] args){
// ROCK PAPER SCISSORS GAME
Scanner scanner = new Scanner(System.in);
Random random = new Random();
String[] choices = {"rock", "paper", "scissors"};
String playerChoice;
String computerChoice;
String playAgain = "yes";
do{
System.out.print("Enter your move (rock, paper, scissors): ");
playerChoice = scanner.nextLine().toLowerCase();
if(!playerChoice.equals("rock") && !playerChoice.equals("paper") && !playerChoice.equals("scissors")){
System.out.println("Invalid choice");
continue;
}
computerChoice = choices[random.nextInt(3)];
System.out.println("Computer choice: " + computerChoice);
if(playerChoice.equals(computerChoice)){
System.out.println("It's a tie!");
}
else if((playerChoice.equals("rock") && computerChoice.equals("scissors")) ||
(playerChoice.equals("paper") && computerChoice.equals("rock")) ||
(playerChoice.equals("scissors") && computerChoice.equals("paper"))){
System.out.println("You win!");
}
else{
System.out.println("You lose!");
}
System.out.print("Play again (yes/no): ");
playAgain = scanner.nextLine().toLowerCase();
}while(playAgain.equals("yes"));
System.out.println("Thanks for playing!");
scanner.close();
}
}
On this page of the site you can watch the video online Code a rock paper scissors game with Java! 🗿 with a duration of hours minute second in good quality, which was uploaded by the user Bro Code 05 December 2024, share the link with friends and acquaintances, this video has already been watched 15,756 times on youtube and it was liked by 356 viewers. Enjoy your viewing!