how to make rock paper scissors in java code example

Example 1: rock paper scissors java

import java.util.Random;
import java.util.Scanner;

public class RockPaperScissors {
    private static boolean playAgain(Scanner scanner) {
        System.out.println("Play again? Y(8), N(9)?");
        switch (scanner.nextInt()) {
        case 8:
            System.out.println("Rock, Paper, Scissors!");
            return true;
        default:
            System.out.println("Thanks for playing!");
            return false;
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        RPSPlayer computer = new RandomComputerPlayer(new Random());
        RPSPlayer human = new HumanPlayer(scanner);

        System.out.println("Rock Paper Scissors, by 200_success!");
        do {
            String comp = computer.play();
            String you = human.play();

            System.out.printf("%s vs. %s", comp, you);
            if (you.equals(comp)) {
                System.out.println(", IT'S A TIE!");
            } else if ( ("Rock".equals(you) && "Scissors".equals(comp)) ||
                        ("Scissors".equals(you) && "Paper".equals(comp)) ||
                        ("Paper".equals(you) && "Rock".equals(comp)) ) {
                System.out.println("! You win!");
            } else {
                assert (("Rock".equals(comp) && "Scissors".equals(you)) ||
                        ("Scissors".equals(comp) && "Paper".equals(you)) ||
                        ("Paper".equals(comp) && "Rock".equals(you)));
                System.out.println("! You lose!");
            }
        } while (playAgain(scanner));
    }
}

Example 2: rock paper scissors java

import java.util.Scanner;

public class rps2 {
    public void rps2(){
        Scanner sage = new Scanner(System.in);
        int b;
        b = 1;
        System.out.println("Play again? Y(8), N(9)?");
        int yes= 8, no = 9;
        int input;
        input = sage.nextInt();

            if(input == yes){
                System.out.println("Rock,Paper,Scissors!");

            }else{
                System.out.println("Thanks for playing!");
            }



    }
}

Example 3: rock paper scissors java

import java.util.Scanner;
import java.util.Random;

public class rps {
    public static void main (String args[]){
        int input;
        int b = 1;
        Scanner sage = new Scanner(System.in);
        Random rnd = new Random();
    System.out.println("Rock Paper Scissors, by Sage!");
    System.out.println("Select 1, 2, 3, for Rock, Paper, Scissors");
    //Menu Present, pretty bad still

        while (b != 0){
        int rock = 1, paper = 2, scissors = 3;
        input = sage.nextInt();
        int randomNumber = rnd.nextInt(3-1+1)+1;

            if(randomNumber == rock){
                if(input == rock){
                    System.out.println("Rock vs. Rock, ITS A TIE!");
            }    else if(input == paper){
                    System.out.println("Rock vs. Paper! You win!" );
            }    else if(input == scissors){
                    System.out.println("Rock vs. Scissors! You lose!");
            }  //These blocks establish options if the computer got Rock

            else if(randomNumber == paper){
                if(input == rock){
                    System.out.println("Paper vs. Rock! You lose!");
            }   else if(input == scissors){
                    System.out.println("Paper vs. Scissors! You win!");
            }   else if(input == paper){
                    System.out.println("Paper vs. Paper! Its a tie!");
            } //These blocks establish the options if comp. got paper

            else if(randomNumber == scissors){
                if(input == rock){
                    System.out.println("Scissors vs. Rock! You win!");
            }   else if(input == scissors){
                    System.out.println("Scissors vs. Scissors, ITS A TIE!");
            }   else if(input == paper){
                    System.out.println("Scissors vs Paper! You lose!");
            } //These blocks establish if the computer got scissors.

            }
            }
            rps2 rps2Object = new rps2();
            rps2Object.rps2();


        }

    }
    }
}

Tags:

Java Example