• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 317
  • Last Modified:

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
0
waltbaby315
Asked:
waltbaby315
  • 4
  • 2
1 Solution
 
objectsCommented:
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);
}
}
0
 
objectsCommented:
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);
      }
}

0
 
waltbaby315Author Commented:
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
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
objectsCommented:
see my second comment
0
 
objectsCommented:

> he worksheet I have it should be 13579

change it to use string, that should give you the result you need


public class Examplechapter6ques7 {
      public static void main(String[] args) {

            String result = "";
            for (int i = 1; i <= 9; i += 2)
                  result = result + i;
            System.out.println(result);
      }
}
0
 
waltbaby315Author Commented:
Thanks
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now