所以我正在为学校的作业制作一个测试课,它看起来像这样:
public class Tester{
//variables.... some for the object to be tested, others for a Base object to be tested against
public void test(SpecialObject objectToBeTested){
SpecialObject baseObject = new SpecialObject();
int testCase = this.compareObjects(objectToBeTested, baseObject); //compare the two
switch (testCase){
case 1: System.out.println("CASE 1"); break;
case 2: System.out.println("CASE 2"); break;
default: System.out.println("Default, everything is good"); break;
//can't just make this case 0, because I think the switch requires a default
//so how do I pass an int to trigger the default?
}
}
public int compareObject(SpecialObject one, SpecialObject two) {
//compare one.this1 and two.that1
if ( one.this1 = two.that1 ) {return 1;};
//compare one.this2 and two.that2
if ( one.this2 = two.that2 ) {return 2;};
//......
// HERE IS WHERE I AM CONFUSED
// I want to do something like
return default;
//or
return "default";
// but can't because I need to pass an int
}
}
由于我试图将所有比较保留在一种方法中(尽管稍后将抽象为多个方法),因此我试图将整数传递回给test()
switch 方法的整数。为了保持良好的信息,我想告诉自己什么特定的比较失败了,我想我需要将一些东西传递给 switch 方法以使用默认值。如果一切都检查了,我应该从什么返回compareObject(one, two)
来触发 switch 方法中的默认情况。