我實際上遇到了我的程序問題。其實我在學校學習(11年級 - 大專),所以請用非常簡單的語言來解釋我。我正在開發一個測驗,非常基礎的學校項目,所以程序就這樣繼續下去......我希望用戶通過輸入1-4的數字輸入他/她的選擇。我不希望用戶輸入字母,字母或特殊字符或任何其他編號。除了1-4之外。我嘗試使用try和catch,但是我的程序在拋出異常後停止。即使在顯示錯誤信息System.out.println(「輸入無效,請在1-4之間輸入一個數字」)之後,我想讓程序代碼運行。 示例程序如何在java中嘗試並捕獲異常處理後恢復代碼
import java.io.*;
import java.lang;
public class
{
public static void main()throws IOException
{
InputStreamReader read =new InputStreamReader (System.in);
BufferedReader in =new BufferedReader (read);
System.out.println(" Select your category");
System.out.println(" 1. Food");
System.out.println(" 2. National");
System.out.println("");
System.out.println(" Enter your choice");
int choice=Integer.parseInt(in.readLine());
if(choice==1)//food category
{
int score = 0;
System.out.println("what are dynamites made?");
System.out.println("1. peanuts");System.out.println("2. grapes");
System.out.println("3. flaxseeds");System.out.println("4. fish");
System.out.println(" Enter your choice");
int food1= Integer.parseInt(in.readline());
if(c1=1)
{ System.out.println(" Correct answer");
score=score+10
}
else
{
System.out.println(" Wronge answer");
score=score+0
}
//then i have the second question with the same format
}
if(choice==2)//natioanl category with the same format as above
{//two question with if else statements in them }
}
}// also help me where to add the try and catch statements
謝謝!這真的很有幫助! –