版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
实验4Java异常处理一、实验目的1、理解异常的基本概念。2、理解throws、try,catch语句的语法格式。3、理解自定义异常类的定义和使用方法。4、掌握Java的异常处理机制、方法与应用。二、实验任务:1、编写一程序实现成绩输入并校验学生成绩,当用户输入-1时表示输入结束。根据正确输入的成绩平均成绩。成绩用实数表示,如果用户输入一个不正确的成绩,如-80、大于100或字符等,程序应该抛出异常,并捕获异常。请使用try和catch语句实现对输入、计算过程中出现的异常进行处理。2、声明一个异常类,即当除数为零时抛出的异常类DivideByZeroException,并在下列代码需要的地方进行异常抛出和异常捕获,以便用户输入出现错误时,程序具有容错功能。本题异常包括除数为零,以及输入数据格式错误,如数据中包含字母。//DivideByZeroTest.java//Asimpleexceptionhandlingexample.//Checkingforadivide-by-zero-error.importjava.text.DecimalFormat;importjavax.swing.*;importjava.awt.*;importjava.awt.event.*;publicclassDivideByZeroTestextendsJFrameimplementsActionListener{privateJTextFieldinput1,input2,output;privateintnumber1,number2;privatedoubleresult;//InitializationpublicDivideByZeroTest(){super("DemonstratingExceptions");Containerc=getContentPane();c.setLayout(newGridLayout(3,2));c.add(newJLabel("Enternumerator",SwingConstants.RIGHT));input1=newJTextField(10);c.add(input1);c.add(newJLabel("EnterdenominatorandpressEnter",SwingConstants.RIGHT));input2=newJTextField(10);c.add(input2);input2.addActionListener(this);c.add(newJLabel("RESULT",SwingConstants.RIGHT));output=newJTextField();c.add(output);setSize(425,100);show();}//ProcessGUIevents//ThrowinganexceptionwhendataformatiswrongorattemptedtodividebyzeropublicvoidactionPerformed(ActionEvente){DecimalFormatprecision3=newDecimalFormat("0.000");output.setText("");number1=Integer.parseInt(input1.getText());number2=Integer.parseInt(input2.getText());result=quotient(number1,number2);output.setText(precision3.format(result));}//Definitionofmethodquotient.Usedtodemonstrate//throwinganexceptionwhenadivide-by-zeroerror//isencountered.publicdoublequotient(intnumerator,intdenominator){return(double)numerator/denominator;}publicstaticvoidmain(Stringargs[]){DivideByZeroTestapp=newDivideByZeroTest();app.addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){e.getWindow().dispose();System.exit(0);}});}}package
zhidao;import
java.awt.event.ActionEvent;import
java.awt.event.ActionListener;import
javax.swing.BorderFactory;import
javax.swing.JFrame;import
javax.swing.JLabel;import
javax.swing.JTextField;import
javax.swing.JTextArea;import
javax.swing.JButton;import
javax.swing.JScrollPane;import
javax.swing.border.BevelBorder;import
javax.swing.JPanel;public
class
StudentScore
extends
JFrame
implements
ActionListener{
private
JTextField
stuId;
private
JTextField
stuScore;
private
JTextArea
allStudent;
private
JButton
button;
private
final
JPanel
panel
=
new
JPanel();
public
StudentScore()
{
setBounds(200,
100,
244,
630);
getContentPane().setLayout(null);
panel.setBounds(0,
0,
228,
592);
getContentPane().add(panel);
panel.setLayout(null);
JLabel
lblNewLabel
=
new
JLabel("\u8F93\u5165\u5B66\u751F\u6210\u7EE9");
lblNewLabel.setBounds(20,
33,
85,
15);
panel.add(lblNewLabel);
JLabel
label
=
new
JLabel("\u8F93\u5165\u5B66\u751F\u7F16\u53F7");
label.setBounds(20,
8,
85,
15);
panel.add(label);
stuId
=
new
JTextField();
stuId.setBounds(113,
5,
105,
21);
panel.add(stuId);
stuId.setColumns(10);
stuScore
=
new
JTextField();
stuScore.setBounds(113,
33,
105,
21);
panel.add(stuScore);
stuScore.setColumns(10);
button
=
new
JButton("\u5F55\u5165");
button.setBounds(84,
64,
80,
23);
panel.add(button);
allStudent
=
new
JTextArea();
JScrollPane
scrollPane
=
new
JScrollPane(allStudent);
scrollPane.setBounds(20,
97,
198,
485);
panel.add(scrollPane);
button.addActionListener(this);
}
@Override
public
void
actionPerformed(ActionEvent
arg0)
{
//监听,try
catch在这里
String
id
=
stuId.getText();
String
score
=
stuScore.getText();
try{
Double
sco
=
Double.valueOf(score);
if(sco
==
-1){
allStudent.append("录入结束");
button.setEnabled(false);
return;
}
if(sco<0
||
sco>100){
throw
new
Exception();
}
allStudent.append("编号:"+id+",成绩:"+score);
allStudent.append("\n");
}catch(Exception
e){
allStudent.append("编号为"+id+"的学生成绩输入异常");
allStudent.append("\n");
}
}
public
static
void
main(String[]
args)
{
new
StudentScore().setVisible(true);
}}/DivideByZeroTest.java//Asimpleexceptionhandlingexample.//Checkingforadivide-by-zero-error.importjava.text.DecimalFormat;importjavax.swing.*;importjava.awt.*;importjava.awt.event.*;publicclassDivideByZeroTestextendsJFrameimplementsActionListener{privateJTextFieldinput1,input2,output;privateintnumber1,number2;privatedoubleresult;//InitializationpublicDivideByZeroTest(){super("DemonstratingExceptions");Containerc=getContentPane();c.setLayout(newGridLayout(3,2));c.add(newJLabel("Enternumerator",SwingConstants.RIGHT));input1=newJTextField(10);c.add(input1);c.add(newJLabel("EnterdenominatorandpressEnter",SwingConstants.RIGHT));input2=newJTextField(10);c.add(input2);input2.addActionListener(this);c.add(newJLabel("RESULT",SwingConstants.RIGHT));output=newJTextField();c.add(output);setSize(425,100);show();}//ProcessGUIevents//ThrowinganexceptionwhendataformatiswrongorattemptedtodividebyzeropublicvoidactionPerformed(ActionEvente){DecimalFormatprecision3=newDecimalFormat("0.000");output.setText("");number1=Integer.parseInt(input1.getText());number2=Integer.parseInt(input2.getText());result=quotient(
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 《夏洛的网》课外阅读课件
- 《梨园风采新版》课件
- 《键盘和打字指法》课件
- 《雷锋月主题班会》课件
- 2026年代谢综合征诊疗指南考试试卷试题及答案
- 《圆的面积复习》课件
- 2026年儿童保健业务岗位模拟测试题及答案
- 2026年法律法规与劳动法考试(含答案)
- 2026年防物体打击安全培训试题(含答案)
- 2026年机械竞赛模拟试卷(含答案)
- 2026广西壮族自治区交通运输厅直属事业单位重点领域急需紧缺高层次人才招聘39人考试备考题库及答案详解
- 融资渠道2026年融资咨询服务合同
- 2026成人高考专升本语文模拟测试试题及答案
- 2026秋统编版(新教材)九年级历史上册(全册)每课核心知识点清单梳理
- 2026秋小学统编版道德与法治五年级上册教学计划含进度表
- 工程项目部绩效考核实施细则
- GA/T 1999.3-2025道路交通事故车辆速度鉴定方法第3部分:基于视频图像
- brc内审员考试试题及答案
- 2026年联通考试试题及答案
- 云梯车安全专项施工方案
- 2026年上海市闵行区初三语文二模试卷及答案
评论
0/150
提交评论