rock paper scissors program

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 1834 Views - Last Post: 09 October 2014 - 02:21 PM Rate Topic: -----

#1 melina2134  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 16
  • Joined: 23-September 14

rock paper scissors program

Posted 07 October 2014 - 09:46 AM

im updating my last post because it got jumbled:

this is the first part to a PROGRAM that's supposed to generate random numbers between 0-2 which enables the user to play rock (0) paper (1) scissors (2) but its giving me errors that i'm not sure how to correct:
Line: 12
'(' or '[' expected

Line: 12
illegal start of type

Line: 12
<identifier> expected

Line: 12
';' expected

Line: 12
illegal start of type

Line: 12
<identifier> expected

Line: 12
';' expected

Line: 12
illegal start of type

Line: 12
<identifier> expected

Line: 12
';' expected

Line: 13
<identifier> expected



import java.util.*;
import java.util.Scanner;

public class Lab3
{
	
		public static void main(String[] args);
		Scanner input = new Scanner(System.in);
		int scissor = 0;
		int rock = 1;
		int paper = 2;
		Random computer = new Random randomGenerator = new Random() + 3;
		int guess = input.nextInt();
	{
		Random randomGenerator = new Random() + 3;
	if (guess == computer){
			System.out.println("It's a draw!");
			num = input.nextInt();
		}
	else {
		if (guess < computer)
		System.out.println("You lost. Better luck next time!");
		num = input.nextInt();
		}
	if (guess > computer)
		{
		System.out.println("You won!");
		}
}
}



Is This A Good Question/Topic? 0
  • +

Replies To: rock paper scissors program

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13493
  • View blog
  • Posts: 53,899
  • Joined: 12-June 08

Re: rock paper scissors program

Posted 07 October 2014 - 09:49 AM

07	        public static void main(String[] args);

Typically main is a start of a method or function. It would not need a semi colon, but would need to { and } to surround the code in its body.
Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101  Icon User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12149
  • View blog
  • Posts: 45,175
  • Joined: 27-December 08

Re: rock paper scissors program

Posted 07 October 2014 - 09:50 AM

At line 7: public static void main(String[] args);. This is illegal. The method should have braces:

public static void main(String[] args){
   //your relevant code goes in here
}



Next, you have random braces scattered all over the place. Start by properly formatting your code and these should become apparent. If a set of braces doesn't belong to a method, loop, or conditional statement, I would question why you have it.
Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky  Icon User is online

  • Chinga la migra
  • member icon


Reputation: 10690
  • View blog
  • Posts: 18,307
  • Joined: 19-March 11

Re: rock paper scissors program

Posted 07 October 2014 - 09:52 AM

 Random computer = new Random randomGenerator = new Random() + 3;



In addition to the above, this line doesn't make sense.

To create an instance of an object, you need a declaration and an initialization:

Class myObject = new Class(arg1, arg2);


That is, you declare that myObject is a variable holding a reference to an object of class Class (this is what's on the left side of the = sign) and then you assign to that a particular reference - in this case, the result of calling the constructor for Class with arguments arg1 and arg2.
Was This Post Helpful? 0
  • +
  • -

#5 melina2134  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 16
  • Joined: 23-September 14

Re: rock paper scissors program

Posted 07 October 2014 - 11:13 AM

View Postmacosxnerd101, on 07 October 2014 - 09:50 AM, said:

At line 7: public static void main(String[] args);. This is illegal. The method should have braces:

public static void main(String[] args){
   //your relevant code goes in here
}



Next, you have random braces scattered all over the place. Start by properly formatting your code and these should become apparent. If a set of braces doesn't belong to a method, loop, or conditional statement, I would question why you have it.


alright i edited this program but it says Compilation Errors Detected

Line: 14
'.class' expected
???

import java.util.*;
import java.util.Scanner;

public class Lab3
{
	
		public static void main(String[] args){
		Scanner input = new Scanner(System.in);
		int scissor = 0;
		int rock = 1;
		int paper = 2;
		Random ran = new Random();
		int x = ran.nextInt(3) + 0;
		int randomIndex = int computer;
		int guess = input.nextInt();
	if (guess == computer){
			System.out.println("It's a draw!");
		}
	else {
		if (guess < computer)
		System.out.println("You lost. Better luck next time!");
		}
	if (guess > computer)
		{
		System.out.println("You won!");
		}
	}
}


Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12149
  • View blog
  • Posts: 45,175
  • Joined: 27-December 08

Re: rock paper scissors program

Posted 07 October 2014 - 11:18 AM

1) Please properly indent your code. It is a mess to read.

