13 Replies - 2399 Views - Last Post: 14 March 2012 - 02:05 PM Rate Topic: -----

#1 NessValdez  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 70
  • Joined: 12-March 12

Rock, Paper, Scissor 'else' without 'if' error

Posted 12 March 2012 - 12:42 PM

Hey! I have to write a program for the classic game, "Rock, Paper, Scissor." I think so far most of my coding is ok. Right now, my biggest problem is whenever I run it, it says 'else' without 'if' error. I tried looking to make sure I had all my brackets, and to me it all looks right. Maybe I am over looking something . . . Once I figure that out, then I can fix the scoring issues . . .

import java.util.Scanner;



  public class VLV58750a9 {

    public static void main(String[] args) {

   

      // Declaring variable for computer


      do {

      int computer = (int)(Math.random() * 3);

      
      // Prompt the user to enter a number



      Scanner input = new Scanner(System.in);

      System.out.println();

      System.out.print(" rock (1), paper (2), scissor (3): ");

     
      int user = input.nextInt();

      if (user == 1 || user == 2 || user == 3) {
                     

      // Execution of game



      switch (computer) {

      
      case 1: if (user == 1) 

              System.out.println("Computer chose rock. You chose rock. It's a draw!");

	

      	      else if (user == 2) 

              System.out.println("Computer chose rock. You chose paper. You won!");

              win = win + 1; 
              win++;


              else if (user == 3) 

              System.out.println("Computer chose rock. You chose scissors. You lose!");

              win = win - 1; 
              win--;

              break;


      case 2: if (user == 1)

	      System.out.println("Computer chose paper. You chose rock. You lose!");

	      win = win - 1; 
	      win--;


              else if (user == 2) 

	      System.out.println("Computer chose paper. You chose paper. It's a draw!");

	

              else if (user == 3) 

	      System.out.println("Computer chose paper. You chose scissors. You won!");

	      win = win + 1; 
	      win++;

	      break;


      case 3: if (user == 1)

   	      System.out.println("Computer chose scissors. You chose rock. You won!");

	      win = win + 1; 
	      win++;


              else if (user == 2) 

	      System.out.println("Computer chose scissors. You chose paper. You lose!");

	      win = win - 1; 
	      win--;


              else if (user == 3) 

	      System.out.println("Computer chose scissors. You chose scissors. It's a draw!");

	      break;

	

      }

      }

	

      else
	System.out.println(" Sorry! PLEASE enter number for rock (1), paper (2), scissor (3): ");

      } while (win < (loss + 3) || (win + 3) > loss);

      

     }

 }




Is This A Good Question/Topic? 0
  • +

Replies To: Rock, Paper, Scissor 'else' without 'if' error

#2 NantucketSleighride  Icon User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • Posts: 111
  • Joined: 13-February 11

Re: Rock, Paper, Scissor 'else' without 'if' error

Posted 12 March 2012 - 02:18 PM

You're missing butt loads of {'s and }'s during some of your if and else if statements.
Was This Post Helpful? 0
  • +
  • -

#3 NessValdez  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 70
  • Joined: 12-March 12

Re: Rock, Paper, Scissor 'else' without 'if' error

Posted 12 March 2012 - 03:05 PM

Please do tell then . . . I had curly brackets plugged in on my 'if and then' statements and it gave me the same error. Someone advised me that they weren't techinally needed so I took them out. Either way, same error. But if you can show instead of tell, that would be very helpful!! Thanks!
Was This Post Helpful? 0
  • +
  • -

#4 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2250
  • View blog
  • Posts: 5,340
  • Joined: 10-September 10

Re: Rock, Paper, Scissor 'else' without 'if' error

Posted 12 March 2012 - 03:42 PM

In the if/else construction, the else statement must immediately follow the if statement or clause it complements. As long as the above rule is followed, braces aren't required, but that doesn't apply to most of the code you've posted. For example:

(Lines 48 - 56)
              else if (user == 2) 

              System.out.println("Computer chose rock. You chose paper. You won!");

              win = win + 1; 
              win++;


              else if (user == 3)

The above code is pulled from the middle of an if/else if/else if/etc. chain. Can you answer:

1. Where does the first else if statement end?
2. Does the 2nd else if statement immediately follow the previous if statement?
Was This Post Helpful? 1
  • +
  • -

#5 NessValdez  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 70
  • Joined: 12-March 12

Re: Rock, Paper, Scissor 'else' without 'if' error

Posted 13 March 2012 - 12:38 PM

