wood burning stoves*
The moose likes Beginning Java and the fly likes sum up numbers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "sum up numbers" Watch "sum up numbers" New topic
Author

sum up numbers

Juan Villena
Greenhorn

Joined: Aug 27, 2011
Posts: 20
Hi, in this program, supposed to add odd numbers
it works when num1 <= numb2 : "10, 1"
but when you enter num1 >=numb2 : "10,1" does not work and theres no errors
what condition i have to add (when numb1>=numb2) and where i have to locate it.
Thanks



Unnar Björnsson
Ranch Hand

Joined: Apr 30, 2005
Posts: 164
That's because you assume, in your code that num1 is less then or equal to num2.
Take a look at line 15
max() and min() in the Math class might be handy here.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

You need to check out your loop. It is custom to have the increment part also in the for loop.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7526
    
  18

Juan Villena wrote:Hi, in this program, supposed to add odd numbers
it works when num1 <= numb2 : "10, 1"
but when you enter num1 >=numb2 : "10,1" does not work and theres no errors
what condition i have to add (when numb1>=numb2) and where i have to locate it.

Well personally, I'd do it right after they enter the numbers; the question is: what do you do if they enter them in the wrong order?

However, another way to do it without worrying about errors would be to just swap the two values if they're in the wrong order.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
 
 
subject: sum up numbers
 
Similar Threads
Problem with Sort Method
Swap function in java
Erroneous sym type What does this mean?
[HOMEWORK] Please need help with java project
toString() method