Ad Code

Creating a Rock, Paper, Scissors Game in Java Tutorial.

Rock, Paper and Scissor  Game in Java 

In this tutorial, we will learn how to write a simple program in Java that implements the game of Rock, Paper, Scissors. This game is a fun way to get started with programming in Java.

To start with, we will define the rules of the game. Rock beats scissors, scissors beats paper, and paper beats rock. We will create a program that prompts the user to enter their choice of rock, paper, or scissors, and then generates a random choice for the computer.

We will use the java.util.Random class to generate the computer's choice randomly. The program will then compare the user's choice with the computer's choice, and display the result of the game.

We will use the Scanner class to take input from the user. We will create a switch statement to compare the user's input with the computer's choice, and display the result of the game accordingly.

This is a simple and easy program, and a great way to get started with programming in Java. With a few lines of code, we can create a fun and interactive game that can be enjoyed by everyone. So, let's get started and write our own Rock, Paper, Scissors game in Java!

Program :-

 import java.util.Scanner;
 import java.util.Random;
 public class rockAndpaper {
    public static void main(String[] args){
    System.out.println("Enter 1 for Rock, enter 2 for Paper, enter 3 for Scissor");
    Scanner N = new Scanner(System.in);
    Random A = new Random();
    int userInput = N.nextInt();
    int computer= A.nextInt(3);
    if(userInput==computer){
        System.out.println("Draw");
    }
    else if(userInput==1 && computer==3 || userInput==2 && computer==1 || userInput==3 && computer==2){
        System.out.println("You Win");
    }
    else{
        System.out.println("Computer win");
        }
    System.out.println("Computer choice "+computer);
    System.out.println("My choice "+userInput);
    }
 }


I hope, It was helpful and useful for you 

ThankYou !

Post a Comment

1 Comments

Ad Code