在java

时间:2015-11-16 12:18:56

标签: java

我写了一个代码,我想在抛出异常后继续运行。 这是我的代码:

    public class SquareEquationException extends Exception{
     public SquareEquationException(){
        super("roots are not real numbers");
    }
     public static void SquareEquation() throws SquareEquationException{
         double a,b,c,sqr;
         int flag;
         Scanner in = new Scanner(System.in);
         System.out.println("enter ax^2:");
         a=in.nextDouble();
         System.out.println("enter bx:");
         b=in.nextDouble();
         System.out.println("enter c:");
         c=in.nextDouble();
         sqr=((b*b)-(4*a*c));
         if(sqr<0)
             throw new SquareEquationException();
         else{
          sqr=Math.sqrt(sqr);
          double x1=(-b+sqr)/(2*a);
          double x2 = (-b-sqr)/(2*a);
          if(x1==x2)
                 System.out.println("root is:" + x1);
          else
                 System.out.println("x1 is:"+x1 +"\n"+ "x2 is:"+x2 );

         }
         System.out.println("enter 1 to continue or any key to exit");
         flag = in.nextInt();
         while(flag==1){
             System.out.println("enter ax^2:");
         a=in.nextDouble();
         System.out.println("enter bx:");
         b=in.nextDouble();
         System.out.println("enter c:");
         c=in.nextDouble();
         sqr=((b*b)-(4*a*c));
         if(sqr<0){
             throw new SquareEquationException();
         }
         else{
          sqr=Math.sqrt(sqr);
          double x1=(-b+sqr)/(2*a);
          double x2 = (-b-sqr)/(2*a);
          if(x1==x2)
                 System.out.println("root is:" + x1);
          else
                 System.out.println("x1 is:"+x1 +"\n"+ "x2 is:"+x2 );
         }
         System.out.println("enter 1 to continue or any key to exit");
         flag=in.nextInt();
     }
     }
     public static void main(String[] args) throws SquareEquationException{
         SquareEquation();
     }
}

如何在抛出异常后保持菜单/程序运行? 菜单的工作:如果用户程序插入的1继续运行,则程序退出。

编辑: i want to get the message in red and keep it running, so the menu will show right after/before and offer to continue

2 个答案:

答案 0 :(得分:0)

你也必须抓住例外。将它包裹在试一试中。此外,您还需要使用循环来再次运行代码。如.. ..

while(true){
    try{
       // Code you want to execute that can throw an error£
      SquareEquation();

      // Exit loop if execution ended without an exception
      break;
    }
    catch(SquareEquationException ex){
        // Exception logic (such as showing a message) or just printing the trace (but this doesn't help a user)
        ex.printStackTrace();

    }
}

答案 1 :(得分:0)

你可以将它包装在try catch中。此外,您可以避免重复的代码,并应完成后关闭扫描仪。实施例

import java.util.Scanner;

public class SquareEquationException extends Exception {
    public SquareEquationException() {
        super("roots are not real numbers");
    }

    public static void SquareEquation() throws SquareEquationException {
        double a, b, c, sqr;
        int flag = 1;
        Scanner in = new Scanner(System.in);
        while (flag == 1) {
            try{
                System.out.println("enter ax^2:");
                a = in.nextDouble();
                System.out.println("enter bx:");
                b = in.nextDouble();
                System.out.println("enter c:");
                c = in.nextDouble();
                sqr = ((b * b) - (4 * a * c));
                if (sqr < 0)
                    throw new SquareEquationException();
                else {
                    sqr = Math.sqrt(sqr);
                    double x1 = (-b + sqr) / (2 * a);
                    double x2 = (-b - sqr) / (2 * a);
                    if (x1 == x2)
                        System.out.println("root is:" + x1);
                    else
                        System.out.println("x1 is:" + x1 + "\n" + "x2 is:" + x2);

                }
            } catch(SquareEquationException e){
                System.out.println(e.getMessage());
            }
            System.out.println("enter 1 to continue or any key to exit");
            flag = in.nextInt();
        }
        in.close();
    }

    public static void main(String[] args) throws SquareEquationException {
        SquareEquation();
    }
}