猿问

求教 程序运行不出来,哪里的问题?





public class ych {

public class r 

{

private double radius;

public void g(double r) throws f

{

if(radius<0)

{

throw new f(r);

}

else

{

radius=r;

}

}


}

public class f extends Exception {

double radius;

  f(double r)

  {

  radius=r;

  }

  public String toString()

  {

  return "radius"+this.radius+"r";

  }

  

}


public static void main(String[] args) {

r b=new r();

     try{

     b.g(-2);

     }

     catch(f e){

     System.out.println(""+e.toString()+"");

     }

  }

}

/*

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 


at ych.main(ych.java:32)

*/

慕粉4062053
浏览 1234回答 2
2回答

慕粉3291149

第一:  你的main方法是静态方法,静态方法只能调用静态变量,很显然你的R类并不是静态的,所以你要先将R类设置为static第二: 改了之后你的程序也是没有结果的,你的R类中radius没有初始化,系统默认初始化为0,所以o肯定不会小于0,所以b.g(-2)不管你传进去的参数是多少,都不会抛出异常,更不会输出

慕瓜9220888

ECPHSJZKACBTTBYMJQXCNFSFFWATRCLKARIICQWVQWAHKXLKXGEBFSDCLUDUEUXWOWFSSSJTFUDJYPFWWLKIASFYILALNZXADDGGEALDOHUALTBTVBUHUJBAJLCIFCJIBDLUEEKOHZJOYLOEITHJSIKRRMUPOJZJSFRIEWHHKQNOPEGVSBHRPQSAGSCFBDUDSTGAZGIJIUCLFRHMEGEGZJMVMDYDXJGMJDBOXFWDHBUINIAGDWCXFXFUNJJGJALRCFCOQZNVOJJPZQRIDMBMZCBNOIGYYBURFDFKEHGJTJKLQMFOEBIJIDVSBHBQHTCNKGZWVNFKCLLEXNAICIHKJPVNQYYELMMMRSHZCFPIFPQBADMJPHYKCKKKWGWCKGRHXHEFYDPEKQJGFDYUCPYRYLMKJLGWMDFGXFEDQDCZCUSUDDMQCZRDYVEHRFHTVUQQWJZRETFYBREHRAIATDMDCGMHPCPOYOGBWVOBNXUBXWEXAGDEVQVZZIVIARJVANGDNXYQZRXGXNFGCKTPFCBLGEJEHERDPWFXFSFOLXYDIUKFBBBMOWVIRMJEFRZAMWMYQOGZOETW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