代码一:
public int findLast (int[] x, int y) {
for (int i=x.length-1; i > 0; i--)
{
if (x[i] == y)
{
return i;
}
}
return -1;
}
// test: x=[2, 3, 5]; y = 2
// Expected = 0
该例问题出现在for语句,忽略了i=0的情况,导致该函数没有判断数组第一个值。
不会出现fault的例子:
x=[];y=4
出现fault但不出现error的例子:
x=[2,3,5];y=4
出现erros不出现failure的例子:
x=[2,3,5]; y=3
expected:1
代码二:
public static int lastZero (int[] x) {
for (int i = 0; i < x.length; i++)
{
if (x[i] == 0)
{
return i;
}
} return -1;
}
// test: x=[0, 1, 0]
// Expected = 2
该例问题出现在return上,如果是要找数组中最后一个为0的元素位置,应该倒序查找。或者当找到i时不直接return,因为return了,后面就不运行了,这样得到的就是第一个为0的位置.
不会出现fault的例子:
x=[]
出现fault但不出现error的例子:
x=[2,3,5]
excepted:-1
出现erros不出现failure的例子:
x=[2,0,2]
expected:1