java code to play stone paper scissors

Solutions on MaxInterview for java code to play stone paper scissors by the best coders in the world

showing results for - "java code to play stone paper scissors"
Henri
06 May 2018
1import java.util.Scanner;
2class Stone_Paper_Scissors {
3  public static void main(String[] args) {
4   Scanner in = new Scanner(System.in);		
5   System.out.println("Welcome to Rock, Paper, Scissors!");
6   while(true) {
7		
8			System.out.print("\nWhat is your move? To make a move ENTER:- \n1) ROCK \n2) PAPER \n3) SCISSORS \n\nTo quit the game, ENTER-> QUIT. ");
9			System.out.print("\n");String myMove = in.nextLine();  
10	                
11			if(myMove.equals("QUIT")) {
12	                	break;
13			}
14
15			if(!myMove.equals("ROCK") && !myMove.equals("PAPER") && !myMove.equals("SCISSORS")) {
16
17				System.out.println("Your move isn't valid!");
18			
19			} 
20			else 
21			{
22
23				int rand = (int)(Math.random()*3);
24				
25				String opponentMove = "";
26				if(rand == 0) {
27					opponentMove = "ROCK";
28				} else if(rand == 1) {
29					opponentMove = "PAPER";
30				} else {
31					opponentMove = "SCISSORS";
32				}
33				System.out.println("Opponent move: " + opponentMove);
34				
35				if(myMove.equals(opponentMove)) {
36	                                System.out.println("It's a tie!");
37				} else if((myMove.equals("ROCK") && opponentMove.equals("SCISSORS")) || (myMove.equals("SCISSORS") && opponentMove.equals("PAPER")) || (myMove.equals("PAPER") && opponentMove.equals("ROCK"))) {
38					System.out.println("You won!");
39				} else {
40					System.out.println("You lost!");
41				}
42
43			}
44
45		}
46                System.out.println("Thanks for playing Rock, Paper, Scissors!");
47
48  }
49}
50 
similar questions
queries leading to this page
java code to play stone paper scissors