Answers for "java code to play stone paper scissors"

0

java code to play stone paper scissors

import java.util.Scanner;
class Stone_Paper_Scissors {
  public static void main(String[] args) {
   Scanner in = new Scanner(System.in);		
   System.out.println("Welcome to Rock, Paper, Scissors!");
   while(true) {
		
			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. ");
			System.out.print("\n");String myMove = in.nextLine();  
	                
			if(myMove.equals("QUIT")) {
	                	break;
			}

			if(!myMove.equals("ROCK") && !myMove.equals("PAPER") && !myMove.equals("SCISSORS")) {

				System.out.println("Your move isn't valid!");
			
			} 
			else 
			{

				int rand = (int)(Math.random()*3);
				
				String opponentMove = "";
				if(rand == 0) {
					opponentMove = "ROCK";
				} else if(rand == 1) {
					opponentMove = "PAPER";
				} else {
					opponentMove = "SCISSORS";
				}
				System.out.println("Opponent move: " + opponentMove);
				
				if(myMove.equals(opponentMove)) {
	                                System.out.println("It's a tie!");
				} else if((myMove.equals("ROCK") && opponentMove.equals("SCISSORS")) || (myMove.equals("SCISSORS") && opponentMove.equals("PAPER")) || (myMove.equals("PAPER") && opponentMove.equals("ROCK"))) {
					System.out.println("You won!");
				} else {
					System.out.println("You lost!");
				}

			}

		}
                System.out.println("Thanks for playing Rock, Paper, Scissors!");

  }
}
Posted by: Guest on April-21-2021

Code answers related to "java code to play stone paper scissors"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language