【发布时间】:2020-09-26 01:56:12
【问题描述】:
我希望它可以打印星星。结果是它以我想要的方式打印了星星,但是在测试时出现错误,“ArrayIndexOutOfBoundsException:索引 0 超出长度 0 的范围”。 代码:
public class Printer {
public static void main(String[] args) {
// You can test the method here
int[] array = {5, 1, 3, 4, 2};
printArrayInStars(array);
}
public static void printArrayInStars(int[] array) {
// Write some code in here
int i = 1;
while (i<=array[0]) {
System.out.print("*");
i++;
}
System.out.println("");
int a = 1;
while (a<=array[1]) {
System.out.print("*");
a++;
}
System.out.println("");
int b = 1;
while (b<=array[2]) {
System.out.print("*");
b++;
}
System.out.println("");
int c = 1;
while (c<=array[3]) {
System.out.print("*");
c++;
}
System.out.println("");
int d = 1;
while (d<=array[4]) {
System.out.print("*");
d++;
}
System.out.println("");
}
}
结果:
**
【问题讨论】:
-
可能需要重新安排循环的方式。
-
您的代码在我这边运行良好。我得到以下输出 - ***** * *** **** **
-
你能告诉我们更多你是如何测试你的代码的细节吗?
-
@Numery 我正在使用 tmc bean,当我单击运行项目时,它给了我正确的输出,但是当我单击本地运行测试时,我得到了那个错误。
-
请向我们提供更多信息。您的测试文件中有什么代码?可以查一下吗?
标签: java