当前位置: 首页 > news >正文

Problem C: 异常1

1.题目描述

检测年龄,其中若为负数或大于等于200岁皆为异常,请将下列代码补充完整。
// 你的代码将被嵌入这里
class Main{
   public static void main(String[] args){
        Person p1=new Person("John",80);
        Person p2=new Person("peter",210);
        try{p1.isValid();}catch(AgeException e){System.out.println(e.getMessage());}
        try{p2.isValid();}catch(AgeException e){System.out.println(e.getMessage());}
    }
}

2.输出描述

John's age is valid!
peter's age is invalid!

3.代码实现

1.提交的代码

class AgeException extends Exception {public AgeException(String message) {super(message);}
}class Person {private String name;private int age;public Person(String name, int age) {this.name = name;this.age = age;}public void isValid() throws AgeException {if (age < 0 || age >= 200) {throw new AgeException(this.name + "'s age is invalid!");} else {System.out.println(this.name + "'s age is valid!");}}
}

2.完整代码

class AgeException extends Exception {public AgeException(String message) {super(message);}
}class Person {private String name;private int age;public Person(String name, int age) {this.name = name;this.age = age;}public void isValid() throws AgeException {if (age < 0 || age >= 200) {throw new AgeException(this.name + "'s age is invalid!");} else {System.out.println(this.name + "'s age is valid!");}}
}class Main {public static void main(String[] args) {Person p1 = new Person("John", 80);Person p2 = new Person("peter", 210);try {p1.isValid();} catch (AgeException e) {System.out.println(e.getMessage());}try {p2.isValid();} catch (AgeException e) {System.out.println(e.getMessage());}}
}

http://www.dtcms.com/a/184973.html

相关文章:

  • Google语法整理
  • c语言第一个小游戏:贪吃蛇小游戏01
  • 关于在使用getOutputStream()方法后续没有用到write()
  • 【大模型面试每日一题】Day 15:流水线并行的Bubble问题及其缓解方法
  • Python训练营打卡DAY22
  • FramePack AI图片生成视频 v1.1 整合包
  • Vue.js 全局导航守卫:深度解析与应用
  • 2024睿抗编程赛国赛-题解
  • Python-Venv多环境管理
  • 拓扑学在天体物理学的应用:python 示例
  • 模块自动导入的小工具
  • c++类【终章】
  • 2023 河南ccpc
  • Python学习之路(玖)-图像识别的实现
  • TypeScript 类型保护详解
  • Beta分布--贝叶斯建模概率或比例常用分布
  • 游戏逆向开发全阶段电子资料分享 – 从入门到精通实战教程
  • C语言和Python在负数取余运算(%)上的差异
  • AUTOSAR图解==>AUTOSAR_TR_InteractionWithBehavioralModels
  • C++基本知识 —— 缺省参数·函数重载·引用
  • 2025年PMP 学习八 -第6章 项目进度管理
  • 方案精读:华为与中软-智慧园区解决方案技术主打胶片【附全文阅读】
  • 张量并行优质博客
  • AQS(AbstractQueuedSynchronizer)解析
  • Python模块与包以及工程文件管理
  • arctan x 导数推理
  • Spring Cloud : OpenFeign(远程调用)
  • LoRA(Low-Rank Adaptation)原理详解
  • 微服务架构-限流、熔断:Alibaba Sentinel入门
  • 【英语笔记(四)】诠释所有16种英语时态,介绍每种时态下的动词变形!!含有所有时态的的动词变形汇总表格