Java實驗報告-異常

1.編寫一個應用程序,要求從鍵盤輸入一個double型的圓的半徑,計算並輸出其面積。測試當輸入的數據不是double型數據(如字符串“abc”)會產生什麼結果,怎樣進行異常處理。

import java.util.InputMismatchException;
import java.util.Scanner;

public class Test1 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		final double PI = 3.1415;
		double r = 0;
		double area;
		int k = 0;
		@SuppressWarnings("resource")
		Scanner in = new Scanner(System.in);
		System.out.println("please input the radius of the circle:");
		try{
			r = in.nextDouble();
		}catch(InputMismatchException e){
			System.out.println("InputError");
			k = 1;
		}
		if(k == 0){
			area = PI * r * r;
			System.out.println("the area is:" + area);
		}else {
			System.out.println("InputErro,Please input again");
		}
	}
}

2.計算兩個100以內的正整數之和,當任意一個數超出此範圍時,拋出自己定義的異常(NumberRangeException)。同時再捕獲輸入時發生的異常InputMismatchException,進行嵌套捕獲。


import java.util.InputMismatchException;
import java.util.Scanner;

class NumberRangeException extends Exception {
	public NumberRangeException(){
		super("輸入的數據不在100以內,請檢查數據!");
	}
	public NumberRangeException(String message){
		super(message);
	}
}

public class Test2 {
	@SuppressWarnings({ "unused", "resource" })
	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		int n1,n2,sum;
		n1=input.nextInt();
		n2=input.nextInt();
		if((n1>100||n1<0)||(n2>100||n2<0))
			sum=n1+n2;
		System.out.println("請輸入帶一個整數"+n1);	
		System.out.println("請輸入帶二個整數"+n2);
			try{
			throw new NumberRangeException("輸入的數據不在100以內,請檢查數據!n1="+n1+"n2"+n2);	
	}catch(NumberRangeException e){
		System.out.println(e.getMessage());	
	}catch(InputMismatchException e1){
		System.out.println("輸入的不是整數!");	
	}catch(Exception e2){
		System.out.println("程序發生錯誤,異常終止!");
	}		
	}	
}

  1. 在定義銀行類時,若取錢數大於餘額則作爲異常處理(InsufficientFundsException)。
    思路:
    ① 產生異常的條件是餘額少於取額,因此是否拋出異常要先判斷該條件。
    ② 確定產生異常的方法,應該在取錢方法(withdrawal)中產生異常InsufficientFundsException 。
    ③ 處理異常安排在調用withdrawal的時候,因此withdrawal方法要聲明異常,由上級方法捕獲並處理。
    ④ 要定義好自己的異常
MyException.java
import java.util.*;

public class MyException extends Exception{
	/**
   *
   */
  private static final long serialVersionUID = 1L;
  private int primmoney = 10000;
	private int moneyInded=0;
	private int moneyleft; 
	
	public int getMoneyleft() {
		return moneyleft;
	}
 
	public int setMoneyleft(int primmoney,int moneyInded) {
		return this.moneyleft = primmoney-moneyInded;
	}
 
	//get  set
	public int getPrimmoney() {
		return primmoney;
	}
 
	public void setPrimmoney(int primmoney) {
		this.primmoney = primmoney;
	}
 
	public int getMoneyInded() {
		return moneyInded;
	}
 
	public void setMoneyInded(int moneyInded) {
		this.moneyInded = moneyInded;
	}
 
	
	//有參 無參構造
	public MyException() {
	}
 
	public MyException( int moneyInded) {
		this.moneyInded = moneyInded;
	}
 
	//取錢
	public  void getmoney(int gm) throws MyException{
		if(gm<0||gm>getPrimmoney()){
			throw new MyException(gm);
		}else{
			int leftmoney=setMoneyleft(primmoney, gm);
			System.out.println("現金提取成功!您卡上的餘額爲:"+leftmoney);
		}
		
	}
	
}

Test3.java
import java.util.Scanner;
 
public class Test3 {
	public static void main(String[] args) throws MyException  {
		MyException m1=new MyException();
		System.out.print("請輸入您要提取的金額:");
		Scanner sc=new Scanner(System.in);
		int moneyget=sc.nextInt();
		m1.getmoney(moneyget);
	}
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章