
12-01-2008, 10:48 AM
|
|
Member
|
|
Join Date: Dec 2008
Posts: 5
Rep Power: 0
|
|
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
|
|

12-01-2008, 11:08 AM
|
 |
Member
|
|
Join Date: Nov 2008
Location: Austin, TX
Posts: 17
Rep Power: 0
|
|
Originally Posted by ReV13
|
|
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
|
|

12-01-2008, 11:09 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,513
Rep Power: 11
|
|
|
You say that you want to print decimal values?
|
|

12-01-2008, 11:33 AM
|
|
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
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: Ukraine,Zaporozhye
Posts: 486
Rep Power: 2
|
|
Maybe you meant this:
|
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
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,513
Rep Power: 11
|
|
|
There are lots of ways to do that, but not much clear what our thread starter wants.
|
|

12-01-2008, 02:15 PM
|
|
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 :
|
Quote:
|
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?
|
|

12-01-2008, 03:00 PM
|
 |
Moderator
|
|
Join Date: Oct 2008
Location: Mexico
Posts: 1,159
Rep Power: 3
|
|
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.
|
|

12-01-2008, 03:10 PM
|
|
Member
|
|
Join Date: Dec 2008
Posts: 5
Rep Power: 0
|
|
|
dno, the code works perfectly in eclipse
|
|

12-01-2008, 03:14 PM
|
|
Member
|
|
Join Date: Nov 2008
Posts: 34
Rep Power: 0
|
|
|
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?
|
|

12-01-2008, 03:28 PM
|
|
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
|
|
Member
|
|
Join Date: Nov 2008
Posts: 34
Rep Power: 0
|
|
|
is your program prints what you want?
|
|

12-01-2008, 03:30 PM
|
 |
Moderator
|
|
Join Date: Oct 2008
Location: Mexico
Posts: 1,159
Rep Power: 3
|
|
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.
|
|

12-01-2008, 03:44 PM
|
 |
Moderator
|
|
Join Date: Oct 2008
Location: Mexico
Posts: 1,159
Rep Power: 3
|
|
take something back...
Independently of the questions and doubts stated in the above posts, I would like to correct a statements that I made:
|
Quote:
|
|
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.
|
|

12-01-2008, 05:25 PM
|
|
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
|
 |
Moderator
|
|
Join Date: Oct 2008
Location: Mexico
Posts: 1,159
Rep Power: 3
|
|
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.
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT +2. The time now is 06:12 PM.
|
|