Results 1 to 16 of 16
  1. #1
    ReV13 is offline Member
    Join Date
    Dec 2008
    Posts
    5
    Rep Power
    0

    Default Need help with a loop

    Hi, Im pretty inexperienced with java and have run into a bit of a problem.

    Basically i need to make a loop starting with -10 that prints out a value after 0.1; 0,11; 0,12; 0,13 etc. until it reaches 10.

    thx

  2. #2
    miss.meli's Avatar
    miss.meli is offline Member
    Join Date
    Nov 2008
    Location
    Austin, TX
    Posts
    17
    Rep Power
    0

    Exclamation

    Quote Originally Posted by ReV13 View Post
    that prints out a value after 0.1; 0,11; 0,12; 0,13 etc. until it reaches 10.
    I understand how you want to loop to go from -10 to 10 but I think I'm still confused on how you want it incremented.

    Meli

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  4. #4
    beginner21 is offline Member
    Join Date
    Nov 2008
    Posts
    34
    Rep Power
    0

    Default

    right,
    i can't understand what you want to achieve..

  5. #5
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    7

    Default

    Maybe you meant this:
    Java Code:
                   double num=0.1D;
    		for(int i=-10;i<=10;i++){
                            System.out.println(num);
    			num+=0.01D;
    			
    		}
    Last edited by serjant; 12-01-2008 at 11:05 AM.

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  7. #7
    ReV13 is offline Member
    Join Date
    Dec 2008
    Posts
    5
    Rep Power
    0

    Default

    Sorry, I tried to be as clear as I could but im kinda new to java terminology (english isnt my native language eighter):P

    Anyways after being stuck for a week, i finally came up with this :

    int num = -1;
    ArrayList <Double> v = new ArrayList<Double>();

    for(double n = 0.1; n < 5;n = n + 0.01 ){
    v.add(n);
    }

    for (double i = -10; i <= 10; i = i + v.get(num)) {
    num = num + 1;
    System.out.println(i);
    }
    any ways this could be done simpler?

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

    Default hhhmmm...

    Let's see...
    • The first for statement is to populate 5 elements in the v arraylist
    • The second for statement... I have no clue what this is supposed to do. To begin with, it won't compile... v.get(-1) will make the compiler upchuck big time.
    • What do you want to do with the second for statement? Print the values of the arraylist?


    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  9. #9
    ReV13 is offline Member
    Join Date
    Dec 2008
    Posts
    5
    Rep Power
    0

    Default

    dno, the code works perfectly in eclipse

  10. #10
    beginner21 is offline Member
    Join Date
    Nov 2008
    Posts
    34
    Rep Power
    0

    Default

    Java Code:
    for (double i = -10; i <= 10; i = i + v.get(num)) {
    num = num + 1;
    System.out.println(i);
    }
    what does the second for loop prints?

  11. #11
    ReV13 is offline Member
    Join Date
    Dec 2008
    Posts
    5
    Rep Power
    0

    Default

    The first loop is to make a "step" to get the second loop running properly. Second one is to print out the actual values I need(-10; -9.9; -9.790...01; -9.670...02; etc.)

  12. #12
    beginner21 is offline Member
    Join Date
    Nov 2008
    Posts
    34
    Rep Power
    0

    Default

    is your program prints what you want?

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

    Default WOW... head spinning...

    Let's see...
    • You don't know what the second for loop's task is
    • ... and you say it's working perfectly... huh... then what is this post for?


    If you want help, please explain the problem you're having, because it would appear that you want help for something that is working correctly and you don't have a clue as to how it works.

    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

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

    Default take something back...

    Independently of the questions and doubts stated in the above posts, I would like to correct a statements that I made:
    To begin with, it won't compile... v.get(-1) will make the compiler upchuck big time.
    The code would probably compile, but would it fail at run time... and the program is the one that would upchuck.
    Sorry if this caused any misunderstandings (haven't had my morning coffee yet).
    Clarification: the above questions/doubts still stand.

    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  15. #15
    ReV13 is offline Member
    Join Date
    Dec 2008
    Posts
    5
    Rep Power
    0

    Default

    Basically, I managed to come up with a working (tho crude) solution to my problem after I made this thread.

    I was just wondering if I could simplify the code. Thx for the help.

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

    Default

    Java Code:
    int num = -1
    Why is int Num initialized to -1? Try making it equal to zero.. that may be what you are looking for.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

Similar Threads

  1. Need help with While Loop
    By mrdestroy in forum New To Java
    Replies: 14
    Last Post: 10-20-2008, 02:29 PM
  2. while loop
    By Unknown1369 in forum New To Java
    Replies: 5
    Last Post: 07-08-2008, 10:15 AM
  3. How to use Do While loop
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 07:45 PM
  4. How to use While loop
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 07:44 PM
  5. do...while loop
    By eva in forum New To Java
    Replies: 16
    Last Post: 01-31-2008, 06:44 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
  •