2) This is illegal syntax: int randomIndex = int computer;. What are you trying to do here?
Was This Post Helpful? 0
  • +
  • -

#7 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13493
  • View blog
  • Posts: 53,899
  • Joined: 12-June 08

Re: rock paper scissors program

Posted 07 October 2014 - 11:18 AM

Quote

Line: 14

It says to look at line 14.


14	        int randomIndex = int computer;

What is going on there? What is 'computer'? Why is there an 'int' in front of computer?
Was This Post Helpful? 0
  • +
  • -

#8 melina2134  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 16
  • Joined: 23-September 14

Re: rock paper scissors program

Posted 07 October 2014 - 11:45 AM

the computer is going to randomly print out either 0, 1, or 2. these are integers. the valuse that the computer will print is an integer so i labeled it as such...

This post has been edited by andrewsw: 08 October 2014 - 01:16 PM
Reason for edit:: Removed previous quote, just press REPLY

Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101  Icon User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12149
  • View blog
  • Posts: 45,175
  • Joined: 27-December 08

Re: rock paper scissors program

Posted 07 October 2014 - 11:48 AM

So how about assigning randomIndex a random integer in {0, 1, 2} using the Random nextInt() method? That would make more sense.
Was This Post Helpful? 0
  • +
  • -

#10 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13493
  • View blog
  • Posts: 53,899
  • Joined: 12-June 08

Re: rock paper scissors program

Posted 07 October 2014 - 11:50 AM

No.. that is not how it works.

A variable is a alphanumeric name for a memory space.

In this case:
09	        int scissor = 0;

you have a variable named 'scissor' that is filled with the value '0'.


If this:
14          int randomIndex = int computer;


would be this:
int randomIndex = computer;

It would say variable 'randomIndex' is filled by variable 'computer'. (And you would have to create the variable 'computer' before this line).

You do not have a variable 'computer' anywhere.. and you normally do not put "int" to the right of the 'get' operator.
Was This Post Helpful? 0
  • +
  • -

#11 jon.kiparsky  Icon User is online

  • Chinga la migra
  • member icon


Reputation: 10690
  • View blog
  • Posts: 18,307
  • Joined: 19-March 11

Re: rock paper scissors program

Posted 07 October 2014 - 11:58 AM

I think you really need to review this tutorial and then start again.
Was This Post Helpful? 1
  • +
  • -

#12 melina2134  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 16
  • Joined: 23-September 14

Re: rock paper scissors program

Posted 08 October 2014 - 12:24 PM

i just need to know what's wrong with my code. i've looked at many beginner coding websites that show the if then statements and even then i couldnt fix this. its supposed to play rock paper scissors with the computer however the input "0" is always either a draw or it wont even respond with whether or not the user lost. input "2" you only lose. "1" is always a draw. "0" as well. ???

import java.util.*;
import java.util.Scanner;
import java.lang.Math;

public class Lab3
{
	
		public static void main(String[] args){
		{
			System.out.println("Play rock paper scissors! Choose: scissors (0), rock (1), or paper (2).");
			System.out.println();
		}
		Scanner input = new Scanner(System.in);
		int scissor = 0;
		int rock = 1;
		int paper = 2;
		Random random = new Random();
		int randomIndex = random.nextInt(2) + 0;
		int computer = randomIndex;
		int guess = input.nextInt();
	if (guess == computer){
			System.out.println("It's a draw!");
		}
	if (guess < computer | guess == 2 && computer== 0 ){
			System.out.println("You lost. Better luck next time!");
			}
	if (guess > computer | guess == 0 && computer == 2){
			System.out.println("You won!");
		}
	}
}


