Welcome to Dream.In.Code
Become a Java Expert!

Join 149,493 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,357 people online right now. Registration is fast and FREE... Join Now!




Nested loops for calculating sums of numbers between 2 numbers

 
Reply to this topicStart new topic

Nested loops for calculating sums of numbers between 2 numbers

Bradwick
21 Mar, 2007 - 06:47 PM
Post #1

New D.I.C Head
*

Joined: 21 Mar, 2007
Posts: 11


My Contributions
I'm having some problems writing a method that will take two integers and add up the sum of the integers and all the numbers between them. Ex. the int are 3 and 7, the method does 3+4+5+6+7 and returns 25.

What I have so far is:

CODE
public int sum(int num1, int num2)        
        {
            if(num1<num2)
                for (int sum = num1; num1 <= num2; num1++)
                {
                    sum += num1;
                    return sum;
                }

            
            if (num2>num1)
                for (int sum = num2; num2 <= num1; num2++)
                {
                    sum += num2;
                    return sum;
                }

        }


I still have to write a portion showing what happens when num1 and num2 are equal, but... when I try to compile what I have so far I get the following error:

C:\Documents and Settings\Brad\Desktop\CSE\Geek.java:75: missing return statement
}
^

When I comment that section of my code out it compiles, so I'm sure that's where the problem is but I can't see what return I'm missing (I'm terribly new at java). Am I even close?

Any help would be appreciated.
User is offlineProfile CardPM
+Quote Post

keems21
RE: Nested Loops For Calculating Sums Of Numbers Between 2 Numbers
21 Mar, 2007 - 06:57 PM
Post #2

D.I.C Head
Group Icon

Joined: 3 Feb, 2007
Posts: 183



Thanked: 2 times
Dream Kudos: 25
My Contributions
This is actually pretty good considering that you're new to Java. The problem that you're having is that your return statements are inside of the for loops.

Because of your two if conditionals, you don't take into account that num1 = num2, so if that is the case, neither of the if statements are ever true, and therefore, no value is ever returned.

So here's the solution:
take the two pieces of your code that say, return sum, and put it at the very end of your code (just before the last bracket of the method. This will work because in both of the if conditionals, the value you want to return has the name sum.

One thing you may want to look out for is if num1 = num2, because you may be returning the variable sum, which hasn't ever been initialized. To fix this, I would advise declaring the variable sum in the begining of the method and give it an initial value of either 0, or -1.

Good work for just starting. Keep up the good work.
User is offlineProfile CardPM
+Quote Post

Bradwick
RE: Nested Loops For Calculating Sums Of Numbers Between 2 Numbers
21 Mar, 2007 - 07:01 PM
Post #3

New D.I.C Head
*

Joined: 21 Mar, 2007
Posts: 11


My Contributions
That fixed it right up, I knew it was something simple like that (isn't it always?). Thanks a ton!
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 05:53PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month