Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

sum up numbers

 
Juan Villena
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to check out your loop. It is custom to have the increment part also in the for loop.
 
Winston Gutkowski
Bartender
Pie
Posts: 10430
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic