在线等~~~ java c语言帝。编写一个应用程序,要求从键盘输入一个double型的圆的半径,计算并输出其面积。

2025-04-07 15:35:21
推荐回答(1个)
回答1:

import java.util.Scanner;
public class Circle{
    public static void main(String[] args){
        double r;
        Scanner s = new Scanner(System.in);
        while(true){
            try{
            r = Double.parseDouble(s.next());
            if(r<0)
                throw new NegativeException();
            else if(r<1.0||r>100.0)
                throw new NumberRangeException();
            else{
                System.out.println("面积是"+3.14*r*r);
                break;
            }
            }catch(NumberFormatException e){
                System.out.println("非数值异常");
            }catch(NegativeException e){
                System.out.println("负数异常");
            }catch(NumberRangeException e){
                System.out.println("越界异常");
            }
        }
    }
}
class NegativeException extends Exception{
    public NegativeException(){
        super();
    }
    public NegativeException(String msg){
        super(msg);
    }
    public NegativeException(String msg, Throwable cause){
        super(msg,cause);
    }
    public NegativeException(Throwable cause){
        super(cause);
    }
}
class NumberRangeException extends Exception{
    public NumberRangeException(){
        super();
    }
    public NumberRangeException(String msg){
        super(msg);
    }
    public NumberRangeException(String msg, Throwable cause){
        super(msg,cause);
    }
    public NumberRangeException(Throwable cause){
        super(cause);
    }
}

望采纳~