![實驗4 Java異常處理_第1頁](http://file4.renrendoc.com/view/50e6432782763cd6f499ebef98fd0b3a/50e6432782763cd6f499ebef98fd0b3a1.gif)
![實驗4 Java異常處理_第2頁](http://file4.renrendoc.com/view/50e6432782763cd6f499ebef98fd0b3a/50e6432782763cd6f499ebef98fd0b3a2.gif)
![實驗4 Java異常處理_第3頁](http://file4.renrendoc.com/view/50e6432782763cd6f499ebef98fd0b3a/50e6432782763cd6f499ebef98fd0b3a3.gif)
![實驗4 Java異常處理_第4頁](http://file4.renrendoc.com/view/50e6432782763cd6f499ebef98fd0b3a/50e6432782763cd6f499ebef98fd0b3a4.gif)
![實驗4 Java異常處理_第5頁](http://file4.renrendoc.com/view/50e6432782763cd6f499ebef98fd0b3a/50e6432782763cd6f499ebef98fd0b3a5.gif)
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
實驗4Java異常處理一、實驗?zāi)康?、理解異常的基本概念。2、理解throws、try,catch語句的語法格式。3、理解自定義異常類的定義和使用方法。4、掌握Java的異常處理機制、方法與應(yīng)用。二、實驗任務(wù):1、編寫一程序?qū)崿F(xiàn)成績輸入并校驗學生成績,當用戶輸入-1時表示輸入結(jié)束。根據(jù)正確輸入的成績平均成績。成績用實數(shù)表示,如果用戶輸入一個不正確的成績,如-80、大于100或字符等,程序應(yīng)該拋出異常,并捕獲異常。請使用try和catch語句實現(xiàn)對輸入、計算過程中出現(xiàn)的異常進行處理。2、聲明一個異常類,即當除數(shù)為零時拋出的異常類DivideByZeroException,并在下列代碼需要的地方進行異常拋出和異常捕獲,以便用戶輸入出現(xiàn)錯誤時,程序具有容錯功能。本題異常包括除數(shù)為零,以及輸入數(shù)據(jù)格式錯誤,如數(shù)據(jù)中包含字母。//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)
{
//監(jiān)聽,try
catch在這里
String
id
=
stuId.getText();
String
score
=
stuScore.getText();
try{
Double
sco
=
Double.valueOf(score);
if(sco
==
-1){
allStudent.append("錄入結(jié)束");
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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2024-2025學年度九年級物理全冊15.2電流和電路教學設(shè)計2新版新人教版
- 生產(chǎn)技術(shù)員工作總結(jié)
- 一年級班隊上學期工作計劃
- 工地保安部年終工作總結(jié)
- 教育教學工作計劃書
- 景區(qū)酒店合作協(xié)議書范本
- 駕駛員安全協(xié)議書范本
- 學員聽評課記錄表怎么填
- 自相矛盾聽評課記錄表
- 北京4年級數(shù)學試卷
- 長江委水文局2025年校園招聘17人歷年高頻重點提升(共500題)附帶答案詳解
- 2025年湖南韶山干部學院公開招聘15人歷年高頻重點提升(共500題)附帶答案詳解
- 廣東省廣州市番禺區(qū)2023-2024學年七年級上學期期末數(shù)學試題
- 不可切除肺癌放療聯(lián)合免疫治療專家共識(2024年版)j解讀
- 教科版科學六年級下冊14《設(shè)計塔臺模型》課件
- 智研咨詢發(fā)布:2024年中國MVR蒸汽機械行業(yè)市場全景調(diào)查及投資前景預(yù)測報告
- 法規(guī)解讀丨2024新版《突發(fā)事件應(yīng)對法》及其應(yīng)用案例
- JGJ46-2024 建筑與市政工程施工現(xiàn)場臨時用電安全技術(shù)標準
- 信息安全意識培訓課件
- 家譜、宗譜頒譜慶典講話
- 新員工入職登記表
評論
0/150
提交評論