Results 1 to 12 of 12
  1. #1
    chris.bos's Avatar
    chris.bos is offline Member
    Join Date
    Dec 2011
    Posts
    7
    Rep Power
    0

    Default A simple question

    Well i don't know how to name the post but this looks good.
    Here is a piece of code!

    Java Code:
    int z=1;
          int j=1;
          label1:
          if (x>1){
              System.out.print("!");
              j++;
              if (j>z){
                  j=1;
                  z++;
                  System.out.println( );
              }
           if (z<=x)
              break label1;
    }


    Obviously you have already understand the problem.Is there another way to do what the "break label" does but for a simple if?

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: A simple question

    Is the code trying to loop under a condition?
    Write the code as a loop that continues looping as per the conditional requirement.

  3. #3
    chris.bos's Avatar
    chris.bos is offline Member
    Join Date
    Dec 2011
    Posts
    7
    Rep Power
    0

    Default Re: A simple question

    Thnx Norm
    but I forgot to mention that I have to wright this whole program without using while, do-while or for.So is there a command to replace
    brake or do I have to find a complete different way to right the program?
    Yes it will loop under condition.
    Last edited by chris.bos; 12-02-2011 at 02:06 PM.

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

    Default Re: A simple question

    Your code looks like it should be in a loop.
    What looping statements are you allowed to use?

    Is this some trick about the way the variables' values are set and changed?
    Can you replace the code on lines 4 to 11 with any code? Say just with an increment of z .

  5. #5
    chris.bos's Avatar
    chris.bos is offline Member
    Join Date
    Dec 2011
    Posts
    7
    Rep Power
    0

    Default Re: A simple question

    Let me explain you cause i see that you are willing to help :D
    it is a project for my uni.He want to make a program but we cant use while,do-while and for or we are going to fail.
    the program asks the user to insert a number (it's on a previous part of the program) an it does this thing
    for x=2
    !
    !!
    for x =5
    !
    !!
    !!!
    !!!!
    !!!!!
    So as you can see i had to make a program that need a loop without been able to use any.And i thought of this trick.I don't want to change the program or have someone to make it for me cause i am very staborn and want to make all things by myself xD.
    I just want to know (because i don't due to luck of experience) if there is a command that sends you some lines back in the program
    (from line 13 to line 3)

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: A simple question

    a command that sends you some lines back in the program
    That sounds like you are looking for a goto. That's banned in java.
    Is the prof looking for a recursive method?

  7. #7
    chris.bos's Avatar
    chris.bos is offline Member
    Join Date
    Dec 2011
    Posts
    7
    Rep Power
    0

    Default Re: A simple question

    Quote Originally Posted by Norm View Post
    Is the prof looking for a recursive method?
    He does not ask for something specific :/
    So i have to find an other way to make it work?
    Pfff that's a mess
    Thnx anyway ;)

  8. #8
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default Re: A simple question

    If you can't use the usual looping methods, I would guess he wants you to use a recursive method. I don't know why for a simple project like this, but he's the teacher.

    Do you know what recursion is?
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  9. #9
    chris.bos's Avatar
    chris.bos is offline Member
    Join Date
    Dec 2011
    Posts
    7
    Rep Power
    0

    Default Re: A simple question

    Quote Originally Posted by Dark View Post
    Do you know what recursion is?
    Since i am learning java in an other language i don't know the meaning of "recursion" but if you mean the method that calls itself i used that and managed to solve the prob.Only when i came up with this method i understood what
    Norm said. xD
    Thnx tho! prob solved!

  10. #10
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default Re: A simple question

    Yes, recursion is essentially a method calling itself at the end. Incrementing a counter and whenever the counter is reached it breaks the recursion cycle. Recursion is essentially looping without using a predefined loop function.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  11. #11
    chris.bos's Avatar
    chris.bos is offline Member
    Join Date
    Dec 2011
    Posts
    7
    Rep Power
    0

    Default Re: A simple question

    i Started learning java about a week ago,just for the project, so i didn't even know hot to make a recursion method.Looking in the book was the best I could do for this situation.Thnx for your help mates.It seems that i should start learning their English names too.

  12. #12
    Rewry is offline Member
    Join Date
    Dec 2011
    Posts
    19
    Rep Power
    0

    Default Re: A simple question

    When someone mentions recursion two things should immediately pop into your head.
    Firstly: Recursive Call
    Secondly: Base Case

    The main idea of recursion is breaking down your problem into "smaller chunks" until they are small enough to solve. Once these small problems are solved, they are traced back up to solve the bigger problem further up in the stack. Sorting algorithms such as the Quick Sort and the Merge Sort are programmed by using this programming technique.

Similar Threads

  1. simple question
    By agater in forum New To Java
    Replies: 5
    Last Post: 11-07-2011, 03:22 AM
  2. Simple question...
    By Onyx in forum New To Java
    Replies: 10
    Last Post: 08-10-2011, 11:44 PM
  3. Simple question
    By Qsc in forum New To Java
    Replies: 6
    Last Post: 03-07-2011, 12:24 AM
  4. Simple Question
    By stackptr89 in forum New To Java
    Replies: 13
    Last Post: 01-29-2011, 06:35 PM
  5. some simple question?
    By jperson in forum New To Java
    Replies: 4
    Last Post: 05-03-2010, 06:32 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
  •