| Author |
if one int is less than or greater than another int by one
|
Rene Rad
Greenhorn
Joined: Feb 10, 2010
Posts: 15
|
|
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
Joined: Feb 23, 2007
Posts: 1561
|
|
|
Why don't you show us your code and perhaps we can take a look at it.
|
 |
Garrett Rowe
Ranch Hand
Joined: Jan 17, 2006
Posts: 1295
|
|
Sometimes once you have a method signature the implementation writes itself:
|
Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
|
 |
Mike Simmons
Ranch Hand
Joined: Mar 05, 2008
Posts: 2782
|
|
|
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
Bartender
Joined: Aug 16, 2005
Posts: 12929
|
|
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.
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
 |
|
|
subject: if one int is less than or greater than another int by one
|
|
|