English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I am trying to make a code that sums the first 10 positive integers using a for loop. But when I attempt to compile
for(int i=0; i<10; i++){
it comes up with an error saying it expects a class or interface.

2006-10-02 09:55:04 · 4 answers · asked by snoboarder2k6 3 in Computers & Internet Programming & Design

My teacher is making us use for loops just to learn how to do it

2006-10-02 10:07:28 · update #1

4 answers

That's not the right approach or even the correct code.

Try something that looks like sum = sum + i;

public class Sum {

int i;
int sum=0;

for(int i=0;i<=10;i++) {

sum = sum + i;
}

System.out.printf()
}

As the other poster mentioned - do you know what a Java class structure looks like? Are you aware of what a Java program consists of:

2006-10-02 10:04:28 · answer #1 · answered by Anonymous · 0 0

O it rather is straightforward seem interior your for assertion the place you have x+=index so on the 1st loop x = 0 and index = 0 2nd loop x=0 index = 2 output = 2 third loop x = 2 index = 4 output = 6 do you notice it yet?

2016-10-15 10:58:03 · answer #2 · answered by ? 4 · 0 0

Besides the class laid out in the previous responses, you're going to need a "main" method too. You can find basic tutorials in the link below.

2006-10-03 07:52:39 · answer #3 · answered by Dr.Mr.Ed 5 · 0 0

In Java, all code should belong to a class.

Try something like

public class MyClass {
...
}

2006-10-02 10:13:47 · answer #4 · answered by alex_self 2 · 0 0

fedest.com, questions and answers