Java面向对象编程基础(2)

第1关:接口,异常处理综合应用


import java.util.Scanner;
public class MainClass {
   
    public static Scanner sc=new Scanner(System.in);
    public static void main(String[] args) {
   
        System.out.println("请输入柱体类型");
        System.out.println("0-圆柱体\n1-三菱柱\n2-长方体");
        IShape bottom = null;
        switch (sc.nextInt()) {
   
            //请在begin=end之间补充代码,其他地方补充代码或修改其他代码一律判0分。
/***************begin*************************************/
            case 0:
                bottom=new Circle();
                break;
            case 1:
                bottom=new Triangle();
                break;
            case 2:
                bottom=new Rectangle();
                break;
/***************end*************************************/
            default :
                System.out.println("暂不支持这种柱体!");
                sc.close();
                return;
        }
        System.out.println("请输入该柱体底面属性值:");
        //下面加入异常处理,确保输入的属性值都是合法的值(正数,对于三角形来说,还必须能构成三角形),否则,要求重新输入
        while(true) {
   
            try {
   
                bottom.input(sc);
                break;
            }catch(ShapeException exp) {
   
                System.out.println(exp.getMessage());
                System.out.println("请重新输入属性值:");
            }
        }
        System.out.println("请输入柱体的高度:");
        double height = sc.nextDouble();
        //创建柱体,计算体积
        Cylinder cylinder = new Cylinder(bottom, height);
        String s = String.format("体积=%.2f,表面积=%.2f", cylinder.getVolume(),
                cylinder.getSurfaceArea());
        System.out.printf(s
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值