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
Printable View
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
You say that you want to print decimal values?
right,
i can't understand what you want to achieve..
Maybe you meant this:
Code:double num=0.1D;
for(int i=-10;i<=10;i++){
System.out.println(num);
num+=0.01D;
}
There are lots of ways to do that, but not much clear what our thread starter wants.
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 :
any ways this could be done simpler?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);
}
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
dno, the code works perfectly in eclipse
what does the second for loop prints?Code:for (double i = -10; i <= 10; i = i + v.get(num)) {
num = num + 1;
System.out.println(i);
}
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.)
is your program prints what you want?
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
Independently of the questions and doubts stated in the above posts, I would like to correct a statements that I made:
The code would probably compile, but would it fail at run time... and the program is the one that would upchuck.Quote:
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.
CJSL
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.
Why is int Num initialized to -1? Try making it equal to zero.. that may be what you are looking for.Code:int num = -1
Luck,
CJSL