Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Condtions and loops

loop

  • Please log in to reply
3 replies to this topic

#1 hoku2000_99

hoku2000_99

    CC Newcomer

  • Just Joined
  • PipPip
  • 24 posts

Posted 25 March 2009 - 04:14 PM

I am working on this java application of rock, paper, scissors. I think I have everything I need, but I get most of my errors when it comes to the nested if statements that I am suppose to be using. I would appreciate it if someone were to help me out and give me some pointers.

import java.util.Scanner;
import java.util.Random;

public class RPS
{
	public static void main (String[] args)
	{

		      String Player;

		      String Computer;

		      int ComputerInt;

		      Scanner scan = new Scanner(System.in);
			  Random generator = new Random();

		      //------------------------------------------------
			  //User will enter an upper or lowercase r, p, or s
			  //for his/her selection.
			  //-------------------------------------------------


				System.out.print ("Please enter: r for rock, p for paper, or s for scissors: ");
				Player = scan.nextLine();


			  //----------------------------------------------------
			 //Generate a random number (integer value of 0, 1, or 2)
			 //for the computer's play.
			 //------------------------------------------------------
			  ComputerInt = generator.nextInt(3);

		      //-----------------------------------------------------
		      //Convert the computer's random integer to an uppercase
		      //letter using a switch structure.
		      //-----------------------------------------------------

		 	switch (ComputerInt)
		 	{
		 		case 0:
		 			Computer = "R";
		 			System.out.println ("Computer chose rock.");
		 			break;
		 		case 1:
		 			Computer = "P";
		 			System.out.println ("Computer chose paper.");
		 			break;
		 		case 2:
		 			Computer = "S";
		 			System.out.println ("Computer chose scissors.");
		 			break;
		 	}
				//------------------------------------------------------
				//Display the computer's play as a string. For example:
				//The computer's play is S
				//------------------------------------------------------
				System.out.println ("The computer chose" + Computer);

				//---------------------------------------------------------------
				//Determine who won. Use nested if statements (not using && or ||).
				//Display the results as a sentence. For example:
				//Rock crushes scissors, you win!
				//Remember: Rock crushes scissors. Scissors cut paper.
				//Paper covers rock.Ties are possible.
				//----------------------------------------------------------------
if (!Player.equals(Computer))
 System.out.println ("It's a tie!");
    {
      if  (Player.equals("R"))
        {
            if (Computer.equals("S"))
               System.out.println ("Rock crushes scissors. You win!");
            else
                (Computer.equals("P"));
            }
        		else
            		System.out.println ("Paper beats rock. Computer wins!");
    			}
    				if (Player.equals("P"));
    				{
						 if (Computer.equals("R"));
						  System.out.println ("Paper beats rock. You win!");
						else
						System.out.println("Rock beats paper. Computer wins!");
					}
						if (Player.equals("S"));
						{
							if (Computer.equals("R"));
							{
								else
								System.out.println ("Rock crushes scissors. Computer wins!");

    }
}

My errors that I get:

java:82: not a statement (Computer.equals("P"));

java:91: 'else' without 'if' else

java:92: <identifier> expected System.out.println("Rock beats paper. Computer wins!");

java:92: illegal start of type System.out.println("Rock beats paper. Computer wins!");

java:94: class, interface, or enum expected if (Player.equals("S");

java:95: class, interface, or enum expected {

java:97: class, interface, or enum expected {

java:101: class, interface, or enum expected }
  • 0

#2 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 25 March 2009 - 04:53 PM

I've done some reformatting of your code to help highlight the real structure. Watch out for extra semicolons, and read the comments I've added.
if (!Player.equals(Computer))
  System.out.println ("It's a tie!");  //shouldn't this be Player.equals(Computer)?  Need an else
{
  if  (Player.equals("R"))
  {
    if (Computer.equals("S"))
      System.out.println ("Rock crushes scissors. You win!");
    else
      (Computer.equals("P"));  //this should be a System.out.println
  }
  else
    System.out.println ("Paper beats rock. Computer wins!");  //this triggers whenever Player is not Rock
}
if (Player.equals("P"));
{
  if (Computer.equals("R"));
    System.out.println ("Paper beats rock. You win!");
  else
    System.out.println("Rock beats paper. Computer wins!");
}
if (Player.equals("S"));
{
  if (Computer.equals("R"));
  {
    else //else???  You need a statement first!
      System.out.println ("Rock crushes scissors. Computer wins!");
  }
}

  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#3 hoku2000_99

hoku2000_99

    CC Newcomer

  • Just Joined
  • PipPip
  • 24 posts

Posted 25 March 2009 - 05:27 PM

Thanks for the help!
  • 0

#4 John

John

    CC Mentor

  • Moderator
  • 4450 posts
  • Location:New York, NY

Posted 25 March 2009 - 06:24 PM

I would recommend always using braces even for single line conditions, it makes readability more clear.
Rather than
if(x == 5) 
    foo();
    bar();
It is a lot more clear to do:
if(x == 5) {
    foo();
}
bar();

  • 0





Also tagged with one or more of these keywords: loop

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download