I've been trying to answer this problem all night, but I think my brain is just too fried from my midterms to answer it correctly. So the question exactly is [quoted]: Write a method highLow that accepts an integer as a parameter and returns whether or not the number has alternating "high" and "low" digits. 0 through 4 are "low" digits and 5 through 9 are "high" digits. Your method should return true if the number passed alternates between "high" and "low" digits, and false if not. You may assume the number passed is positive. If the number passed consists of a single digit, your method should return true.
Note: The method returns true if the number alternates starting with a "high" digit or starting with a "low" digit. What is important is that the digits alternate. For example, both highLow(9292) and highLow(2929) should return true.
Here are some example calls to the method and their resulting return values:
Call Value Returned highLow(1918193) true highLow(7283) true highLow(3827) true highLow(9388) false highLow(895151) false highLow(707) true highLow(44) false highLow(45) true highLow(5) true You may not use a String to solve this problem
And this is my most recent attempt:
public class Practeese {
public static void main(String[] args) {
highLow(1918193);
highLow(7283);
highLow(3827);;
highLow(9388);
highLow(895151);
highLow(707);
highLow(44);
highLow(45);
highLow(5);
}
public static boolean highLow(int n) {
// boolean isHigh = true;
// boolean isLow = true;
boolean test = true;
while (n > 0) {
boolean isHigh = true;
boolean isLow = true;
if (n % 10 >= 5) {
isHigh = true;
} else if (n%10<=5) {
isLow = true;
} else {
return false;
}
n = n / 10;
if (n % 10 == 0 && (isLow!= isHigh)) {
test = true;
} else {
test = false;
}
}
return test;
}
}
I understand that this is a fencepost style of question but I just can seem to solve it.. Any help is appreciated.