4 Replies - 3019 Views - Last Post: 07 November 2010 - 09:20 PM Rate Topic: -----

#1 Guest_Dylan*


Reputation:

Rock Paper Scissors Syntax Error

Posted 07 November 2010 - 01:48 PM

//****************************
//Rock.java
//
//Design and implement an application that plays the Rock- Paper- Scissors game against the computer.
//****************************

import cs1.Keyboard;
import java.util.Random;


public class Rock 
{
	public static void main(String[] args)
	{
		//User's play: R, P, or S
		String personPlay; 
		
		//Computer's play: R, P, or S
		String computerPlay= ''; 
	
		//Randomly generated number used to determine
		//computer's play
		int computerInt; 

		Random generator = new Random();

		//Get player's play 
		System.out.println ('Play ROCK PAPER SCISSORS!!!');
		System.out.println ('R = Rock');
		System.out.println ("P = Paper");
		System.out.println ('S = Scissors');
		System.out.println ('Choose your weapon');
		personPlay = Keyboard.readString();

		//Make player's play uppercase for ease of comparison
		personPlay = personPlay.toUpperCase();
		System.out.println ('You chose... '+personPlay+' as your weapon.');

		//Generate computer's play (0,1,2)
		computerInt = generator.nextInt(3);

		//Translate computer's randomly generated play to string
		switch (computerInt){
		case 0:
		computerPlay = 'R';
		System.out.println ('Computer has chosen ROCK.');
		break;
		case 1:
		computerPlay = 'P';
		System.out.println ('Computer has chosen PAPER.');
		break;
		case 2:
		computerPlay = 'S';
		System.out.println ('Computer has chosen SCISSORS.');
		break;
		default:
		System.out.println ('Please re-enter a value between 0-2');
		}

		//Print computer's play
		System.out.println ('The computer has chosen '+computerPlay+'--Prepare for a war!');
	}

}


It gives me an error for this line of code: String computerPlay= '';
Also when I imported the cs1 I got an error saying this: static String readString();

This post has been edited by macosxnerd101: 07 November 2010 - 01:51 PM
Reason for edit:: Added code tags.


Is This A Good Question/Topic? 0

Replies To: Rock Paper Scissors Syntax Error

#2 Brewer  Icon User is offline

  • Awesome
  • member icon

Reputation: 179
  • View blog
  • Posts: 1,044
  • Joined: 14-June 10

Re: Rock Paper Scissors Syntax Error

Posted 07 November 2010 - 01:52 PM

You first error is most likely because you didn't assign anything to the string. If you don't want to assign anything to it you can simply declare it as String computerPlay;.
Was This Post Helpful? 0
  • +
  • -

#3 Guest_Dylan*


Reputation:

Re: Rock Paper Scissors Syntax Error

Posted 07 November 2010 - 01:56 PM

I do want it to assign a string though, I don't know what I would put
Was This Post Helpful? 0

#4 Brewer  Icon User is offline

  • Awesome
  • member icon

Reputation: 179
  • View blog
  • Posts: 1,044
  • Joined: 14-June 10

Re: Rock Paper Scissors Syntax Error

Posted 07 November 2010 - 02:05 PM

Well strings, at least in my experience, are defined using double quotes (" "). Chars are usually defined by using single quotes(' ').
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

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

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Rock Paper Scissors Syntax Error

Posted 07 November 2010 - 09:20 PM

It is

String computerPlay= "";
computerPlay = "R";

or

char computerPlay = ' ';
computerPlay = 'R';

This post has been edited by pbl: 07 November 2010 - 09:23 PM
Reason for edit:: it is indeed ' ' not ''

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1