JAVA的小程序老报错,不知道为什么?谢谢了~~急急急

来源:百度知道 编辑:UC知道 时间:2024/05/12 13:40:05
class D61_OutOfRangeException extends Exception {
}
public class Debug6_1 {
public static final int MAX_X = 1000;
public static final int MIN_X = 10;
private int x;
public int getX() {
return x;
}
public void setX( int value ) {
if ( value >= MIN_X && value <= MAX_X ) {
x = value;
} else {
throw new D61_OutOfRangeException();
}
}
public static void main( String[] args ) {
Debug6_1 a = new Debug6_1();
a.setX( 275 );
System.out.println( a.getX() );
}
}

改成这样就好了
public class Debug6_1 {
public static final int MAX_X = 1000;
public static final int MIN_X = 10;
private int x;
public int getX() {
return x;
}
public void setX( int value ) throws Exception {
if ( value >= MIN_X && value <= MAX_X ) {
x = value;
}
}
public static void main( String[] args ) throws Exception {
Debug6_1 a = new Debug6_1();
a.setX( 275 );
System.out.println( a.getX() );
}
}