哪位大侠帮忙看下我的JAVA程序哪错了!!!!!!!!!

2024-11-13 08:35:54
推荐回答(2个)
回答1:

在check()方法那里,把参数改成这样:

void check(int n[]) throws oushu,benshen{
……
}

另外在上面,
for(int i=0;i<7;i++){
c[i]=a[i]/b[i];
}
这里,有可能抛出异常,因为c[]定义为int类型,可以考虑这样写:

c[i]=(int)(a[i]/b[i]);

回答2:

class Even extends Exception{
/**
*
*/
private static final long serialVersionUID = 1090745113674278282L;

public Even(){
System.out.println("结果不是偶数!");
}
}

class Benshen extends Exception{
/**
*
*/
private static final long serialVersionUID = 8972657783401536692L;

public Benshen(){
System.out.println("结果是除数本身!");
}
}

public class CustomException{

public static void main(String[] args){
int a[] = {4, 8, 15, 32, 64, 127, 256};
int b[] = {2, 1, 2, 4, 4, 4, 8};
int c[] = new int[a.length];

for(int i = 0; i < 7; i++){
c[i] = a[i]/b[i];
}
try{
CustomException.check(c);
}catch(Exception e){
System.out.println(e);
}
}

public static void check(int n[]) throws Even,Benshen{
int b[] = {2, 1, 2, 4, 4, 4, 8};
for(int i = 0; i < 7; i++){
System.out.println(n[i]);
if((n[i]%2) != 0){
throw new Even();
}
if(n[i] == b[i]){
throw new Benshen();
}
}
}
}