Ok, so I added back the brackets and it came up with the same errors. So I took everything to do with the scoring out and it works just fine. How would I go about adding the scoring back in? I want the program to allow the user to continuously play until either the user or the computer has 3 more wins than losses. I know I have to do a loop. Would a do while loop work best? Here is my coding so far :

 import java.util.Scanner;



  public class VLV58750a9 {

    public static void main(String[] args) {

   



      // Declaring variable for computer

      

      

      int computer = (int)(Math.random() * 3);

      

      // Prompt the user to enter a number



      Scanner input = new Scanner(System.in);

      System.out.println();

      System.out.print(" rock (1), paper (2), scissor (3): ");

     

      int user = input.nextInt();



      if (user == 1 || user == 2 || user == 3) {

                     

      // Execution of game



      switch (computer) {

      

      case 1: if (user == 1) {

              System.out.println("Computer chose rock. You chose rock. It's a draw!"); 

	      }

      	      else if (user == 2) {

              System.out.println("Computer chose rock. You chose paper. You won!");

              }

              else if (user == 3) {

              System.out.println("Computer chose rock. You chose scissors. You lose!");

              }

              break;



      case 2: if (user == 1) {

	      System.out.println("Computer chose paper. You chose rock. You lose!");

	      }

              else if (user == 2) {

	      System.out.println("Computer chose paper. You chose paper. It's a draw!");

	      }

              else if (user == 3) {

	      System.out.println("Computer chose paper. You chose scissors. You won!");

	      }

	      break;



      case 3: if (user == 1) { 

   	      System.out.println("Computer chose scissors. You chose rock. You won!");

	      }

              else if (user == 2) {

	      System.out.println("Computer chose scissors. You chose paper. You lose!");

	      }

              else if (user == 3) {

	      System.out.println("Computer chose scissors. You chose scissors. It's a draw!");

	      }

	      break;

	

      }



      }

	

      else System.out.println(" PLEASE enter a number representing rock (1), paper (2), scissor (3): ");

      

      } 

      

     }

Was This Post Helpful? 0
  • +
  • -

#6 collegekid901  Icon User is offline

  • New D.I.C Head

Reputation: 8
  • View blog
  • Posts: 41
  • Joined: 09-March 12

Re: Rock, Paper, Scissor 'else' without 'if' error

Posted 13 March 2012 - 12:56 PM

do while seem like it would work. you can have two variables that keep tracks of win and losses. You will need a condition that ends the while loop which ends the game. Like if a player gets 5 wins or 5 losses or if a player inputs a 0 or whatever you see fit.
Was This Post Helpful? 0
  • +
  • -

#7 collegekid901  Icon User is offline

  • New D.I.C Head

Reputation: 8
  • View blog
  • Posts: 41
  • Joined: 09-March 12

Re: Rock, Paper, Scissor 'else' without 'if' error

Posted 13 March 2012 - 01:02 PM

I missed the scoring part you originally had. Are you trying to do when you have three more wins than losses or three more losses than wins the game ends? And add that to the code you have already written?
Was This Post Helpful? 0
  • +
  • -

#8 NessValdez  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 70
  • Joined: 12-March 12

Re: Rock, Paper, Scissor 'else' without 'if' error

Posted 13 March 2012 - 01:19 PM

Ok. Here is how I set everything up:

// Generating random number for computer

      do {

      int computer = (int)(Math.random() * 3);


// Prompt the user to enter a number

      Scanner input = new Scanner(System.in);

      System.out.println();

      System.out.print(" rock (1), paper (2), scissor (3): ");

      
      int win = 0;
      int loss = 0;

      int user = input.nextInt();



      if (user == 1 || user == 2 || user == 3) {


// Execution of game



      switch (computer) {

      

      case 1: if (user == 1) {

              System.out.println("Computer chose rock. You chose rock. It's a draw!"); 

	      }

      	      else if (user == 2) {

              System.out.println("Computer chose rock. You chose paper. You won!");

              win = win + 1;
	      win++;
	      }

              else if (user == 3) {

              System.out.println("Computer chose rock. You chose scissors. You lose!");
	      loss = loss + 1;
	      loss++;

              }

              break;



I'm not done with the while statement. I am just using this to make sure everything else works first. But the error I am getting is that it can't find the symbol for "win" or "loss" on line 93 which is where my while statement is located. Im guessing this has something to do with the placement of my curly brackets?

      }



      }

	

      else System.out.println(" PLEASE enter a number representing rock (1), paper (2), scissor (3): ");

      
      } while (win > loss);

      } 

      

     }


To Collegekid, whatever which way works. I've tried both here.

To Collegekid, whatever which way works. I've tried both here.
Was This Post Helpful? 0
  • +
  • -

#9 collegekid901  Icon User is offline

  • New D.I.C Head

Reputation: 8
  • View blog
  • Posts: 41
  • Joined: 09-March 12

Re: Rock, Paper, Scissor 'else' without 'if' error

Posted 13 March 2012 - 01:42 PM

Any reason you do
win = win + 1;
win++;

