利用異常類方法?。。?!求代碼求代碼
允許用戶在鍵盤依次輸入若干數(shù)字(每輸入一個(gè)數(shù)字都需要按回車鍵確認(rèn)),程序?qū)⒂?jì)算這些輸?shù)暮鸵约捌骄?。利用異常類方法,?dāng)用戶輸入的數(shù)字大于100或小于0時(shí),程序立刻終止執(zhí)行,并提示這是一個(gè)非法的成績數(shù)據(jù)。
允許用戶在鍵盤依次輸入若干數(shù)字(每輸入一個(gè)數(shù)字都需要按回車鍵確認(rèn)),程序?qū)⒂?jì)算這些輸?shù)暮鸵约捌骄?。利用異常類方法,?dāng)用戶輸入的數(shù)字大于100或小于0時(shí),程序立刻終止執(zhí)行,并提示這是一個(gè)非法的成績數(shù)據(jù)。
2017-12-27
舉報(bào)
2017-12-28
private static final long serialVersionUID = 1L;這是什么意思?
2017-12-28
import java.util.Scanner;
public class GLmethod {
?
?? public static void main(String[] args) {
????? //連續(xù)輸入n個(gè)數(shù)
????? System.out.println("請輸入需從鍵盤鍵入的數(shù)字個(gè)數(shù):");
????? Scanner scanner = new Scanner(System.in);
???int n = scanner.nextInt();
???double sum = 0;
???double average = 0;
???int m = 0;
???//try...catch
???try {
????//從鍵盤鍵入多個(gè)數(shù)值
????for(int i = 1; i < n + 1 ; i ++) {
?????System.out.println("請輸入第" + i + "個(gè)數(shù)字:");
?????Scanner inPut = new Scanner(System.in);
?????int j = inPut.nextInt();
?????gl(j);
??????sum += j ;
??????m ++ ;
????}
???} catch (defException e) {
????System.out.println("這是一個(gè)非法的成績數(shù)據(jù)");
???}? finally {
?????if(m != 0) {
?????average = sum / m;
? ?? System.out.println("輸入的數(shù)和為:" + sum + "\n平均值為:" + average);
?????}
????? }
????? }
?
???? public static int gl(int j) {
??? ? if(j >= 100 || j <=0) {?
????? throw new defException ("程序終止");
???? }?
??return 1 ;
???? }
}
public class defException extends RuntimeException {
?private static final long serialVersionUID = 1L;
???
?public defException() {}
?
?public defException(String message) {
??super(message);
?}
}