Link to home
Start Free TrialLog in
Avatar of waltbaby315
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
Avatar of Mick Barry
Mick Barry
Flag of Australia image

you don't declare result


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+i;
System.out.println(""+i);
}
}
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);
      }
}

Avatar of waltbaby315
waltbaby315

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
see my second comment
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks