Results 1 to 10 of 10

Thread: for loop help

  1. #1
    soc86 is offline Member
    Join Date
    Oct 2008
    Posts
    39
    Rep Power
    0

    Default for loop help

    What is wrong with my for loop, i am calling on a method and if x=5 then the program will close? keep getting the error "(" expected


    public static void clearPlayingField() {



    for Tron.ClearGridPoint (x=5){

    Tron.MAX_DIM;
    }// for( x=.....

  2. #2
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    that's not how you write a for loop.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  3. #3
    soc86 is offline Member
    Join Date
    Oct 2008
    Posts
    39
    Rep Power
    0

    Default

    Thank you, any idea how i would edit that if i wanted to use a method in it??

  4. #4
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    how does Tron.ClearGridPoint (x=5) look like? what does it return?
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  5. #5
    soc86 is offline Member
    Join Date
    Oct 2008
    Posts
    39
    Rep Power
    0

    Default

    Below is the method, i am trying to clear the grid if x =5?
    (a bit like the snake game on the old nokia phones)


    public static void ClearGridPoint(int x, int y) {

    grid[x][y]=0;

    if((x==0)&&(y==0)) {
    System.out.println("Upper left of grid cleared");
    }
    if((x==(MAX_DIM-1))&&(y==(MAX_DIM-1))) {
    System.out.println("Lower right of grid cleared");
    }

  6. #6
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    then why don't you just call Tron.ClearGridPoint (x=5). why must you put it in a for loop??

    you can do this:
    if(x==5) {
    Tron.ClearGridPoint(x,y);
    }
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  7. #7
    soc86 is offline Member
    Join Date
    Oct 2008
    Posts
    39
    Rep Power
    0

    Default

    Thats how i understood what i needed to do for my assigmment, i have 5 methods and the one i'am stuck on is required below, do you think i have misunderstood something?

    Five Methods to Edit
    Method : ClearPlayingField()
    The game needs this method to clear-up the playing matrix at startup. Inside this method implement a
    'for-loop-in-a-foor-loop' structure to clear the playing grid using the Tron.ClearGridPoint( x , y
    ); method. If, for example, x holds the value 5 and y the value 6, the grid coordinates [5,6] will
    be cleared. As both 'for' loops run from zero to Tron.MAX_DIM, eventually all grid coordinates will
    be cleared.

  8. #8
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    i'm guessing you need something like this:
    Java Code:
    for(int i = 0; ...){
      for(int j = 0; ...){
        Tron.ClearGridPoint( x , y );
      }
    }
    * fill in the ... using this: "both 'for' loops run from zero to Tron.MAX_DIM"


    remember: if the thread is solved, mark it as solved.
    Last edited by angryboy; 01-28-2009 at 02:43 PM.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  9. #9
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default

    Soc... you can't just throw code together and hope it works... Here's some basics of "for" loops:
    for ( {initialization}; {exit condition}; {incrementor} )
    {code_block;}
    For example, to execute a simple loop 5 times printing out the iteration number each time through the loop:
    Java Code:
    for ( i=1; i<6; i++ ) {
    System.out.println(i);
    }
    *courtesy of tech-recipes
    The for Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)

    I would strongly suggest that you use the following link for further basic Java questions:

    The Java™ Tutorials

    Luck,
    CJSL
    Last edited by CJSLMAN; 01-28-2009 at 03:03 PM. Reason: Add link
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  10. #10
    HaplessNoob is offline Member
    Join Date
    Jan 2011
    Posts
    14
    Rep Power
    0

    Default

    EDIT: ignore this
    Last edited by HaplessNoob; 01-25-2011 at 12:43 PM.

Similar Threads

  1. Need help with a loop
    By ReV13 in forum New To Java
    Replies: 15
    Last Post: 12-01-2008, 04:48 PM
  2. How to use Do While loop
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 07:45 PM
  3. How to use While loop
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 07:44 PM
  4. while loop
    By michcio in forum New To Java
    Replies: 5
    Last Post: 01-27-2008, 12:56 AM
  5. can you help me with this for loop?
    By java_fun2007 in forum New To Java
    Replies: 6
    Last Post: 12-22-2007, 10:20 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •