If/Else Statements in Swtich Statements?

Started by
14 comments, last by rip-off 12 years, 11 months ago
So basically, I'm trying to write a pretty basic Rock, Paper, Scissors console game. Here's what I have so far:




public class RockPaperScissors {
public static void main(String[] args) {
String win = "You win!";
String lose = "Sorry. You lose.";
String computerMove;
String selection;
int myMove;
int rand;


//Get myMove

TextIO.putln("What's your move?");
TextIO.putln();
TextIO.putln(" 1. Rock");
TextIO.putln(" 2. Paper");
TextIO.putln(" 3. Scissors");
myMove = TextIO.getlnInt();

//Get computerMove

rand = (int)(3*Math.random());
if ( rand == 0)
computerMove = "Rock";
else if (rand == 1)
computerMove = "Paper";
else
computerMove = "Scissors";

//Compare moves

switch (myMove) {
case 1:
selection = "Rock";
if (computerMove == "Paper")
TextIO.putln(lose);
TextIO.putln(computerMove);
TextIO.putln(" beats ");
TextIO.putln(selection);
else
TextIO.putln(win);
TextIO.putln(selection);
TextIO.putln(" beats ");
TextIO.putln(computerMove);
}

}
}


The compiler is saying the "ELSE" statement in the case labeled "Case 1" is giving me the error: "Syntax error on token "else", delete this token". What can I do to remedy this? Thanks in advance.

PS. I posted a topic earlier, but it's subject has nothing to do with the content of this one. If I've created too many topics, please feel free to let me know so I can move this into the prior topic. Thanks again.
Advertisement
Just add parens to the case block to scope things properly


switch
{
case 0:
{
if (blah blah)
{
}
else
{
}

}
}
don't you need to use brackets when the if has multiple lines?
[size="2"]I like the Walrus best.
Your [font="'Courier New"]if[/font] and your [font="'Courier New"]else[/font] inside switch (the ones it's complaining about) don't have any braces ([font="'Courier New"]{ }[/font]) on them!
If an if/else has more than one statement inside it, those statements need to be contained within braces.

This is what Java is seeing: if (computerMove == "Paper")
TextIO.putln(lose);
//the 'if' just ended here
TextIO.putln(computerMove);//this is outside of the if
TextIO.putln(" beats ");
TextIO.putln(selection);
else //wtf, why is there an 'else' here that isn't connected to an 'if'?? ZOMG I'll report an error telling the user to delete it.
TextIO.putln(win);
TextIO.putln(selection);
TextIO.putln(" beats ");
TextIO.putln(computerMove);
I was just about to edit my initial post to ask that. But wouldn't it alter what the computer sees in and outside the brackets?

For example:

computerMove is initialized

{computerMove isn't initialized}



Thanks again!


Your [font="Courier New"]if[/font] and your [font="Courier New"]else[/font] inside switch (the ones it's complaining about) don't have any braces ([font="Courier New"]{ }[/font]) on them!
If an if/else has more than one statement inside it, those statements need to be contained within braces.

This is what Java is seeing: if (computerMove == "Paper")
TextIO.putln(lose);
//the 'if' just ended here
TextIO.putln(computerMove);//this is outside of the if
TextIO.putln(" beats ");
TextIO.putln(selection);
else //wtf, why is there an 'else' here that isn't connected to an 'if'?? ZOMG I'll report an error telling the user to delete it.
TextIO.putln(win);
TextIO.putln(selection);
TextIO.putln(" beats ");
TextIO.putln(computerMove);






Thank you for explaining in detail! Detail is always helpful :D I was aware that I could specifically define that multiple lines of code are within an 'if' statement with brackets, but I was afraid it would blind the compiler to everything outside of it.


So here's the final product (with a few changes from my initial post):

public class RockPaperScissors {
public static void main(String[] args) {
String win = "You win!";
String lose = "Sorry. You lose.";
String computerMove;
String selection;
int myMove;
int rand;


//Get myMove

TextIO.putln("What's your move?");
TextIO.putln();
TextIO.putln(" 1. Rock");
TextIO.putln(" 2. Paper");
TextIO.putln(" 3. Scissors");
myMove = TextIO.getlnInt();

//Get computerMove

rand = (int)(3*Math.random());
if ( rand == 0)
computerMove = "Rock";
else if (rand == 1)
computerMove = "Paper";
else
computerMove = "Scissors";

//Compare moves

switch (myMove) {
case 1:
selection = "Rock";
if (computerMove == "Paper")
{TextIO.putln(lose);
TextIO.putln();
TextIO.putln(computerMove + " beats " + selection);}
else if (computerMove == "Rock")
TextIO.putln("Tie!");
else
{TextIO.putln(win);
TextIO.putln();
TextIO.putln(selection + " beats " + computerMove);}
break;
case 2:
selection = "Paper";
if (computerMove == "Scissors")
{TextIO.putln(lose);
TextIO.putln();
TextIO.putln(computerMove + " beats " + selection );}
else if (computerMove == "Paper")
TextIO.putln("Tie!");
else
{TextIO.putln(win);
TextIO.putln();
TextIO.putln(selection + " beats " + computerMove);}
break;
case 3:
selection = "Scissors";
if (computerMove == "Rock")
{TextIO.putln(lose);
TextIO.putln();
TextIO.putln(computerMove + " beats " + selection);}
else if (computerMove == "Scissors")
TextIO.putln("Tie!");
else
{TextIO.putln(win);
TextIO.putln();
TextIO.putln(selection + " beats " + computerMove);}
break;
default:
TextIO.putln("You can't choose that! I quit!");
System.exit(1);
}

}
}


Is there anything anyone would've done differently? Also, rather than breaking from the case statement and ending the program, how can I go back to the beginning of the main class to start the game again?




edit: Nevermind. I'm pretty sure I can implement a boolean expression to determine whether or not the user would like to play again. (Although I'm not sure how to return to the beginning of the main class). Still, though. What would you have done differently?
challenge accepted :-)

public static void main(String[] args) {
String[] choices = new String[]{"Rock", "Paper", "Scissors"};
System.out.println("What's your move?");
for (int i = 0; i < 3; ++i)
System.out.println("\t" + (1 + i) + ". " + choices);
Scanner in = new Scanner(System.in);
int human = in.nextInt() - 1; //Get myMove
int pc = (int) (3. * Math.random());//Get computerMove

System.out.println("Computer chooses: " + choices[pc]);
System.out.println("You choosed: " + choices[human]);

int i = human - pc;
if (pc == human)
System.out.println("Tie!");
else if (i == 1 || i == -2)
System.out.println("You win!");
else
System.out.println("Computer wins!");
}



for such a little programm a while loop will do, break out if the player don'T want to play another game.


ps: i nearly forgot, never ever compare two strings with ==. Always use the equals method or isEmpty.
a.equals("b") instead of a == "b"
a.isEmpty() instead of a == ""

challenge accepted :-)

