import java.io.*; class TooHigh extends Exception{ int score; public TooHigh(String mess,int score){ super(mess); this.score = score; } } class TooLow extends Exception{ int score; public TooLow(String mess,int score){ super(mess); this.score = score; } } public class ShiYan10_1 { public static void main(String[] args){ MyExceptionDemo med=new MyExceptionDemo(); try{ med.getScore(105); }catch (TooHigh e){ e.printStackTrace(); System.out.println(e.getMessage() + "score is:" + e.score); }catch(TooLow e){ e.printStackTrace(); System.out.println(e.getMessage() + " core is:" + e.score); } } public void getScore(int x) throws TooHigh,TooLow{ if(x>100){ TooHigh e = new TooHigh("score>100",x); throw e; } else if(x<0){ TooLow e = new TooLow("score<0",x); throw e; } else System.out.println("score is:"+x); } }怎么更改错误
时间: 2024-02-27 17:56:54 浏览: 129
在这段代码中,编译器提示找不到 `MyExceptionDemo` 类,因为你没有定义这个类。如果你的意图是想使用 `ShiYan10_1` 类来实现这个程序,那么你需要将 `med.getScore(105);` 这一行改为 `getScore(105);`,因为 `getScore` 方法已经是 `ShiYan10_1` 类中的一个方法了。
此外,你还需要将 `MyExceptionDemo` 类的声明和实现添加进来,或者将其替换为 `ShiYan10_1` 类。如果你想使用 `ShiYan10_1` 类,那么你需要将代码中的 `MyExceptionDemo` 改为 `ShiYan10_1`,例如:
```java
public class ShiYan10_1 {
public static void main(String[] args){
ShiYan10_1 med = new ShiYan10_1();
try{
med.getScore(105);
}catch (TooHigh e){
e.printStackTrace();
System.out.println(e.getMessage() + " score is:" + e.score);
}catch(TooLow e){
e.printStackTrace();
System.out.println(e.getMessage() + " score is:" + e.score);
}
}
public void getScore(int x) throws TooHigh,TooLow{
if(x > 100){
TooHigh e = new TooHigh("score>100",x);
throw e;
} else if(x < 0){
TooLow e = new TooLow("score<0",x);
throw e;
} else {
System.out.println("score is:" + x);
}
}
}
```
这样就可以正确地编译和运行这个程序了。
阅读全文