Rock, Paper, Scissors

Can anyone help me!!

Page 1 of 1

5 Replies - 1312 Views - Last Post: 12 February 2010 - 06:12 AM Rate Topic: -----

#1 rasmus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 10-February 10

Rock, Paper, Scissors

Posted 12 February 2010 - 04:40 AM

Can someone help me and see what I have done wrong. It says Error at

Rock1.java: 44: 'else' without 'if'
else (computerPlay.equals ( "P"))

Rock1.java: 44: not a statement
else (computerPlay.equals ( "P"));

Rock1.java: 53: not a statement
else (computerPlay.equals ( "S"));

/code/
import java.util.Scanner;
import java.util.Random;

public class Rock1
{
public static void main (String[] args)
{
String personPlay;
String computerPlay = "1, 2, 3";
int computerInt;


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

System.out.println ("Spela ROCK PAPER SCISSORS!!!");
System.out.println ("R = Rock");
System.out.println ("P = Paper");
System.out.println ("S = Scissors");

System.out.println ("Enter your play: r, p, or s");
personPlay = personPlay.toUpperCase();
personPlay = Scan.nextLine();

computerPlay = generator.nextInt (3) + 1;
System.out.println ("From 1 to 3: " + computerPlay);


//Print computer's play
System.out.println ("Datorn har valt "+computerPlay+" Förbered er för strid! ");
//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(computerPlay.equals("P"));
{
System.out.println ("Paper beats rock. Computer wins! ");
}

if (personPlay.equals("R"))
{
System.out.println ("Paper beats rock. You win! ");
}
else (computerPlay.equals("S"));
{
System.out.println ("Scissors cut paper. Computer wins!");
}

if (computerPlay.equals("R"))
System.out.println ("Rock crushes scissors. Computer wins!");

if (computerPlay.equals("P"))
System.out.println ("Scissors cut paper. You win!");

}
}
}
}
/code/

Is This A Good Question/Topic? 0
  • +

Replies To: Rock, Paper, Scissors

#2 EdwinNameless  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 120
  • View blog
  • Posts: 710
  • Joined: 15-October 09

Re: Rock, Paper, Scissors

Posted 12 February 2010 - 04:44 AM

You have a couple of "if" or "else" statements with a ";" right after the condition, like for example:

if (computerPlay.equals("S")); 



Remove these semicolons.
Was This Post Helpful? 0
  • +
  • -

#3 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Rock, Paper, Scissors

Posted 12 February 2010 - 05:43 AM

also, take a look at that line:
else(computerPlay.equals("P"));



you cannot add a condition to an else statement.
an else statement is like a "default".
what happens if all other conditions are false.
so you can't add a condition to it.

what you will have to do, is add the "if" keyword, nd create an else if statement.
this will fix it:
(in addition to what EdwinNameless suggested of course)
else if(computerPlay.equals("P"))



i spotted that mistake again in your code so fix them all.

also, for next time, please post your code between code tags.
Was This Post Helpful? 1
  • +
  • -

#4 rasmus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 10-February 10

Re: Rock, Paper, Scissors

Posted 12 February 2010 - 05:59 AM

Now it is like this??

Rock1.java:25: incompatible types
found : int
required: java.lang.String
computerPlay = generator.nextInt (3) + 1;
Was This Post Helpful? 0
  • +
  • -

#5 anonymouscodder  Icon User is offline

  • member icon

Reputation: 126
  • View blog
  • Posts: 710
  • Joined: 01-January 10

Re: Rock, Paper, Scissors

Posted 12 February 2010 - 06:07 AM

personPlay = personPlay.toUpperCase();
personPlay = Scan.nextLine();


What's the point in calling the toUpperCase method before the nextLine call?

And use the code tags, like :code:.

View Postrasmus, on 12 February 2010 - 08:59 AM, said:

Now it is like this??

Rock1.java:25: incompatible types
found : int
required: java.lang.String
computerPlay = generator.nextInt (3) + 1;


It's because computerPlay is a String. If you want to treat a int like a String you can use the method Integer.toString(). If you want to treat a String like an int you can use the method Integer.parseInt().

This post has been edited by anonymouscodder: 12 February 2010 - 06:08 AM

Was This Post Helpful? 0
  • +
  • -

#6 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Rock, Paper, Scissors

Posted 12 February 2010 - 06:12 AM

anonymouscodder is right.
i believe you meant to use the computerInt varable instead of the computerPlay here:
computerPlay = generator.nextInt (3) + 1;
//sould be
computerInt = generator.nextInt (3) + 1;


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1