Hello, I'm starting to learn Java and this one of my homework but for some reason it won't run. My algorithm is to just add all 1-1000. I highlighted the part that's causing a problem in red. Please help, thanks!

import java.util.*;
public class thousand
{
    static final Scanner in = new Scanner ( System.in );
    public static void main(String[] args)
    {
        int n = 1000;
        int total = n(n+1)/2;
        System.out.println ("%i" + total);
    }
}

This is the error code:

thousand.java:10: cannot find symbol
symbol  : method n(int)
location: class thousand
        int total = n(n+1)/2;
                    ^
1 error
Member Avatar for ztini

just change it like so

int total = n * (n + 1) / 2;

The compiler is confused by your original code; it thinks you are calling method n(int) with parameter "n+1".

oh i never realized that! thank you!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.