Reputation: 193
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.
Upvotes: 1
Views: 1197
Reputation: 3556
This worked for me
public class Practeese {
public static void main(String[] args) {
System.out.println(highLow(1918193));
System.out.println(highLow(7283));
System.out.println(highLow(3827));
System.out.println(highLow(9388));
System.out.println(highLow(895151));
System.out.println(highLow(707));
System.out.println(highLow(44));
System.out.println(highLow(45));
System.out.println(highLow(5));
}
public static boolean highLow(int n) {
boolean high = false;
boolean low = false;
while (n > 0) {
if(n % 10 >= 5) {
if(high) {
return false;
} else {
low = false;
high = true;
}
} else {
if(low) {
return false;
} else {
high = false;
low = true;
}
}
n = n / 10;
}
return true;
}
}
Output : true true true false false true false true true
Upvotes: 0
Reputation: 7081
You need to reach false only once and then you can return - because other numbers will not matter. Also you need to check if result is true - compared to the previous number. So you can do it like that:
public static boolean highLow(int n) {
boolean isLastHigh= n % 10 >= 5 ; //First number check - we don't compare it to anything
n=n/10;
// Start checking the next numbers and see if they are high-low-high-low
// if the condition is not met - return false and stop checking. Otherwise keep going
while (n > 0) {
if (n % 10 >= 5) {
if(isLastHigh)
return false; //Two highs in a row
isLastHigh = true;
} else {
if(!isLastHigh)
return false; //Two lows in a row
isLastHigh = false;
}
n = n / 10;
}
return true; //We never returned false so all numbers until now have been high-low-high and the result is true
}
Upvotes: 2