Course Hive
Search

Welcome

Sign in or create your account

Continue with Google
or
Code a rock paper scissors game with Java! 🗿
Play lesson

Java tutorial for beginners ☕ - Code a rock paper scissors game with Java! 🗿

5.0 (4)
40 learners

What you'll learn

This course includes

  • 12 hours of video
  • Certificate of completion
  • Access on mobile and TV

Summary

Keywords

Full Transcript

#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(); } }

Course Hive

Continue this lesson in the app

Install CourseHive on Android or iOS to keep learning while you move.

Related Courses

FAQs

Course Hive
Download CourseHive
Keep learning anywhere