它必须是哪种异常? JUnit测试断言错误
What kind of exception it must be? JUnit test Assertion error
请帮助我理解这个案例。前段时间在研究Java,很多时候没看懂。我的程序有一个整数列表输入。它将这个列表转换为二维数组,并在数组内部构建一个像 /\ 这样的锯齿三角形,最小的数字在顶部,从左到右的顺序是 returns 。我还需要创建一个新的 class 来处理错误
private static int[] countSize(int listSize){
int rows = 0;
int columns = 0;
for(int x=1, y=0; x+y<=listSize; x=x+y, y++, rows++);
columns = 2*rows-1;
return new int[] { rows, columns };
}
private static int[][] buildTriangle(List<Integer> list) throws myException{
if(!list.isEmpty()&&!list.contains(null)&&(list.size()<Integer.MAX_VALUE - 8))
{
ArrayList<Integer> workList = new ArrayList<>();
workList.addAll(list);
Collections.sort(workList);
int[] sizes = countSize(list.size());
int[][] resArray = new int[sizes[0]][sizes[1]];
//...sort logic...
System.out.println();
for(int x=0; x<sizes[0]; x++) {
for (int y=0; y<sizes[1]; y++)
System.out.print("\t" + resArray[x][y] + " ");
System.out.println();
}
return resArray;
}
else throw new myException("Input is incorrect");
}
public int[][] buildPyramid(List<Integer> input) {
try{
return buildTriangle(input);
} catch (myException e) {
throw new myException();
}
}
和异常处理class
public class myException extends RuntimeException {
public myException(String message) {
super(message);}
}
当 x 为 256(或其他数字,如 10000)或更大时,JUnit 测试显示断言错误,因为它正在等待异常。不明白这样的数据会导致什么样的异常?我也很高兴听到对代码的评论
这是 7 个输入元素的结果
0 0 0 1 0 0 0
0 0 2 0 3 0 0
0 4 0 5 0 6 0
7 0 0 0 0 0 0
我的猜测是 'able to build pyramid' 表示 'the base needs to be filled'。 1、3、6、10等都是这种情况。
找出该系列的模式,并检查给定的列表是否适合;否则抛出异常。
这是一个不适合的列表:{ 1,2,3,4,5,6,7}。
输出数组:
00100
02340
56700
看到这不是一个合适的金字塔吗?
请帮助我理解这个案例。前段时间在研究Java,很多时候没看懂。我的程序有一个整数列表输入。它将这个列表转换为二维数组,并在数组内部构建一个像 /\ 这样的锯齿三角形,最小的数字在顶部,从左到右的顺序是 returns 。我还需要创建一个新的 class 来处理错误
private static int[] countSize(int listSize){
int rows = 0;
int columns = 0;
for(int x=1, y=0; x+y<=listSize; x=x+y, y++, rows++);
columns = 2*rows-1;
return new int[] { rows, columns };
}
private static int[][] buildTriangle(List<Integer> list) throws myException{
if(!list.isEmpty()&&!list.contains(null)&&(list.size()<Integer.MAX_VALUE - 8))
{
ArrayList<Integer> workList = new ArrayList<>();
workList.addAll(list);
Collections.sort(workList);
int[] sizes = countSize(list.size());
int[][] resArray = new int[sizes[0]][sizes[1]];
//...sort logic...
System.out.println();
for(int x=0; x<sizes[0]; x++) {
for (int y=0; y<sizes[1]; y++)
System.out.print("\t" + resArray[x][y] + " ");
System.out.println();
}
return resArray;
}
else throw new myException("Input is incorrect");
}
public int[][] buildPyramid(List<Integer> input) {
try{
return buildTriangle(input);
} catch (myException e) {
throw new myException();
}
}
和异常处理class
public class myException extends RuntimeException {
public myException(String message) {
super(message);}
}
当 x 为 256(或其他数字,如 10000)或更大时,JUnit 测试显示断言错误,因为它正在等待异常。不明白这样的数据会导致什么样的异常?我也很高兴听到对代码的评论
这是 7 个输入元素的结果
0 0 0 1 0 0 0
0 0 2 0 3 0 0
0 4 0 5 0 6 0
7 0 0 0 0 0 0
我的猜测是 'able to build pyramid' 表示 'the base needs to be filled'。 1、3、6、10等都是这种情况。
找出该系列的模式,并检查给定的列表是否适合;否则抛出异常。
这是一个不适合的列表:{ 1,2,3,4,5,6,7}。
输出数组:
00100
02340
56700
看到这不是一个合适的金字塔吗?