即使在java中使用try和catch进行异常处理后如何恢复代码 [英] How to resume code even after exception handling with try and catch in java

查看:30
本文介绍了即使在java中使用try和catch进行异常处理后如何恢复代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序实际上有问题.其实我在学校学习(11年级-大专)所以请用非常简单的语言解释我.我正在开发一个测验,非常基本的学校项目,所以程序会像这样继续......我希望用户通过输入 1-4 的数字来输入他/她的选择.我不希望用户输入字母、字母或特殊字符或任何其他编号.除了 1-4.我尝试使用 try 和 catch 但我的程序在抛出异常后停止.我希望程序代码即使在显示错误消息 System.out.println(" Invalid input. Please enter a number between 1-4"); 后也能运行.示例程序

I am actually having trouble with my program. Actually I study in school (11 grade- junior college) so please explain me in very simple language. I am developing a quiz, very basic school project and so the programs goes on like this.... I want the user to enter his/her choice, by inputting numbers from 1-4. I don't want the user to enter an alphabet, letter or special character or any other no. other than 1-4. I tried using try and catch but my program stops after throwing the exception. I want the program code to run even after displaying the error message System.out.println(" Invalid input. Please enter a number between 1-4"); sample program

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

推荐答案

这里有一个更优雅的解决方案.不涉及尝试捕获.

Here's a more elegant solution to this problem. No try-catch involved.

import java.io.*;
  import java.lang;
  public class
  { 
     //Compares response to the string representations of all numbers between 1 and numOptions, inclusive. 
     //Returns the matching integer or -1 otherwise.
     public static int parseResponse(String response, int numOptions) 
     {
          for(int i=1;i<=numOptions;i++) {
              if(response.equals(Integer.toString(i))) return i;
          }
          return -1;
     }

     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");

         //New code
         int choice=-1;
         while(choice==-1) {
             choice=parseResponse(in.readLine(),2);
             if(choice==-1)
             {
                 System.out.println(" Invalid input. Please enter a number between 1-2");
             }
         }


         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");

             //New code
             int food1=-1;
             while(food1==-1) {
                 food1=parseResponse(in.readLine(),4);
                 if(food1==-1)
                 {
                     System.out.println(" Invalid input. Please enter a number between 1-4");
                 }
             }

             if(food1==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

如果你不知道:

 if(response.equals(Integer.toString(i))) return i;

 if(response.equals(Integer.toString(i)))
 {
      return i;
 }

这篇关于即使在java中使用try和catch进行异常处理后如何恢复代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