亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

異常處理:

輸入的是否是整數可以使用判斷結構嗎?

正在回答

3 回答

Scanner input=new Scanner(System.in);

if(input.hasNext()) {

???? try{

???????? Integer.valueOf(input.nextLine());#轉為int成功的話,就是整數,轉化失敗就不是整數

???????? System.out.println("你輸入的是整數");

???? }

????catch(Exception e){

???? ???? System.out.println("你輸入的不是整數");

???? }

}


0 回復 有任何疑惑可以回復我~


/**

?* 學習異常

?* @author zhuzg

?*

?*/

public class TestException {

public static void main(String[] args) {

/*try {

int i = 100/0;

}catch(java.lang.ArithmeticException e){

e.printStackTrace();

}

int[] iArray = new int[10];

iArray[1] = 10;*/

//iArray[1] = new Object();

System.out.println("TestException is " + TestException());

System.out.println("TestException2 is " + TestException2());

System.out.println("TestException3 is " + TestException3());

System.out.println(" testException5 ? result is " + testException5() );?

MyCls mc = new MyCls();

?

}

/**

*? ?問返回多少? ?4

* @return

*/

public static int? TestException() {

// TODO Auto-generated constructor stub

try {

int i = 100/0;

return 1;

}catch(java.lang.ArithmeticException e){

e.printStackTrace();

return 2;

}finally{

return 4;

}

//return 3;

}

/**

*? ?問返回多少? 2

* @return

*/

public static int? TestException2() {

// TODO Auto-generated constructor stub

try {

int i = 100/0;

return 1;

}catch(java.lang.ArithmeticException e){

e.printStackTrace();

return 2;

}

//return 3;

}

/**

* 返回內容

* @return

*/

public static int? TestException3() {

// TODO Auto-generated constructor stub

try {

int i = 100/0;

return 1;

}catch(java.lang.ArithmeticException e){

//e.printStackTrace();

int j= 100/0;

return 2;

}

//return 3;

}

public static int? TestException4() {

// TODO Auto-generated constructor stub

? ? // 錯誤的代碼寫法? 前邊不能包括后邊

/* try {

int i = 100/0;

return 1;

}catch(Exception e) {

}catch(ArithmeticException e){

//e.printStackTrace();

int j= 100/0;

return 2;

}*/

/**

*? 后邊可以包含前邊

*/

try {

int i = 100/0;

return 1;

}catch(ArithmeticException e) {

return 100;

}catch(Exception e){

//e.printStackTrace();

int j= 100/0;

return 2;

}

//return 3;

}

/**

* 不會有任何返回會

* @return

*/

public static int testException5() {

try {

return 100/0;

}catch(Exception e) {

System.exit(10);

return 10;

}finally {

return 20;

}

}

public static int testException6() {

try {

return 100/0;

}catch(Exception e) {

// 得到的是錯誤詳細信息

// e.getMessage()

// 打印棧的調用順序圖

// e.printStackTrace();

//? 既然是棧? 一定在同一個線程? ? ?一個線程不可能打印另外一個線程中異常內容。

// 一個線程中的函數絕對不可能調用到其他線程的函數

return 10;

}finally {

return 20;

}

}


}


/**

?* 構造器也需要處理異常

?* @author zhuzg

?*

?*/

class MyCls{

public MyCls() {

int i = 100/0;

}

}


0 回復 有任何疑惑可以回復我~
#1

學霸985

評論時可以選擇代碼框輸入代碼的,,,
2019-02-22 回復 有任何疑惑可以回復我~

你可以使用instanceof關鍵字來判斷是不是整數,例如:https://img1.sycdn.imooc.com//5b32d42c00017df606300299.jpg




0 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消
Java入門第三季
  • 參與學習       409766    人
  • 解答問題       4543    個

Java中你必須懂得常用技能,不容錯過的精彩,快來加入吧

進入課程

異常處理:

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號