今天看完毕老师讲的求圆和长方形的面积后,我自己试着重新编写这个程序,现在出现了问题 ,自己看不懂,求高手帮忙
package example;
//求圆和长方形的面积 自定义两种异常,1输入0和负数的异常;2输入大于20的数的异常
public class ExceptionTest1
{
public static void main(String[] args)
{
try
{
YuanXing y = new YuanXing(5);
y.area(); 这儿报错了 Exception in thread "main" java.lang.Error: Unresolved compilation problem:
No enclosing instance of type ExceptionTest1 is accessible. Must qualify the allocation with an enclosing instance of type ExceptionTest1 (e.g. x.new A() where x is an instance of ExceptionTest1).
}
catch(NoValueException e)
{
System.out.println("输入非法值了");
}
}
}
class YuanXing
{
private int banjin;
public static final double PI = 3.14;
YuanXing(int banjin) throws NoValueException
{
if(banjin <= 0)
throw new NoValueException("输入非法值了");
this.banjin = banjin;
}
public void area()
{
System.out.println(banjin * banjin *PI);
}
}
class NoValueException extends Exception
{
NoValueException(String message)
{
super(message);
}
}
|
|