public static void main(String[] args) {
String[] choices = new String[]{"Rock", "Paper", "Scissors"};
System.out.println("What's your move?");
for (int i = 0; i < 3; ++i)
System.out.println("\t" + (1 + i) + ". " + choices);
Scanner in = new Scanner(System.in);
int human = in.nextInt() - 1; //Get myMove
int pc = (int) (3. * Math.random());//Get computerMove

System.out.println("Computer chooses: " + choices[pc]);
System.out.println("You choosed: " + choices[human]);

int i = human - pc;
if (pc == human)
System.out.println("Tie!");
else if (i == 1 || i == -2)
System.out.println("You win!");
else
System.out.println("Computer wins!");
}



for such a little programm a while loop will do, break out if the player don'T want to play another game.





lol! Show off :P




As a beginner, pieces of your solution looks foreign to me. Would you care to elaborate more with comments to specify what you're doing and when? If not, you're not obligated to. I'm just in Learning Mode, today because I'm happy that I achieved the results that I wanted to. This is the first interactive program I've started from scratch. And no matter how newbish my solution may be, I'm proud of it! :D




edit: By the way, you forgot to include 'import java.util.Scanner;' ;)


Just ask about the parts u don't understand.

I think the top part of the code should be quite clear.

Instead of "[font="CourierNew, monospace"][color="#660066"]TextIO[/font]" I use the standard utils to output and get values from the standart Streams(Console).
System.out.println("What's your move?");
...Scanner in = new Scanner(System.in);
int human = in.nextInt() - 1


After that the player and the pc has chosen a value in the range [0,2]. Where 0:Rock, 1:Paper and 2:Sicssors.
Now when u look at the numbers and compare them with the game rules u can see, that a number always wins against the next smaller one(1 vs 0, 2 vs 1).
There are 3 different combinations and because of that we need only one more rule to decide who is the winner(have already 2 above).

We check first of course if both choices are the same and call it a "Tie".
after that we subtract both choices from each other and will get -1, 1, -2 or 2 depending against which player we want to check we check for (1, -2) or (-1, 2)

This topic is closed to new replies.

Advertisement