I want to create a for like this:
for(int i=-0.8;i<2;i+n){
...
}
I do have the error: error: not a statement.
Whats my mistake? Can I do it like this?
Thanks for help.
-Regards
ChrizZly
for(int i=-0.8;i<2;i+n){
Here, i+n
is not valid. You need to assign the new value to i
, so it should be like this
for(int i=-0.8;i<2;i=i+n){
or
for(int i=-0.8;i<2;i+=n){
You also need to modify the type of i
to double
, as int
doesnt have decimals.
for(double i=-0.8;i<2;i+=n){
int i=-0.8;
0.8 is a double value. Should not be assigned to int
for(int i=0;i<2;i++)//to increment i by one
or
for(int i=0;i<2;i+=n)//increment i by n
you can search for compound operators in java.
If you want to use double in loop, for the stopping condition you can use
for(double i = 0.0 ; Double.compare(i, n) > 0 ; i+=n)
Double.compare(d1, d2) // is more precise for comparing double/float values
You are assigning a double
value to an int
. This is invalid. Use:
double i = -0.8
You are not modifying the counter variable ( i
) in the step section. i+n
is a mathematical operation, not an assignment. Use:
i += n
The condition is OK
Final for
loop:
for (double i = -0.8; i < 2; i += n){
//...
}
First problem:
int i=-0.8 (Type mismatch: cannot convert from double to int)
Second problem:
in order to have a correct for statement you have to use an assignment where you wrote i+n
This is a possible compiling snippet of code
double n=10.0;
for(double i=-0.8; i<2.0 ;i=i+n){
....
}
make it int i = 0; or double = -0.8;
int i = -0.8 will be error because 0.8 is not integer. it double. I hope you will get this.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.