Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Unit3;
- import java.util.Scanner;
- public class Rock {
- public static void main(String[] args) {
- //declarations
- int cpuRand;
- String cpuPlay="", userPlay;
- Scanner s=new Scanner(System.in);
- boolean playerWin=false;
- //inputs
- cpuRand=(int)(Math.random()*3)+1;//gen 1 - 3
- System.out.println("Please enter your play: R, P, S");
- userPlay=s.next();
- //computations
- //assign cpuPlay
- switch(cpuRand){
- case 1:
- cpuPlay="R";
- break;
- case 2:
- cpuPlay="P";
- break;
- case 3:
- cpuPlay="S";
- break;
- default:
- cpuPlay="R";
- }
- //resolve round
- if(userPlay.equals("R")){
- if(cpuPlay.equals("S")){
- playerWin=true;
- }
- }else if(userPlay.equals("P")){
- if(cpuPlay.equals("R")){
- playerWin=true;
- }
- }else{//userPlay == "S"
- if(cpuPlay.equals("P")){
- playerWin=true;
- }
- }
- System.out.printf("You played %s%n", userPlay);
- System.out.printf("The compter played %s%n", cpuPlay);
- if(userPlay.equals(cpuPlay)){
- System.out.println("Tie");
- }else if(playerWin){
- System.out.println("You win!");
- }else{
- System.out.println("You lose!");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment