这个isNumber错在哪里?# JobHunting - 待字闺中
t*d
1 楼
做了个望上的测试,结果昨天晚上做了,今天被据了。请刷了poj的看看。 这哪里还有
bug?
跑出来的结果是
the number 007 is a number: false
the number 0.5 is a number: true
the number .01 is a number: true
the number 9. is a number: true
the number 1.000 is a number: true
the number 00.5 is a number: false
the number -.005000 is a number: true
我看着没问题
public class IsNumber {
/**
* The IsNumber function takes a String and returns true if that string
is a number, and false otherwise.
* This implementation, however, has several bugs in it. Your task is to
find and fix those bugs.
* Please note that you should be fixing bugs in this implementation,
not implementing your own version.
* Parameters:
* - Numbers should be base-10 only. They may be negative, and may have
decimal portions
* - Numbers should not have any size restrictions (as might be imposed
by the sizes of doubles or longs)
* - Numbers should not have any extra characters, such as whitespace
or letters
* - Numbers should not have leading zeros (007 is a secret agent, not
a number)
* - 0.5, .01, 9., and 1.000 are all numbers, however. 00.5 is not.
* Keep in mind that an engineer will be reviewing your code, and write
it in a way that would pass a peer code review
*/
public static void main(String[] args) {
IsNumber n = new IsNumber();
System.out.println("the number 007 is a number: " + n.isNumber("007"
));
System.out.println("the number 0.5 is a number: " + n.isNumber("0.5"
));
System.out.println("the number .01 is a number: " + n.isNumber(".01"
));
System.out.println("the number 9. is a number: " + n.isNumber("9."));
System.out.println("the number 1.000 is a number: " + n.isNumber("1.
000"));
System.out.println("the number 00.5 is a number: " + n.isNumber("00.
5"));
System.out.println("the number -.005000 is a number: " + n.isNumber(
"-.005000"));
}
public boolean isNumber(String toTest) {
int l = 0;
int r = toTest.length() -1;
while (l <= r && toTest.charAt(l) == ' ') {
l++;
}
if (l > toTest.length() -1) {
return false;
}
while ( r >= l && toTest.charAt(r) == ' ') {
r--;
}
if (toTest.charAt(l) == '+' || toTest.charAt(l) == '-') {
l++;
}
boolean onePoint = false;
boolean hasNonZeroChar = false;
boolean care = true;//if 0 is ahead of . , then we care if
second 0 shows up, if 0 is behind . , then we do not care. 007 is not a
number, 1.007 is.
for (int i = l; i <= r; i++) {
char c = toTest.charAt(i);
if (c == '.') {
if (onePoint)
return false;
onePoint = true;
hasNonZeroChar = true;
care = false;
}
else if (c >= '1' && c <= '9') {
hasNonZeroChar = true;
}
else if (c == '0') {
if (!hasNonZeroChar && care) {
if (i < toTest.length() - 1) {
if (toTest.charAt(i+1) == '.') {
care = false;
continue;
}
else return false;
} else break;
}
}
else if (c == '-' || c == '+') {
return false;
} else {
return false;
}
}
return true;
}
}
bug?
跑出来的结果是
the number 007 is a number: false
the number 0.5 is a number: true
the number .01 is a number: true
the number 9. is a number: true
the number 1.000 is a number: true
the number 00.5 is a number: false
the number -.005000 is a number: true
我看着没问题
public class IsNumber {
/**
* The IsNumber function takes a String and returns true if that string
is a number, and false otherwise.
* This implementation, however, has several bugs in it. Your task is to
find and fix those bugs.
* Please note that you should be fixing bugs in this implementation,
not implementing your own version.
* Parameters:
* - Numbers should be base-10 only. They may be negative, and may have
decimal portions
* - Numbers should not have any size restrictions (as might be imposed
by the sizes of doubles or longs)
* - Numbers should not have any extra characters, such as whitespace
or letters
* - Numbers should not have leading zeros (007 is a secret agent, not
a number)
* - 0.5, .01, 9., and 1.000 are all numbers, however. 00.5 is not.
* Keep in mind that an engineer will be reviewing your code, and write
it in a way that would pass a peer code review
*/
public static void main(String[] args) {
IsNumber n = new IsNumber();
System.out.println("the number 007 is a number: " + n.isNumber("007"
));
System.out.println("the number 0.5 is a number: " + n.isNumber("0.5"
));
System.out.println("the number .01 is a number: " + n.isNumber(".01"
));
System.out.println("the number 9. is a number: " + n.isNumber("9."));
System.out.println("the number 1.000 is a number: " + n.isNumber("1.
000"));
System.out.println("the number 00.5 is a number: " + n.isNumber("00.
5"));
System.out.println("the number -.005000 is a number: " + n.isNumber(
"-.005000"));
}
public boolean isNumber(String toTest) {
int l = 0;
int r = toTest.length() -1;
while (l <= r && toTest.charAt(l) == ' ') {
l++;
}
if (l > toTest.length() -1) {
return false;
}
while ( r >= l && toTest.charAt(r) == ' ') {
r--;
}
if (toTest.charAt(l) == '+' || toTest.charAt(l) == '-') {
l++;
}
boolean onePoint = false;
boolean hasNonZeroChar = false;
boolean care = true;//if 0 is ahead of . , then we care if
second 0 shows up, if 0 is behind . , then we do not care. 007 is not a
number, 1.007 is.
for (int i = l; i <= r; i++) {
char c = toTest.charAt(i);
if (c == '.') {
if (onePoint)
return false;
onePoint = true;
hasNonZeroChar = true;
care = false;
}
else if (c >= '1' && c <= '9') {
hasNonZeroChar = true;
}
else if (c == '0') {
if (!hasNonZeroChar && care) {
if (i < toTest.length() - 1) {
if (toTest.charAt(i+1) == '.') {
care = false;
continue;
}
else return false;
} else break;
}
}
else if (c == '-' || c == '+') {
return false;
} else {
return false;
}
}
return true;
}
}