Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

java example

// 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

java example

class Anupam
{
  public static void main(String...args)
  {
    System.out.println("HI this is Anupam Guin ");
  }
}
Comment

PREVIOUS NEXT
Code Example
Java :: java get last char of string 
Java :: how to create a stack data structure in java? 
Java :: how to make a string lowercase in java 
Java :: hashmap iteration 
Java :: java isolate the numbers from string 
Java :: fill an array with random numbers between 1 and 100 java 
Java :: action bar message spigot 1.8 
Java :: slice a list java 
Java :: count vowels in java 
Java :: how to send get request in java 
Java :: javafx resizable window 
Java :: java iso 8601 format 
Java :: how to import math lib in java 
Java :: java read keyboard input 
Java :: convert base64 to image png without saving 
Java :: how to add two numbers in java 
Java :: how to add 2 numbers in java 
Java :: java for 
Java :: java stream distinct by key 
Java :: String remove duplicate in java 
Java :: arraylist items into string 
Java :: java spring boot json deserialize date inaccurate 
Java :: check key is pressed java 
Java :: java create file in folder 
Java :: jpa enum as string 
Java :: milliseconds to seconds java 
Java :: android studio alert dialog box 
Java :: Send image file to server useing Retrofit 
Java :: java string static arrat 
Java :: android java listview clean 
ADD CONTENT
Topic
Content
Source link
Name
2+5 =