Conditionals and loops

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 712 Views - Last Post: 25 March 2009 - 07:51 PM Rate Topic: -----

#1 swim_fan08  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 238
  • Joined: 19-February 09

Conditionals and loops

Posted 24 March 2009 - 04:15 PM

This a program that I am working its a rock, paper, scissors program, my main trouble that I am having is with the nested if statements.

I get the following errors when I compile it: java:75: operator % cannot be applied to java.lang.String,java.lang.String if( Player % Computer == "P")

java:77: operator % cannot be applied to java.lang.String,java.lang.String
if( Player % Computer == "S")

I would also appreciate it, if someone looked over my work to make sure that I've everything correctly.

Thanks!


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.println ("R = Rock");
				  System.out.println ("P = Paper");
				  System.out.println ("S = Scissors");

			  //----------------------------------------------------
			 //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 == Computer)
				{
					if( Player % Computer == "P")
					{
						if( Player % Computer == "S")
						System.out.println("Rock crushes scissors, you win!");
					else
			   			System.out.println("Scissors cut paper.");
						}
						else
							System.out.println("Paper covers rock.");
							System.out.println();
							}
	}
}
 

This post has been edited by swim_fan08: 24 March 2009 - 04:21 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Conditionals and loops

#2 thebpf  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 42
  • Joined: 19-March 09

Re: Conditionals and loops

Posted 24 March 2009 - 04:33 PM

Your line:

if (Player == Computer) won't work it should be:

if (!Player.equals(Compter)) // will check if player doesn't equal computer

What are you trying to accomplish with the "%" operator? The "%" operator is used for getting the remainder in division. ie (5%2 = 5 from 5/2 = 2.5) So you can only use the "%" operator on numbers.


I think what you meant was:

if (Player.equals("P") || Computer.equals("P")) ... Insert code after // CHecks if player equals "P" or computer equals "P"

Do the same thing for the other if statements.
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

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

Reputation: 8066
  • View blog
  • Posts: 31,310
  • Joined: 06-March 08

Re: Conditionals and loops

Posted 24 March 2009 - 04:35 PM

The % modulo operator returns the reminder of the division of 2 int

10 % 3 ---> 1

10 divided by 3 gives 3 reminder 1

you cannot apply % to String like Player or Computer

what do you think "Georges" % "David" should yield as result ?
Was This Post Helpful? 0
  • +
  • -

#4 Mikeyp926  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 29
  • View blog
  • Posts: 205
  • Joined: 20-March 09

Re: Conditionals and loops

Posted 24 March 2009 - 05:01 PM

A few things I noticed...

1.) Previous posters are correct, I don't believe you can use "%" on Strings, and also when comparing two strings you should use Player.equals(Computer).

2.) It would probably be a good idea to not capitalize names of variables such as "Player" and "Computer". Typically capitalization is reserved for class names, so capitalizing variables can lead to confusion. However, this is merely a suggestion, and it will not effect the way your program works.

3.) Finally, I noticed in your comments that you are supposed to use "nested if statements" and not "&&" or "||". This doesn't make sense to me, but if you are required to do it this way then your code would look something like

if(!Player.equals(Computer))
{
	 if(Player.equals("R")
	{
		if(Computer.equals("P")
		{
			   System.out.println("Computer Wins");
		}
		if(Computer.equals("S")
		{
			   System.out.println("Player Wins");
		}
	}
}
//...more code here to handle when the player is "S" and "P".

else
{
	System.out.println("Tie game");
}




You should be able to follow the pattern and handle the other cases using "else if" statements.
If this doesn't make sense just ask.

Hope this helps!
~Michael
Was This Post Helpful? 0
  • +
  • -

#5 swim_fan08  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 238
  • Joined: 19-February 09

Re: Conditionals and loops

Posted 24 March 2009 - 06:33 PM

View PostMikeyp926, on 24 Mar, 2009 - 04:01 PM, said:

A few things I noticed...

1.) Previous posters are correct, I don't believe you can use "%" on Strings, and also when comparing two strings you should use Player.equals(Computer).

2.) It would probably be a good idea to not capitalize names of variables such as "Player" and "Computer". Typically capitalization is reserved for class names, so capitalizing variables can lead to confusion. However, this is merely a suggestion, and it will not effect the way your program works.

3.) Finally, I noticed in your comments that you are supposed to use "nested if statements" and not "&&" or "||". This doesn't make sense to me, but if you are required to do it this way then your code would look something like