Was This Post Helpful? 0
  • +
  • -

#13 macosxnerd101  Icon User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12149
  • View blog
  • Posts: 45,175
  • Joined: 27-December 08

Re: rock paper scissors program

Posted 08 October 2014 - 01:11 PM

Two points.
  • Why are you still not properly indenting your code? Please do this. It will make your code easier for everyone to read and debug; including most importantly, yourself.

  • The bulk of your code is outside the main() method. You should be getting a lot of compilation errors. We have already addressed this issue before.

Was This Post Helpful? 0
  • +
  • -

#14 melina2134  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 16
  • Joined: 23-September 14

Re: rock paper scissors program

Posted 08 October 2014 - 01:14 PM

ALRIGHT HUNTIES IVE BEEN WORKIN MY BOOTY HERES WHATS UP

it runs but when the user tries to play rock paper scissors with the program it will either not respond to user input, 2 will only lose, 1 will only draw, same with 0?!
rest assured ive been trying for hours to fix this and constant revision has gotten me nowhere help and review is very appreciated!

import java.util.*;
import java.util.Scanner;
import java.lang.Math;

public class Lab3
{
	
		public static void main(String[] args){
		{
		System.out.println("Play rock paper scissors! Choose: scissors (0), rock (1), or paper (2).");
		System.out.println();
		}
		Scanner input = new Scanner(System.in);
		int scissor = 0;
		int rock = 1;
		int paper = 2;
		Random random = new Random();
		int randomIndex = random.nextInt(2) + 0;
		int computer = randomIndex;
		int guess = input.nextInt();
	if (guess == computer){
			System.out.println("It's a draw!");
		}
	else {
		if (guess < computer | guess == 2 && computer== 0)
		System.out.println("You lost. Better luck next time!");
		}
	if (guess > computer | guess == 0 && computer == 2)
		{
		System.out.println("You won!");
		}
	}
}


Was This Post Helpful? -1
  • +
  • -

#15 macosxnerd101  Icon User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12149
  • View blog
  • Posts: 45,175
  • Joined: 27-December 08

Re: rock paper scissors program

Posted 08 October 2014 - 01:23 PM

Please stop ignoring our advice. As it turns out, your code was in the main() method, but just formatted so poorly that I didn't realize it. I gave you a -1 because you aren't helping us help you (by refusing to indent your code). I ran your code through an automated formatter so folks can actually read it.

import java.util.*;
import java.util.Scanner;
import java.lang.Math;

public class Lab3
{   
    
    public static void main(String[] args){
        
        {
            
            System.out.println("Play rock paper scissors! Choose: scissors (0), rock (1), or paper (2).");
            System.out.println();
            
        }
        
        Scanner input = new Scanner(System.in);
        int scissor = 0;
        int rock = 1;
        int paper = 2;
        Random random = new Random();
        int randomIndex = random.nextInt(2) + 0;
        int computer = randomIndex;
        int guess = input.nextInt();
        if (guess == computer){
            
            System.out.println("It's a draw!");
            
        }
        
        else {
            
            if (guess &lt; computer | guess == 2 && computer== 0)
            System.out.println("You lost. Better luck next time!");
            
        }
        
        if (guess &gt; computer | guess == 0 && computer == 2)
        {
            
            System.out.println("You won!");
            
        }
        
        
    }
    
    
}



First thing. These braces are unnecessary. Delete them.
        {
            
            System.out.println("Play rock paper scissors! Choose: scissors (0), rock (1), or paper (2).");
            System.out.println();
            
        }



Second thing, the bitwise or operator | is evaluated before the && operator. However, && is evaluated before ||. So using parentheses, the following to conditions are equivalent:
if (guess > computer | guess == 0 && computer == 2){ }

if ( (guess > computer | guess == 0) && computer == 2 ){ } 


Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2