
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"); }}
发表评论
最新留言
留言是一种美德,欢迎回访!
[***.207.175.100]2025年04月07日 22时27分09秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
MySQL Explain查看执行计划详解
2019-03-06
IntelliJ IDEA 中,项目文件右键菜单没有svn选项解决办法
2019-03-06
Spring 动态绑定多实现类实例综述
2019-03-06
IDEA 调试Java代码的两个技巧
2019-03-06
MyBatis常见面试题:#{}和${}的区别是什么?
2019-03-06
Vue 数组和对象更新,但视图未更新,背后的故事
2019-03-06
剑指Offer面试题:9.二进制中1的个数
2019-03-06
《你是在做牛做马还是在做主管》- 读书笔记
2019-03-06
ASP.NET Core on K8S学习之旅(12)Ingress
2019-03-06
重新温习软件设计之路(4)
2019-03-06
《刷新》:拥抱同理心,建立成长型思维
2019-03-06
MVC3+NHibernate项目实战(二) :数据库访问层
2019-03-06
Flask入门
2019-03-06
MySQL数据库与python交互
2019-03-06
python如何对字符串进行html转义与反转义?
2019-03-06
开发小白也毫无压力的hexo静态博客建站全攻略 - 躺坑后亲诉心路历程
2019-03-06