課程
/后端開發
/Java
/Java入門第三季
輸入的是否是整數可以使用判斷結構嗎?
2018-06-18
源自:Java入門第三季 1-2
正在回答
Scanner input=new Scanner(System.in);
if(input.hasNext()) {
???? try{
???????? Integer.valueOf(input.nextLine());#轉為int成功的話,就是整數,轉化失敗就不是整數
???????? System.out.println("你輸入的是整數");
???? }
????catch(Exception e){
???? ???? System.out.println("你輸入的不是整數");
}
/**
?* 學習異常
?* @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() {
* 返回內容
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()
// 打印棧的調用順序圖
// e.printStackTrace();
//? 既然是棧? 一定在同一個線程? ? ?一個線程不可能打印另外一個線程中異常內容。
// 一個線程中的函數絕對不可能調用到其他線程的函數
?* 構造器也需要處理異常
class MyCls{
public MyCls() {
學霸985
你可以使用instanceof關鍵字來判斷是不是整數,例如:
舉報
Java中你必須懂得常用技能,不容錯過的精彩,快來加入吧
1 回答異常處理報錯
3 回答異常處理問題
4 回答異常處理問題
3 回答異常處理的問題
3 回答異常處理的時間
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網安備11010802030151號
購課補貼聯系客服咨詢優惠詳情
慕課網APP您的移動學習伙伴
掃描二維碼關注慕課網微信公眾號
2018-06-18
Scanner input=new Scanner(System.in);
if(input.hasNext()) {
???? try{
???????? Integer.valueOf(input.nextLine());#轉為int成功的話,就是整數,轉化失敗就不是整數
???????? System.out.println("你輸入的是整數");
???? }
????catch(Exception e){
???? ???? System.out.println("你輸入的不是整數");
???? }
}
2018-09-09
/**
?* 學習異常
?* @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;
}
}
2018-06-27
你可以使用instanceof關鍵字來判斷是不是整數,例如: