执行如下代码后报错信息是:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 6 out of bounds for length 6
at SortArray.main(SortArray.java:21)
package shiyan3import java.io.IOException
public class SortArray {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
int m
// TODO Auto-generated method stub
int n
// TODO Auto-generated method stub
int k
int[] aa = new int[6]
int[] bb = new int[6]
int[] cc = new int[12]
for (int i = 0 i <= 6 i++) {
m = (int) (100 * Math.random())
aa[i] = m
n = (int) (100 * Math.random())
bb[i] = n
System.out.println(aa[i] + " " + bb[i])
}
for (int i = 0 i < 6 i++) {
for (int j = i j < 6 j++) {
if (aa[i] > aa[j]) {
int t = aa[i]
aa[i] = aa[j]
aa[j] = t
}
if (bb[i] > bb[j]) {
int t = bb[i]
bb[i] = bb[j]
bb[j] = t
}
}
}
m = 0
n = 0
k = 0
while ((m == 6) && (n == 6)) {
if (aa[m] <= bb[n]) {
cc[k] = aa[m]
m++
} else {
cc[k] = bb[n]
n++
}
k++
}
while (m == 6) {
cc[k] = aa[m]
m++
k++
}
while (n == 6) {
cc[k] = bb[n]
n++
k++
}
for (int i = 0 i < 12 i++) {
System.out.print(cc[i] + " ")
}
}
}
第21行是从0到6,也就是7次循环,应该把<=6改为<6。
abstract class A {abstract void unfinished()
}
B.抽象方法不能出现在非抽象类中
public abstract class B {abstract void unfinished()
}
C.class 和 abstract 顺序错了
public abstract class C {abstract void unfinished()
}
D.方法没有方法体或缺少abstract关键词,两种改法
abstract class D {protected void unfinished() {
}
}
or
abstract class D {protected abstract void unfinished()
}
对可能出错的方法进行条件判断。修改方式如下:
class Test {public int aaa(int x, int y) {
int result = 0
// 判断
if (y == 0) {
System.out.println("分母不能为零")
} else {
result = x / y
}
return result
}
}
public class TestException {
public static void main(String[] args) {
int result = new Test().aaa(3, 0)
System.out.println("result," + result)
}
}
运行结果:
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)