• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

if one int is less than or greater than another int by one

 
Rene Rad
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to create a boolean that will return true/false based on if one int (which is a variable) is less than or greater than another int (which is a variable) by one. What I'm thinking of coding seems overly complicated and I was wondering if there was something that could do those calculations for me, like a method of some sort or a simple addition to a method. There's nothing in the int java docs that I can find.


Thanks.
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why don't you show us your code and perhaps we can take a look at it.
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sometimes once you have a method signature the implementation writes itself:

 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It can also be helpful to try a simpler problem first. For example, what if you ignore the "or greater than" part? Can you write a method that returns true if one int is less than the other by one? Hint: is there another way to write "less than the other by one"?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15369
40
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't overthink this problem, the solution is very very simple...

The text almost literally says it: check if the difference between two numbers is 1 or -1
Now you'll just have to convert that statement to Java code.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic