// ****************************************************************
// Rock.java
//
// Play Rock, Paper, Scissors with the user
//
// ****************************************************************
import java.util.Scanner;
import java.util.Random;
public class Rock
{
public static void main(String[] args)
{
String personPlay; //User's play -- "R", "P", or "S"
String computerPlay; //Computer's play -- "R", "P", or "S"
int computerInt; //Randomly generated number used to determine
//computer's play
Scanner scan = new Scanner(System.in);
Random generator = new Random();
System.out.println("Please enter you play R, P, or S!");
personPlay = scan.nextLine();
//Get player's play -- note that this is stored as a string
//Make player's play uppercase for ease of comparison
//Generate computer's play (0,1,2)
//Translate computer's randomly generated play to string
personPlay = personPlay.toUpperCase();
computerInt = generator.nextInt(3);
switch (computerInt){
case 0:
computerPlay = "R";
System.out.println ("Computer has chosen ROCK.");
break;
case 1:
computerPlay = "P";
System.out.println ("Computer has chosen PAPER.");
break;
case 2:
computerPlay = "S";
System.out.println ("Computer has chosen SCISSORS.");
break;
}
//Determine who wins
if (personPlay.equals(computerPlay));
System.out.println ("It's a tie!");
if (personPlay.equals("R"))
if (computerPlay.equals("S"))
System.out.println ("Rock crushes scissors. You win!");
else if (computerPlay.equals("P"));
System.out.println ("Paper beats rock. Computer wins!");
if (personPlay.equals("P"))
if (computerPlay.equals("R"))
System.out.println ("Paper beats rock. You win!");
else if (computerPlay.equals("S"))
System.out.println ("Scissors cut paper. Computer wins!");
else if (personPlay.equals("S"))
if (computerPlay.equals("R"))
System.out.println ("Rock crushes scissors. Computer wins!");
else if (computerPlay.equals("P"))
System.out.println ("Scissors cut paper. You win!");
}
}
This post has been edited by macosxnerd101: 18 October 2011 - 01:11 PM
Reason for edit:: Please use code tags

New Topic/Question
Reply



MultiQuote






|