課程
/后端開發(fā)
/Java
/Java入門第三季
輸入的是否是整數(shù)可以使用判斷結(jié)構(gòu)嗎?
2018-06-18
源自:Java入門第三季 1-2
正在回答
Scanner input=new Scanner(System.in);
if(input.hasNext()) {
???? try{
???????? Integer.valueOf(input.nextLine());#轉(zhuǎn)為int成功的話,就是整數(shù),轉(zhuǎn)化失敗就不是整數(shù)
???????? System.out.println("你輸入的是整數(shù)");
???? }
????catch(Exception e){
???? ???? System.out.println("你輸入的不是整數(shù)");
}
/**
?* 學(xué)習(xí)異常
?* @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 {
return 1;
return 2;
}finally{
return 4;
//return 3;
*? ?問返回多少? 2
public static int? TestException2() {
* 返回內(nèi)容
public static int? TestException3() {
//e.printStackTrace();
int j= 100/0;
public static int? TestException4() {
? ? // 錯誤的代碼寫法? 前邊不能包括后邊
/* try {
}catch(Exception e) {
}catch(ArithmeticException e){
}*/
*? 后邊可以包含前邊
}catch(ArithmeticException e) {
return 100;
}catch(Exception e){
* 不會有任何返回會
public static int testException5() {
return 100/0;
System.exit(10);
return 10;
}finally {
return 20;
public static int testException6() {
// 得到的是錯誤詳細信息
// e.getMessage()
// 打印棧的調(diào)用順序圖
// e.printStackTrace();
//? 既然是棧? 一定在同一個線程? ? ?一個線程不可能打印另外一個線程中異常內(nèi)容。
// 一個線程中的函數(shù)絕對不可能調(diào)用到其他線程的函數(shù)
?* 構(gòu)造器也需要處理異常
class MyCls{
public MyCls() {
學(xué)霸985
你可以使用instanceof關(guān)鍵字來判斷是不是整數(shù),例如:
舉報
Java中你必須懂得常用技能,不容錯過的精彩,快來加入吧
3 回答異常處理問題
4 回答異常處理問題
1 回答異常處理報錯
1 回答異常處理的作用
3 回答異常處理的問題
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網(wǎng)安備11010802030151號
購課補貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號
2018-06-18
Scanner input=new Scanner(System.in);
if(input.hasNext()) {
???? try{
???????? Integer.valueOf(input.nextLine());#轉(zhuǎn)為int成功的話,就是整數(shù),轉(zhuǎn)化失敗就不是整數(shù)
???????? System.out.println("你輸入的是整數(shù)");
???? }
????catch(Exception e){
???? ???? System.out.println("你輸入的不是整數(shù)");
???? }
}
2018-09-09
/**
?* 學(xué)習(xí)異常
?* @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;
}
/**
* 返回內(nèi)容
* @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()
// 打印棧的調(diào)用順序圖
// e.printStackTrace();
//? 既然是棧? 一定在同一個線程? ? ?一個線程不可能打印另外一個線程中異常內(nèi)容。
// 一個線程中的函數(shù)絕對不可能調(diào)用到其他線程的函數(shù)
return 10;
}finally {
return 20;
}
}
}
/**
?* 構(gòu)造器也需要處理異常
?* @author zhuzg
?*
?*/
class MyCls{
public MyCls() {
int i = 100/0;
}
}
2018-06-27
你可以使用instanceof關(guān)鍵字來判斷是不是整數(shù),例如: