英文:
Why is my test Java method only returning the first key in my list?
问题
public Key equalorLess(Key key) {
if (first == null || first.key.compareTo(key) > 0) return null;
Node x = first;
while (x.next != null && x.next.key.compareTo(key) <= 0) {
x = x.next;
}
return x.key;
}
这段代码用于返回表中最大的键,该键小于或等于输入的键。所以在字符串 "abcd" 中,如果你调用 "e",你会得到 "d"。或者如果你调用 "a",你会得到 "a"。
我正在编写类似以下的测试代码。注意,我知道这有些混乱。
public static void equalorLessTest(String key, String answer) {
String keys = "abcdefijklxyz";
LinkedList<String, Integer> newLst = new LinkedList<String, Integer>();
for (int i = 0; i < keys.length(); i++) {
newLst.put(keys.substring(i, i+1), i);
}
String result = newLst.equalorLessTest(key);
if (answer == null) {
if (result == null)
StdOut.format("equalorLessTest: 正确 字符串 %s 预期结果: null\n", key, result);
else
StdOut.format("equalorLessTest: *错误* 字符串 %s 实际结果: %s\n", key, result);
return;
}
if (result == null && answer != null) {
StdOut.format("equalorLess: *错误* 字符串 %s 实际结果: null\n", key, result);
return;
}
if (result.compareTo(answer) == 0)
StdOut.format("equalorLess: 正确 字符串 %s 实际结果: %s\n", key, result);
else
StdOut.format("equalorLess: *错误* 字符串 %s 实际结果: %s\n", key, result);
}
请注意,这段翻译仅包括你提供的代码部分,不包含任何其他内容。
英文:
public Key equalorLess(Key key) {
if (first == null || first.key.compareTo(key) > 0) return null;
Node x = first;
while(x.next != null && x.next.key.compareTo(key) <= 0) {
x = x.next;
}
return x.key;
}
This is code to return a the largest key in a table that is < or = to inputted key. So in "abcd" if you call "e" you'll return "d". Or if you called "a" you'd get "a".
I'm writing test code that looks like the following. Precursor, I know its a mess.
public static void equalorLessTest( String key, String answer) {
String keys = "abcdefijklxyz";
LinkedList<String,Integer> newLst= new LinkedList<String,Integer>();
for (int i =0; i < keys.length(); i++) {
newLst.put(keys.substring(i, i+1),i);
}
String result = newLst.equalorLessTest(key);
if ( answer == null) {
if (result == null)
StdOut.format("equalorLessTest: Correct String %s Answer: null\n", key,result);
else
StdOut.format("equalorLessTest: *Error* String %s Actual: %s\n", key,result);
return;
}
if (result == null && answer != null ) {
StdOut.format("equalorLess: *Error* String %s Actual: null\n", key,result);
return;
}
if ( result.compareTo(answer) ==0)
StdOut.format("equalorLess: Correct String %s Actual: %s\n", key,result);
else
StdOut.format("equalorLess: *Error* String %s Actual: %s\n", key,result);
}
The only test that works is checking for null. I'm iterating over that test set of keys. So, if I call "b" as my input, I get "a". Every single one I try to test returns "a". And in other test cases, I tend to get nulls or the first key in the given list I create. Can somebody please look at my test method and tell me what's going on. I believe the actual function is right. And it needs to be done in this manner. I can't use alternate methods.
答案1
得分: 1
两个if
分支是相同的:
if (temp != null && x.key.compareTo(temp) >= 0)
temp = x.key;
else
temp = x.key;
这肯定是个错误。
英文:
Both branches of the if
are the same:
if(temp!= null && x.key.compareTo(temp) >= 0)
temp = x.key;
else
temp = x.key;
That’s got to be a bug.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论