第五次实训作业

1. 编写一个类ExceptionTest,在main方法中使用try-catch-finally语句结构实现:

²  在try语句块中,编写两个数相除操作,相除的两个操作数要求程序运行时用户输入;

²  在catch语句块中,捕获被0除所产生的异常,并输出异常信息;

在finally语句块中,输出一条语句

package practice5;
import java.util.Scanner;
public class ExceptionTest {

    
    public static void main(String[] args) {
           Scanner input=new Scanner(System.in);
           System.out.println("请输入除数:");
           int o1=input.nextInt();
           System.out.println("请输入被除数:");
           int o2=input.nextInt();
           int result=0;
           try{
           result=o2/o1;
          }catch(ArithmeticException e){
              e.printStackTrace();
              System.out.println("异常");
           }finally{
     
           }
           System.out.println(result);
    }

}

1. 编写一个应用程序,要求从键盘输入一个double型的圆的半径,计算并输出其面积。测试当输入的数据不是double型数据(如字符串“abc”)会产生什么结果,怎样处理。

 

package practice5;
import java.util.InputMismatchException;
import java.util.Scanner;
public class yuan {
    static double p=3.14;
    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
        System.out.println("请输入圆的半径:");
        double s=0;
        double r=0;
        try{
         r=input.nextDouble();
        }catch(InputMismatchException e){
             e.printStackTrace();
             System.out.println("非数字异常");
             }
        catch(NegativeArraySizeException e){
             e.printStackTrace();
             System.out.println("负数异常");
        }
        s=p*r*r;
        System.out.println(s);
    }

}

1. 编写一个类ExceptionTest,在main方法中使用try-catch-finally语句结构实现:

²  在try语句块中,编写两个数相除操作,相除的两个操作数要求程序运行时用户输入;

²  在catch语句块中,捕获被0除所产生的异常,并输出异常信息;

在finally语句块中,输出一条语句

package practice5;
import java.util.Scanner;
public class ExceptionTest {

    
    public static void main(String[] args) {
           Scanner input=new Scanner(System.in);
           System.out.println("请输入除数:");
           int o1=input.nextInt();
           System.out.println("请输入被除数:");
           int o2=input.nextInt();
           int result=0;
           try{
           result=o2/o1;
          }catch(ArithmeticException e){
              e.printStackTrace();
              System.out.println("异常");
           }finally{
     
           }
           System.out.println(result);
    }

}

1. 编写一个应用程序,要求从键盘输入一个double型的圆的半径,计算并输出其面积。测试当输入的数据不是double型数据(如字符串“abc”)会产生什么结果,怎样处理。

 

package practice5;
import java.util.InputMismatchException;
import java.util.Scanner;
public class yuan {
    static double p=3.14;
    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
        System.out.println("请输入圆的半径:");
        double s=0;
        double r=0;
        try{
         r=input.nextDouble();
        }catch(InputMismatchException e){
             e.printStackTrace();
             System.out.println("非数字异常");
             }
        catch(NegativeArraySizeException e){
             e.printStackTrace();
             System.out.println("负数异常");
        }
        s=p*r*r;
        System.out.println(s);
    }

}

public class Testshiyan {
    public static void main(String[] args){
        shiyan s1=new shiyan();
          s1.setl(2);
          s1.setw(3);
          s1.mianji();
         cft s2=new cft(2,5,7);
         s2.tiji();
   
    }
}

class Vehicle{

int wheels;
double weight;
public Vehicle() {
this .weight= 4 ;
this .wheels= 100 ;
}
public void myprint1() {
System.out.println( "车轮个数为:" +weight+ "车重为:" +wheels+ "kg" );
}
}
class Car extends Vehicle{
int loader;
public Car() {
this .loader= 4 ;
}
public void myprint2() {
System.out.println( "载人数为:" +loader);
}
}
class Truck extends Car{
int payload;
public Truck() {
this .payload= 50 ;
}
public void myprint3() {
System.out.println( "载重量为:" +payload+ "kg" );
}
}
public class Test {
public static void main(String[] args) {
Truck x1= new Truck();
System.out.println( "本车" );
x1.myprint1();
x1.myprint2();
x1.myprint3();
}

转载于:https://www.cnblogs.com/xsz623/p/10941522.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值