• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
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.
 
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.
 
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:

 
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"?
 
Java Cowboy
Sheriff
Posts: 16079
88
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.
 
Today you are you, that is turer than true. There is no one alive who is youer than you! - Seuss. Tiny ad:
The WEB SERVICES and JAX-RS Course
https://coderanch.com/t/690789/WEB-SERVICES-JAX-RS
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!