This site requires JavaScript, please enable it in your browser!
Greenfoot back
Charlie
Charlie wrote ...

2012/5/29

How do i end a java sentence i really need help

Charlie Charlie

2012/5/29

#
Hello my name is charlie i am new to programming i am currently programming a rocket game and you fly around and shoot nukes to kill the thing that trys to attack you i need help
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Rocket here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Rocket extends Actor
{
    /**
     * Act - do whatever the Rocket wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
      if(Greenfoot.isKeyDown("W"))
      move(10);
     
    }
      {  if(Greenfoot.isKeyDown("D"))
      
         setRotation (getRotation() - 5);
        } 
          
    {
       if ("space".equals(Greenfoot.getKey()))
       {   
    
        } 
And it says reached the end of file while parsing and the last bracket is high lighted RED
trash1000 trash1000

2012/5/29

#
Of course it says this. In Java you define blocks. Within these blocks you can call methods, assign values to variables and other stuff. A very easy to remember rule: each block you open needs to be closed (with curly brackets).
public class Rocket extends Actor  
{  
    /** 
     * Act - do whatever the Rocket wants to do. This method is called whenever 
     * the 'Act' or 'Run' button gets pressed in the environment. 
     */  
    public void act() {  
        if(Greenfoot.isKeyDown("W")) {
            move(10);  
        }
        if(Greenfoot.isKeyDown("D"))  {    
            setRotation (getRotation() - 5);  
        }   
        if (Greenfoot.isKeyDown("space")) { 

        }
    }
}   
It should compile like this. P.S.: Each block is embraced with two curly brackets - when you've done everything right you'll have a even number of brackets.
danpost danpost

2012/5/29

#
What is happening, is that you do not have matching brackets. You have an open on 16 and a close at 20 that 'match' (completing your act method) You have an open on 21 (though not considered in a method) and a close at 24 that 'match' You have an open on 28 and a close at 30 that 'match' That leaves the open at 10 and the open at 26 that have no closings. Maybe you had something for the "S" key that you accidentally deleted, leaving one close bracket hanging? Anyway, make sure you enclose properly what code you need where in matching brackets. - all class code should be between matching brackets - each method code should be between matching brackets - each multiple-line 'if', 'for', 'while', 'do', 'switch', etc. group should be between matching brackets
IsVarious IsVarious

2012/5/29

#
Something which may help to visualize the importance of this, is that when you write code, it's as if you were creating a digital fish tank full of water, and when you write code, you have to be sure you open, and close to it the correct way, otherwise bad things can happen. In this case you get compile errors. An example of the proper way to open and close a method would look like this... private void exampleMethod() { The curly bracet to my left, is opening the method. } This curly bracet to my left, is closing the method. Hope this helps.
Charlie Charlie

2012/5/29

#
OMG thank you guys and IsVarious i visualise it and got it now my game is going to be pro and i have learned something
You need to login to post a reply.