It does the same thing twice so wins=0 becomes wins =2.


With your while condition keep in mind.Do-while loops run the code once and then check to see if your while loop condition is met. If condition is met the code stops, if not the code keeps running.

Also declare your win/loss variables outside the loop. They will be reset to 0 every time the loop run since they are inside the loop.
Was This Post Helpful? 1
  • +
  • -

#10 NessValdez  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 70
  • Joined: 12-March 12

Re: Rock, Paper, Scissor 'else' without 'if' error

Posted 13 March 2012 - 01:59 PM

I moved the win/loss variables outside the loop. No wonder my while statement couldnt find the variables. I had both statements because when I did the same program in Visual Basic, that's how it computed the scoring.
win = win + 1;
      win++;


But I took out one like you suggested. Now I have to figure out the while loop statement. Am I really off? Because the program wont continuously loop.

 } while (win == 3 + loss || loss == 3 + win);

Was This Post Helpful? 0
  • +
  • -

#11 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2250
  • View blog
  • Posts: 5,340
  • Joined: 10-September 10

Re: Rock, Paper, Scissor 'else' without 'if' error

Posted 13 March 2012 - 02:27 PM

Write in words what the while loop is supposed to do. In other words, describe the condition(s) - in words - that will cause the while loop to execute and then the conditions - in words - that will stop the while loop execution.
Was This Post Helpful? 0
  • +
  • -

#12 NessValdez  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 70
  • Joined: 12-March 12

Re: Rock, Paper, Scissor 'else' without 'if' error

Posted 13 March 2012 - 02:52 PM

Here is the code if anybody wanted to use it as a reference!! Works GREAT now :)

import java.util.Scanner;



  public class VLV58750a9 {

    public static void main(String[] args) {

   

      // Scoring
      int win = 0;
      while (win <= 2 || win <= -2) {


      // Generating random number for computer

      int computer = (int) ((Math.random() * 3) + 1);

      

      // Prompt the user to enter a number

      Scanner input = new Scanner(System.in);

      System.out.println();

      System.out.print(" rock (1), paper (2), scissor (3): ");

      
      int user = input.nextInt();



      if (user == 1 || user == 2 || user == 3) {

                     

      // Execution of game



      switch (computer) {

      

      case 1: if (user == 1) {
	      System.out.println();

              System.out.println("Computer chose rock. You chose rock. It's a draw!"); 

	      }

      	      else if (user == 2) {
	      System.out.println();

              System.out.println("Computer chose rock. You chose paper. You won!");

              win++;
	      }

              else if (user == 3) {
	      System.out.println();

              System.out.println("Computer chose rock. You chose scissors. You lose!");
	      win--;

              }

              break;



      case 2: if (user == 1) {
	      System.out.println();

	      System.out.println("Computer chose paper. You chose rock. You lose!");
	      win--;

	      }

              else if (user == 2) {
	      System.out.println();

	      System.out.println("Computer chose paper. You chose paper. It's a draw!");
	      }

              else if (user == 3) {
	      System.out.println();

	      System.out.println("Computer chose paper. You chose scissors. You won!");
	      win++;

	      }

	      break;



      case 3: if (user == 1) { 
	      System.out.println();

   	      System.out.println("Computer chose scissors. You chose rock. You won!");
	      win++;

	      }

              else if (user == 2) {
	      System.out.println();

	      System.out.println("Computer chose scissors. You chose paper. You lose!");
	      win--;

	      }

              else if (user == 3) {
	      System.out.println();

	      System.out.println("Computer chose scissors. You chose scissors. It's a draw!");
	      }

	      break;

	

      }



      }

	

      else 
	      System.out.println(" PLEASE enter a number representing rock (1), paper (2), scissor (3): ");

      }

      if (win > 2) {
	System.out.println();
	System.out.println("You have won this match!");
	System.out.println();

      } 

      else if (win < -2) {
	System.out.println();
	System.out.println("Sorry. You have lost this match.");
	System.out.println();

     }

     }
  }




Was This Post Helpful? 0
  • +
  • -

#13 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Rock, Paper, Scissor 'else' without 'if' error

Posted 13 March 2012 - 09:52 PM

Remove thos completly useless blank line in your code...
On a labtop we have to scroll to see a complete method
Rule of the thumb: a method should fit on a screen ... if it doesn' split it into 2 different methods

The way you write your code, each method will have a maximum of 5 to 7 lines :)
Was This Post Helpful? 0
  • +
  • -

#14 NessValdez  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 70
  • Joined: 12-March 12

Re: Rock, Paper, Scissor 'else' without 'if' error

Posted 14 March 2012 - 02:05 PM

Sorry. When I copy and pasted, for some reason it added extra lines. I just didnt take the time to delete them all. Will know for next time! It was bothering me too!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1