BuezaWebDev
10-08-2004, 12:26 AM
Hey guys, I'm having a hard time with my last few nested if statements at the end.
Most of all though, I'm getting sooooo confused with all the nested stuff. :|
// ****************************************************************
// Rock.java
//
// Play Rock, Paper, Scissors with the user
//
// ****************************************************************
import cs1.Keyboard;
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
Random generator = new Random();
//Get player's play -- note that this is stored as a string
System.out.println ("Play ROCK PAPER SCISSORS!!!");
System.out.println ("R = Rock");
System.out.println ("P = Paper");
System.out.println ("S = Scissors");
System.out.println ("Choose your weapon");
personPlay = Keyboard.readString();
//Make player's play uppercase for ease of comparison
personPlay = personPlay.toUpperCase();
System.out.println ("You chose... "+personPlay+" as your weapon.");
//Generate computer's play (0,1,2)
computerInt = generator.nextInt(3);
//Translate computer's randomly generated play to string
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;
default:
System.out.println ("Please re-enter a value between 0-2");
}
//Print computer's play
System.out.println ("The computer has chosen "+computerPlay+"--Prepare to do battle!");
//See who won
if (personPlay.equals(computerPlay)) {
System.out.println("It's a tie!");
} else {
if (personPlay.equals("R")) {
if (computerPlay.equals("S")) {
System.out.println("Rock crushes scissors. You win!!");
} else {
}
}
}
}
}
The bolded text represents the area of code that I'm having trouble with.
If you have any tips on trying to simplifying or...making more sense of these nested if statements, please do tell!
I'm always getting confused when it starts to get really nested.
Most of all though, I'm getting sooooo confused with all the nested stuff. :|
// ****************************************************************
// Rock.java
//
// Play Rock, Paper, Scissors with the user
//
// ****************************************************************
import cs1.Keyboard;
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
Random generator = new Random();
//Get player's play -- note that this is stored as a string
System.out.println ("Play ROCK PAPER SCISSORS!!!");
System.out.println ("R = Rock");
System.out.println ("P = Paper");
System.out.println ("S = Scissors");
System.out.println ("Choose your weapon");
personPlay = Keyboard.readString();
//Make player's play uppercase for ease of comparison
personPlay = personPlay.toUpperCase();
System.out.println ("You chose... "+personPlay+" as your weapon.");
//Generate computer's play (0,1,2)
computerInt = generator.nextInt(3);
//Translate computer's randomly generated play to string
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;
default:
System.out.println ("Please re-enter a value between 0-2");
}
//Print computer's play
System.out.println ("The computer has chosen "+computerPlay+"--Prepare to do battle!");
//See who won
if (personPlay.equals(computerPlay)) {
System.out.println("It's a tie!");
} else {
if (personPlay.equals("R")) {
if (computerPlay.equals("S")) {
System.out.println("Rock crushes scissors. You win!!");
} else {
}
}
}
}
}
The bolded text represents the area of code that I'm having trouble with.
If you have any tips on trying to simplifying or...making more sense of these nested if statements, please do tell!
I'm always getting confused when it starts to get really nested.