為什么只能輸入一個數字,輸入不了第二個數字?

public class Demo extends Exception {//自定義例外,必須繼承Exception或其子類
public Demo() {
super();
}
public Demo(String code) {
super(code);
}
}import java.util.Scanner;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("code:");
Scanner input = new Scanner(System.in);
try {
int code = input.nextInt();
show(code);
} catch (Demo e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch(Exception e) {
e.printStackTrace();
}
System.out.println("程式結束");
}
public static void show(int code) throws Demo{
if(code<1||code>3) {
throw new Demo("課程編碼越界");//使用throws關鍵字宣告例外物件
}else if (code==1) {
System.out.println("c++");
}else if(code==2){
System.out.println("java");
}
else {
System.out.println("mysql");
}
}
}
uj5u.com熱心網友回復:
加個回圈就可以了while(1){
try {
int code = input.nextInt();
show(code);
} catch (Demo e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch(Exception e) {
e.printStackTrace();
}
System.out.println("程式結束");
}
}
uj5u.com熱心網友回復:
稍微改了下,拋出例外就停止public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("code:");
try {
while(true) {
show();
}
} catch (Demo e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch(Exception e) {
e.printStackTrace();
}
System.out.println("程式結束");
}
public static void show() throws Demo{
Scanner input = new Scanner(System.in);
int code = input.nextInt();
if(code<1||code>3) {
throw new Demo("課程編碼越界");//使用throws關鍵字宣告例外物件
}else if (code==1) {
System.out.println("c++");
}else if(code==2){
System.out.println("java");
}
else {
System.out.println("mysql");
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/279087.html
標籤:Java相關
下一篇:關于mysql多表關聯查詢的問題
