Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

rps java

// Fun rock paper scissors game in Java

import java.util.*;

public class Game 
{
    
    public static final String ROCK = "ROCK";
    public static final String PAPER = "PAPER";
    public static final String SCISSORS = "SCISSORS";

    public static void main(String args[]) 
    {
      System.out.println("Enter any one of the following inputs:  ");
      System.out.println("ROCK");
      System.out.println("PAPER");
      System.out.println("SCISSORS");
      System.out.println();
          
      String playerMove = getPlayerMove();
      String computerMove = getComputerMove(); 
 
      //Rules of the Game Applied Below:
      /*if both playerMove and computerMove
        produces the same formation, then 
        Game is a tie*/
      if (playerMove.equals(computerMove))
            System.out.println("Game is Tie !!");
      // if playerMove is ROCK         
      else if (playerMove.equals(Game.ROCK))
        System.out.println(computerMove.equals(Game.PAPER) ? "Computer Wins": "Player wins");   
      // if playerMove is PAPER
      else if (playerMove.equals(Game.PAPER))
        System.out.println(computerMove.equals(Game.SCISSORS) ? "Computer Wins": "Player wins");   
      // if playerMove is SCISSORS    
      else
        System.out.println(computerMove.equals(Game.ROCK) ? "Computer Wins": "Player wins");   
    }
    
    /* Get Computer's move using Random 
       class nextInt() method */   
    public static String getComputerMove()
    {
        String computermove;
        Random random = new Random();
        int input = random.nextInt(3)+1;
        if (input == 1)
            computermove = Game.ROCK;
        else if(input == 2)
            computermove = Game.PAPER;
        else
            computermove = Game.SCISSORS;
            
        System.out.println("Computer move is: " + computermove);
        System.out.println();
        return computermove;    
    }
    
    /* Get Player's move using Scanner
       class */
    public static String getPlayerMove()
    {
        Scanner in = new Scanner(System.in);
        String input = in.next();
        String playermove = input.toUpperCase();
        System.out.println("Player move is: "+ playermove);
        return playermove;
    }    
}
Comment

PREVIOUS NEXT
Code Example
Java :: java 11 download 64 bit 
Java :: turn ascii into text javascriot 
Java :: java create image 
Java :: write json file java 
Java :: conditions in for loop java 
Java :: data java 
Java :: @notblank not working in spring boot 
Java :: how to find the largest number in a 2d array java 
Java :: copy arraylist java 
Java :: java for each 
Java :: java get variable from another class 
Java :: android recyclerview show hide item with animation with default animation 
Java :: How to do press enter to continue in java 
Java :: len of number in java 
Java :: java selenium send keys integer 
Java :: java regex replace all characters before 
Java :: min priority queue in java 
Java :: 2d arraylist in java 
Java :: how to call intent in adapter class in android 
Java :: change number into array in java 
Java :: toCharArray() method java 
Java :: java restart program 
Java :: java array to collection 
Java :: range in java 
Java :: java get date in utc 
Java :: thread 
Java :: decode base64 to file java 
Java :: java exception message 
Java :: all possible valid IP addresses leetcode 
Java :: operador ternario java 
ADD CONTENT
Topic
Content
Source link
Name
5+8 =