waltbaby315
asked on
output error
Can someone tell me what Im doing wrong to get this error?
class Examplechapter6ques7
{
public static void main(String[] args)
{
int sum = 0;
int i;
for(int i;i<=9;i+=2)result=result+ i;
System.out.println(""+i);
}
}
today1.JPG
class Examplechapter6ques7
{
public static void main(String[] args)
{
int sum = 0;
int i;
for(int i;i<=9;i+=2)result=result+
System.out.println(""+i);
}
}
today1.JPG
you also deslare i twice
class Examplechapter6ques7 {
public static void main(String[] args) {
int sum = 0;
int result = 0;
for (int i = 0; i <= 9; i += 2)
result = result + i;
System.out.println("" + result);
}
}
class Examplechapter6ques7 {
public static void main(String[] args) {
int sum = 0;
int result = 0;
for (int i = 0; i <= 9; i += 2)
result = result + i;
System.out.println("" + result);
}
}
ASKER
Using Savant first example I got an error. Using Savant second example I got an output of 20, the worksheet I have it should be 13579 and it uses both result = result + i; Any reson why they dont use both?
71.JPG
71.JPG
see my second comment
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks
class Examplechapter6ques7
{
public static void main(String[] args)
{
int sum = 0;
int i;
int result = 0;
for(int i;i<=9;i+=2)result=result+
System.out.println(""+i);
}
}