if(!Player.equals(Computer))
{
	 if(Player.equals("R")
	{
		if(Computer.equals("P")
		{
			   System.out.println("Computer Wins");
		}
		if(Computer.equals("S")
		{
			   System.out.println("Player Wins");
		}
	}
}
//...more code here to handle when the player is "S" and "P".

else
{
	System.out.println("Tie game");
}




You should be able to follow the pattern and handle the other cases using "else if" statements.
If this doesn't make sense just ask.

Hope this helps!
~Michael



I think I got it. I am going to try to code it and I dont get it, I'll post back in here.
Was This Post Helpful? 0
  • +
  • -

#6 swim_fan08  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 238
  • Joined: 19-February 09

Re: Conditionals and loops

Posted 25 March 2009 - 04:40 PM

I did the nested if statements 2 ways.

This is what I have, but not in my code.
if(!Player.equals(Computer))
{
	 if(Player.equals("R")
	{
		if(Computer.equals("P")
		{
			   System.out.println("Computer Wins");
		}
		if(Computer.equals("S")
		{
			   System.out.println("Player Wins");
		}
	}
}

if(Player.equals("S")
{
System.out.println("Computer wins!");
}
if(Player.equals("P")
{
System.out.println("Player wins!");
}

else
{
	System.out.println("Tie game");
}




The next code is what I have, but its causing errors

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

java:87: ')' expected if (Player.equals("P");

java:89: ')' expected if (Computer.equals("R");

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 }


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!");

	}
}

This post has been edited by swim_fan08: 25 March 2009 - 05:04 PM

Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

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

Reputation: 8066
  • View blog
  • Posts: 31,310
  • Joined: 06-March 08

Re: Conditionals and loops

Posted 25 March 2009 - 04:51 PM

if(Player.equals("S")
if(Player.equals("P")

you have ) missing: if(Player.equals("S"))
the number of opening ( must match the bumber of closing )
Was This Post Helpful? 1
  • +
  • -

#8 Mikeyp926  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 29
  • View blog
  • Posts: 205
  • Joined: 20-March 09

Re: Conditionals and loops

Posted 25 March 2009 - 05:34 PM

Ahhh, I can't believe I forgot the last ")" in the code I suggested, I should have popped it into eclipse to check for stuff like that. Sorry about that!

Also, the code you posted looks like you might still be having some problems with the nested if-statements.

If your program is working, then great! If not, here's a suggestion. I would structure your code something like this
if(!Player.equals(Computer))
{
	 if(Player.equals("R"))
	{
		if(Computer.equals("P"))
		{
			   System.out.println("Computer Wins");
		}
		if(Computer.equals("S"))
		{
			   System.out.println("Player Wins");
		}
	}
	else if(Player.equals("P"))
	{
		if(Computer.equals("S"))
		{
			   System.out.println("Computer Wins");
		}
		if(Computer.equals("R"))
		{
			   System.out.println("Player Wins");
		}
	}
	else if(Player.equals("S"))
	{
		if(Computer.equals("R"))
		{
			   System.out.println("Computer Wins");
		}
		if(Computer.equals("P"))
		{
			   System.out.println("Player Wins");
		}
	}
}
else
{
	System.out.println("Tie game");
}



Hope this makes sense! Basically, the first if statement checks if the player and comp are the same, if not, then it checks to see who won, but if they are the same then the final else is run and "Tie game" is printed.

If you have any question just ask, but this way seems the best way to me.

-Michael
Was This Post Helpful? 1
  • +
  • -

#9 swim_fan08  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 238
  • Joined: 19-February 09

Re: Conditionals and loops

Posted 25 March 2009 - 05:59 PM

View PostMikeyp926, on 25 Mar, 2009 - 04:34 PM, said:

Ahhh, I can't believe I forgot the last ")" in the code I suggested, I should have popped it into eclipse to check for stuff like that. Sorry about that!

Also, the code you posted looks like you might still be having some problems with the nested if-statements.

If your program is working, then great! If not, here's a suggestion. I would structure your code something like this
if(!Player.equals(Computer))
{
	 if(Player.equals("R"))
	{
		if(Computer.equals("P"))
		{
			   System.out.println("Computer Wins");
		}
		if(Computer.equals("S"))
		{
			   System.out.println("Player Wins");
		}
	}
	else if(Player.equals("P"))
	{
		if(Computer.equals("S"))
		{
			   System.out.println("Computer Wins");
		}
		if(Computer.equals("R"))
		{
			   System.out.println("Player Wins");
		}
	}
	else if(Player.equals("S"))
	{
		if(Computer.equals("R"))
		{
			   System.out.println("Computer Wins");
		}
		if(Computer.equals("P"))
		{
			   System.out.println("Player Wins");
		}
	}
}
else
{
	System.out.println("Tie game");
}



Hope this makes sense! Basically, the first if statement checks if the player and comp are the same, if not, then it checks to see who won, but if they are the same then the final else is run and "Tie game" is printed.

If you have any question just ask, but this way seems the best way to me.

-Michael


Now, when I use this code, I get this error:

java:114: reached end of file while parsing
}
Was This Post Helpful? 0
  • +
  • -

#10 Nat3TheGreat13  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 162
  • Joined: 09-February 09

Re: Conditionals and loops

Posted 25 March 2009 - 06:07 PM

Post your modified code
Was This Post Helpful? 0
  • +
  • -

#11 swim_fan08  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 238
  • Joined: 19-February 09

Re: Conditionals and loops

Posted 25 March 2009 - 06:24 PM

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

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

			  String player;

			  Strin 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))
					{
						 if(player.equals("R"))
						{
							if(computer.equals("P"))
							{
								   System.out.println("Paper beats rock, computer wins!");
							}
							if(computer.equals("S"))
							{
								   System.out.println("Rock crushes scissors. You win!");
							}
						}
						else if(player.equals("P"))
						{
							if(computer.equals("S"))
							{
								   System.out.println("Rock beats paper,computer wins!");
							}
							if(computer.equals("R"))
							{
								   System.out.println("Paper beats rock,you win!");
							}
						}
						else if(player.equals("S"))
						{
							if(computer.equals("R"))
							{
								   System.out.println("Rock crushes scissors, computer wins!");
							}
							if(computer.equals("P"))
							{
								   System.out.println("Scissors cut paper, you win!");
							}
						}
					}
					else
					{
						System.out.println("Tie game");
					}
 

This post has been edited by swim_fan08: 25 March 2009 - 06:30 PM

Was This Post Helpful? 0
  • +
  • -

#12 Nat3TheGreat13  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 162
  • Joined: 09-February 09

Re: Conditionals and loops

Posted 25 March 2009 - 06:30 PM

Your missing two closing braces at the end,

your missing the g on String,
String computer;


the P needs to be lowercase on this line
Player = scan.nextLine();



You spelled computer wrong
if(pomputer.equals("R"))


This post has been edited by Nat3TheGreat13: 25 March 2009 - 06:33 PM

Was This Post Helpful? 1
  • +
  • -

#13 Mikeyp926  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 29
  • View blog
  • Posts: 205
  • Joined: 20-March 09

Re: Conditionals and loops

Posted 25 March 2009 - 06:36 PM

Yep, I just posted the code with the conditionals, you'll have to add the closing braces to close the method and the class.
Just out of curiosity, have you considered using an IDE like Eclipse? I don't know if it would be a good idea or not, but it definitely saves time by finding typos like these.

-Michael
Was This Post Helpful? 0
  • +
  • -

#14 swim_fan08  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 238
  • Joined: 19-February 09

Re: Conditionals and loops

Posted 25 March 2009 - 06:39 PM

View PostMikeyp926, on 25 Mar, 2009 - 05:36 PM, said:

Yep, I just posted the code with the conditionals, you'll have to add the closing braces to close the method and the class.
Just out of curiosity, have you considered using an IDE like Eclipse? I don't know if it would be a good idea or not, but it definitely saves time by finding typos like these.

-Michael



I've been using TextPad, but I think I will look into Eclipse.
Was This Post Helpful? 0
  • +
  • -

#15 Nat3TheGreat13  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 162
  • Joined: 09-February 09

Re: Conditionals and loops

Posted 25 March 2009 - 06:40 PM

Here is the working code. Your going to have to make changes for the game to last longer then 1 turn.
Also, changed the else statement because it would never reach it.
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
				//------------------------------------------------------
			

				//---------------------------------------------------------------
				//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("r"))
						{
							if(computer.equals("p"))
							{
								   System.out.println("Paper beats rock, computer wins!");
							}
						   else if(computer.equals("s"))
							{
								   System.out.println("Rock crushes scissors. You win!");
							}
									 else
									 {
									 System.out.println("Tie");
									 }
						}
						else if(player.equals("p"))
						{
							if(computer.equals("s"))
							{
								   System.out.println("Rock beats paper,computer wins!");
							}
							else if(computer.equals("r"))
							{
								   System.out.println("Paper beats rock,you win!");
							}
									 else
									 {
									 System.out.println("Tie");
									 }
						}
						else if(player.equals("s"))
						{
							if(computer.equals("r"))
							{
								   System.out.println("Rock crushes scissors, computer wins!");
							}
							else if(computer.equals("p"))
							{
								   System.out.println("Scissors cut paper, you win!");
							}
									 else
									 {
									 System.out.println("Tie");
									 }
						}
					
				   
 }
 }


This post has been edited by Nat3TheGreat13: 25 March 2009 - 06:48 PM

Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2