Click here to Skip to main content
15,914,943 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello, I am a new java learner and I have got a problem where I can't execute the code. I am trying to return the sum of all odd integers from 1 to n, but I think my code is doing something wrong. It would be wonderful if you check my problems. Thank you so much.

What I have tried:

<pre>
public class Main
{
    public static void main(String[] args) {
        int n;
        int i;
        int sum = 0;
        System.out.println("The sum of odd integer is " + sum + ".");
        
}
public class oddSum
{
    public int sumOfOdd(int n) {
    int sum =0;
    if (n<1){
        return sum;
    }
    for (int i = 1; i <= n; i += 2){
        if (i % 2 != 0){
            sum = sum+i;
        }
    }
    return sum;
}
}
Posted
Updated 19-Sep-21 20:29pm

You never create an instance of your "oddSum" class, nor do you even try to call it's "sumOfOdd" method with any parameters.

All your code does is start the "main" method in your "Main" class, initialize three variables to zero and print a string with the value of "sum", which will always be zero since that's the only value you assign to it.

This is your class work. Debugging this is up to you. I've told you what you've done wrong. It's up to you to figure out how to fix it.
 
Share this answer
 
v2
Comments
CPallini 20-Sep-21 2:26am    
5.
Quote:
but I think my code is doing something wrong.

In fact, the wrong is that your is not trying to do the thing.
Your main do not use the sumOfOdd.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
Comments
CPallini 20-Sep-21 2:26am    
5.
Patrice T 20-Sep-21 2:28am    
Thank you
As a sid note, did you try to write the sum of the first odd numbers on paper (or notepad)? Namely
1              =  1
1 + 3          =  4
1 + 3 + 5      =  9
1 + 3 + 5 + 7  = 16
...
Do you see a pattern?
 
Share this answer
 
Comments
Sarrymint 123 20-Sep-21 17:46pm    
Ohhh I see the pattern. Thank you so much!
CPallini 21-Sep-21 1:56am    
You are welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900