Java入门学习-nefu瑞格练习6
发布日期:2021-05-07 09:08:48 浏览次数:24 分类:精选文章

本文共 4737 字,大约阅读时间需要 15 分钟。

实验5里没有题目

8702

在这里插入图片描述

import java.util.Scanner;class NoAgesException {       NoAgesException(){       }    static void triangle(Person p) throws IllegalArgumentException{           //start        if(p.age > 200 || p.age < 0) {               throw new IllegalArgumentException();        }        //end    }}class Person {       //start    String name;    int age;    public String getName() {           return name;    }    public void setName(String name) {           this.name = name;    }    public int getAge() {           return age;    }    public void setAge(int age) {           this.age = age;    }    Person (String name, int age) {           /*if(age > 200 || age < 0) {            throw new IllegalArgumentException();        }*/        this.name = name;        this.age = age;    }    void print() {           System.out.println(this.name + "..." + this.age);    }//end}public class Main{       public static void main(String[] args) {           Scanner s=new Scanner(System.in);        Person p=new Person(s.next(),s.nextInt());;        try {               //start            NoAgesException ill = new NoAgesException();            ill.triangle(p);            p.print();            //end        }catch (IllegalArgumentException e) {               System.out.println("年龄数值非法");        }    }}

8701

在这里插入图片描述

import java.util.InputMismatchException;import java.util.Scanner;class TestTriangle {       static void triangle(int a, int b,int c) throws IllegalArgumentException,InputMismatchException{           //starts        if(a + b <= c || a + c <= b || b + c <= a) {               throw new IllegalArgumentException();        }        if(a % 1 != 0 || b % 1 != 0 || c % 1 != 0) {               throw new InputMismatchException();        }        //end    }}class Triangle{       int a;    int b;    int c;    Triangle (int a, int b, int c) {           this.a = a;        this.b = b;        this.c = c;    }    void print() {           System.out.println("三角形的三边长为:" + a + "," + b + "," + c);    }}public class Main {       public static void main(String[] args) {           int a=0, b=0, c=0;        Scanner in = new Scanner(System.in);        a = in.nextInt();        b = in.nextInt();        c = in.nextInt();        try{               //starts            TestTriangle testTriangle = new TestTriangle();            testTriangle.triangle(a, b, c);            Triangle triangle = new Triangle(a, b, c);            triangle.print();            //end        }        catch (IllegalArgumentException e) {               System.out.println(a + "," + b + "," + c + "不能构成三角形");        }        catch (InputMismatchException e) {               System.out.println("请输入整数作为三角形的边长!");        }    }}

8700

在这里插入图片描述

import java.util.Scanner;class DangerException extends Exception {       //starts    DangerException (int num) {           System.out.println("超重");        System.out.println("无法再装载重量是" + num + "吨的集装箱");    }    //end}class CargoBoat {       int realContent;  //装载的重量    int maxContent;   //最大装载量    public void setMaxContent(int c) {           maxContent = c;    }    public void loading(int m) throws DangerException {           //starts        if(this.realContent + m > maxContent) {               throw new DangerException(m);        }        this.realContent = this.realContent + m;        System.out.println("目前装载了" + this.realContent + "吨货物");        //end    }}public class Main {       public static void main(String args[]) {           CargoBoat ship = new CargoBoat();        Scanner s=new Scanner(System.in);        ship.setMaxContent(1000);        int m=0;        try{               //starts            while(true) {                   m = s.nextInt();                ship.loading(m);            }            //end        }        catch(DangerException e) {               //starts            //end        }        finally {               System.out.printf("货船将正点启航");        }    }}

8691

在这里插入图片描述

import java.util.Scanner;public class Main {       public static void main(String[] args){           //write code here        Scanner in = new Scanner(System.in);        InputInt inputInt = new InputInt();        try {               inputInt.input();            inputInt.print();        }catch (ErrorException e) {           }finally {               System.out.println("end");        }    }}class InputInt {       int n;    void input() throws ErrorException{           Scanner in = new Scanner(System.in);        if(! in.hasNextInt()) {               throw new ErrorException();        }        else {               n = in.nextInt();        }    }    void print() {           int ans = 10 - this.n;        System.out.println(ans);    }}class ErrorException extends Exception{       ErrorException (){           System.out.println("not int");    }}
上一篇:动态规划-矩阵连乘
下一篇:Java入门学习-nefu瑞格练习4

发表评论

最新留言

留言是一种美德,欢迎回访!
[***.207.175.100]2025年04月07日 22时27分09秒