所以我的代码看起来像这样:
try {
t.delete("word");
result = t.getRootItem().getWord().equals("humpty");
} catch (Exception e) {
result = false;
}
问题是我的编译器一直说我没有先前的尝试,但我确实有一个先前的尝试,所以有什么问题?这是我的整个主要方法(如果你愿意,我也可以发布整个课程:
public static void main(String args[]) {
BSTRefBased t;
AbstractBinaryTree tt;
int i;
boolean result;
String message;
message = "Test 1: inserting 'word0' -- ";
t = new BSTRefBased();
try {
t.insert("word0");
result = t.getRootItem().getWord().equals("word0");
} catch (Exception e) {
result = false;
}
System.out.println(message + (result ? "passed" : "FAILED"));
message = "Test 2: inserting 'word1', 'word2', 'word3' -- ";
t = new BSTRefBased();
try {
t.insert("word1");
t.insert("word2");
t.insert("word3");
result = t.getRootItem().getWord().equals("word1");
tt = t.detachLeftSubtree();
result &= tt.getRootItem().getWord().equals("word2");
tt = t.detachRightSubtree();
result &= tt.getRootItem().getWord().equals("word3");
} catch (Exception e) {
result = false;
}
System.out.println(message + (result ? "passed" : "FAILED"));
message = "Test 3: deleting 'word3'";
t = new BSTRefBased
try {
t.delete("word3");
result = t.getRootItem().getWord().equals("word3");
} catch (Exception e) {
result = false;
}
System.out.println(message + (result ? "passed" : "FAILED"));
}
答案 0 :(得分:1)
此行似乎不正确:
t = new BSTRefBased
构造函数调用没有()
,并且没有分号。它紧接在try
之前,这些错误必须弄乱解析器,使其不再识别try
。尝试
t = new BSTRefBased(); // or a similar constructor call