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 debug log 
Java :: java array to arraylist 
Java :: java find longest string in list 
Java :: how to write a json file using java 
Java :: Fibonacci Series Program. in java 
Java :: java string to double 
Java :: java int to string 
Java :: has integer java 
Java :: How to perform a breadth first search through a binary tree, in Java? 
Java :: android use attribute color programmatically 
Java :: how to create an array in java 
Java :: get random string from array list 
Java :: how to catch enter key in java text field 
Java :: sorting the characters of a string in java 
Java :: java selenium send keys numbers 
Java :: inline arraylist initialization java 
Java :: java find last element in array 
Java :: putting scanner into an array 
Java :: kotlin variable in string 
Java :: how to convert int into int array of digits in java 
Java :: support different screen sizes android 
Java :: java arithmetic operators 
Java :: how to read a excel file in java 
Java :: Java, how to compare Strings with String Arrays 
Java :: difference between access specifiers and access modifiers in java 
Java :: instantiate optinal java 8 
Java :: max of an array java 
Java :: convert arraylist to csv file java 
Java :: java add to map 
Java :: java catch multiple exceptions 
ADD CONTENT
Topic
Content
Source link
Name
9+9 =