Results 1 to 16 of 16
Thread: Need help with a loop
- 12-01-2008, 10:48 AM #1
Member
- Join Date
- Dec 2008
- Posts
- 5
- Rep Power
- 0
- 12-01-2008, 11:08 AM #2
- 12-01-2008, 11:09 AM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
You say that you want to print decimal values?
- 12-01-2008, 11:33 AM #4
Member
- Join Date
- Nov 2008
- Posts
- 34
- Rep Power
- 0
right,
i can't understand what you want to achieve..
- 12-01-2008, 12:02 PM #5
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 12:05 PM.
- 12-01-2008, 01:23 PM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
There are lots of ways to do that, but not much clear what our thread starter wants.
- 12-01-2008, 02:15 PM #7
Member
- Join Date
- Dec 2008
- Posts
- 5
- Rep Power
- 0
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);
}
- 12-01-2008, 03:00 PM #8
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,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 12-01-2008, 03:10 PM #9
Member
- Join Date
- Dec 2008
- Posts
- 5
- Rep Power
- 0
dno, the code works perfectly in eclipse
- 12-01-2008, 03:14 PM #10
Member
- Join Date
- Nov 2008
- Posts
- 34
- Rep Power
- 0
Java Code:for (double i = -10; i <= 10; i = i + v.get(num)) { num = num + 1; System.out.println(i); }
- 12-01-2008, 03:28 PM #11
Member
- Join Date
- Dec 2008
- Posts
- 5
- Rep Power
- 0
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-01-2008, 03:29 PM #12
Member
- Join Date
- Nov 2008
- Posts
- 34
- Rep Power
- 0
is your program prints what you want?
- 12-01-2008, 03:30 PM #13
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.
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 12-01-2008, 03:44 PM #14
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.
Sorry if this caused any misunderstandings (haven't had my morning coffee yet).
Clarification: the above questions/doubts still stand.
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 12-01-2008, 05:25 PM #15
Member
- Join Date
- Dec 2008
- Posts
- 5
- Rep Power
- 0
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.
- 12-01-2008, 05:48 PM #16
Similar Threads
-
Need help with While Loop
By mrdestroy in forum New To JavaReplies: 14Last Post: 10-20-2008, 03:29 PM -
while loop
By Unknown1369 in forum New To JavaReplies: 5Last Post: 07-08-2008, 11:15 AM -
How to use Do While loop
By Java Tip in forum java.langReplies: 0Last Post: 04-17-2008, 08:45 PM -
How to use While loop
By Java Tip in forum java.langReplies: 0Last Post: 04-17-2008, 08:44 PM -
do...while loop
By eva in forum New To JavaReplies: 16Last Post: 01-31-2008, 07:44 AM
Bookmarks