課程
/后端開發
/Java
/Java入門第三季
允許用戶在鍵盤依次輸入若干數字(每輸入一個數字都需要按回車鍵確認),程序將計算這些輸的和以及平均值。利用異常類方法,當用戶輸入的數字大于100或小于0時,程序立刻終止執行,并提示這是一個非法的成績數據。
2017-12-27
源自:Java入門第三季 1-4
正在回答
private static final long serialVersionUID = 1L;這是什么意思?
qq_慕UI6139309
import java.util.Scanner;
public class GLmethod {??? public static void main(String[] args) {????? //連續輸入n個數????? System.out.println("請輸入需從鍵盤鍵入的數字個數:");????? Scanner scanner = new Scanner(System.in);???int n = scanner.nextInt();???double sum = 0;???double average = 0;???int m = 0;???//try...catch???try {????//從鍵盤鍵入多個數值????for(int i = 1; i < n + 1 ; i ++) {?????System.out.println("請輸入第" + i + "個數字:");?????Scanner inPut = new Scanner(System.in);?????int j = inPut.nextInt();?????gl(j);??????sum += j ;??????m ++ ;????}???} catch (defException e) {????System.out.println("這是一個非法的成績數據");???}? finally {?????if(m != 0) {?????average = sum / m;? ?? System.out.println("輸入的數和為:" + 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);?}}
舉報
Java中你必須懂得常用技能,不容錯過的精彩,快來加入吧
6 回答java自定義異常類===代碼問題
1 回答老師代碼中 按序號查找圖書方法中 (index-1)更有利于用戶需求
3 回答異常鏈示例代碼注釋
1 回答請問下面的代碼的異常怎么寫,跪求大神解答?。?!
1 回答異常處理1-9的代碼
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網安備11010802030151號
購課補貼聯系客服咨詢優惠詳情
慕課網APP您的移動學習伙伴
掃描二維碼關注慕課網微信公眾號
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) {
????? //連續輸入n個數
????? System.out.println("請輸入需從鍵盤鍵入的數字個數:");
????? Scanner scanner = new Scanner(System.in);
???int n = scanner.nextInt();
???double sum = 0;
???double average = 0;
???int m = 0;
???//try...catch
???try {
????//從鍵盤鍵入多個數值
????for(int i = 1; i < n + 1 ; i ++) {
?????System.out.println("請輸入第" + i + "個數字:");
?????Scanner inPut = new Scanner(System.in);
?????int j = inPut.nextInt();
?????gl(j);
??????sum += j ;
??????m ++ ;
????}
???} catch (defException e) {
????System.out.println("這是一個非法的成績數據");
???}? finally {
?????if(m != 0) {
?????average = sum / m;
? ?? System.out.println("輸入的數和為:" + 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);
?}
}