Results 1 to 4 of 4
  1. #1
    david522 is offline Member
    Join Date
    May 2011
    Posts
    21
    Rep Power
    0

    Default Trouble with recursion if statement.

    Basically I'm using a Jpanel set up. The aim is that there are 7 levels of recursion. Everytime I press a button to increase or decrease the recursion the pattern should change to the corresponding level of recursion. So for example level 1 is one square, level 2 is 4 squares around that square and level 3 is 4 squares around those 4 squares...

    Basically I won't go into detail but the "level" is passed into the class as an int. The starting level of recursion is 1.

    How would I create an if statement to let the method know only to call itself again when the method has been changed up or down?

    Java Code:
    private void drawPattern(Graphics g, int level, double leftX, double topY, double size) {
    if (level ?){
    drawPattern(g, level , leftX, topY,size/2);
    Note I am only interested in the if statement as of now.

  2. #2
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    4

    Default Re: Trouble with recursion if statement.

    I'm guessing you already have an instance variable called level. In that case, couldn't you do something like:
    Java Code:
    if (level != this.level) { ... }

  3. #3
    david522 is offline Member
    Join Date
    May 2011
    Posts
    21
    Rep Power
    0

    Default Re: Trouble with recursion if statement.

    Nope, the code just never executes.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,587
    Rep Power
    25

    Default Re: Trouble with recursion if statement.

    Try debugging the code by adding printlns to show the values of the variables as the code is executed.

    Where does your code change the value of the level variable?

Similar Threads

  1. new to java, trouble with if statement
    By Globular in forum New To Java
    Replies: 8
    Last Post: 05-25-2011, 07:03 PM
  2. Replies: 3
    Last Post: 04-12-2011, 07:44 AM
  3. recursion and tail-recursion differences
    By OptimusPrime in forum New To Java
    Replies: 2
    Last Post: 12-28-2009, 07:26 PM
  4. Help with Recursion and return statement
    By nicolek808 in forum New To Java
    Replies: 3
    Last Post: 09-10-2009, 11:02 AM
  5. Statement or Prepared Statement ?
    By paty in forum JDBC
    Replies: 3
    Last Post: 08-01-2007, 05:45 PM

Posting Permissions

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