异常处理:我无法在Java和Continue中捕获异常

时间:2013-08-13 14:20:49

标签: java exception-handling

我已经看到了其他答案但仍然在使用多个try catch块之后,我的程序在第一个异常时终止。我希望它除了所有四个变量值,然后处理任何异常,使用我保存在每个catch块中的标志。

这是我的代码:

import java.text.DecimalFormat;
import java.util.Scanner;

public class Brokerage2 {
   public static void main(String[] args) {
      float bro = 0, buy = 0, sell = 0;
      int quant = 0, flag = 0;
      double subtot, pro, bb, bst, sb, ssst, seltot, buytot, surcharge;
      Scanner sc = new Scanner(System.in);
      try {
         bro = sc.nextFloat();
         if (bro > 0.03 || bro < 0.0) {
            flag = 1;
         }
      } catch (Exception e) {
         flag = 1;
      }
      try {
         buy = sc.nextFloat();
      } catch (Exception e) {
         flag = 1;
      }
      try {
         sell = sc.nextFloat();
      } catch (Exception e) {
         flag = 1;
      }
      try {
         quant = sc.nextInt();
      } catch (Exception e) {
         flag = 1;
      }
      bb = (buy * quant) * bro * 0.01;
      bst = 0.1036 * bb;
      buytot = bst + bb;
      sb = sell * quant * bro * 0.01;
      ssst = sell * quant * 0.00025;
      seltot = sb + ssst + bst;
      surcharge = ((buy * quant) + (sell * quant)) * 0.00006;
      subtot = surcharge + buytot + seltot;
      pro = (sell - buy) * quant;
      if (flag == 1) {
         System.out.println("Invalid Input");
      } else if (pro > subtot) {
         System.out.println("profit");
         System.out.println(Double.parseDouble(new DecimalFormat("##.##").format(pro - subtot)));
      } else if (subtot > pro) {
         System.out.println("loss");
         System.out.println(Double.parseDouble(new DecimalFormat("##.##").format(subtot - pro)));
      }
   }
}

1 个答案:

答案 0 :(得分:1)

如果扫描仪收到无效输入,则不再等待输入。对多个输入使用相同的扫描仪不是一个好主意。而是每次创建一个新的扫描仪,它将等待每个扫描仪的输入。

import java.text.DecimalFormat;

import java.util.Scanner;

public class brokerage2 {

    public static void main(String[] args) {
        float bro = 0, buy = 0, sell = 0;
        int quant = 0, flag = 0;
        double subtot, pro, bb, bst, sb, ssst, seltot, buytot, surcharge;

        Scanner sc = new Scanner(System.in);
        try {
            System.out.println("Enter float: ");
            bro = sc.nextFloat();
            if (bro > 0.03 || bro < 0.0) {
                flag = 1;

            }

        } catch (Exception e) {
            flag = 1;

        }

        try {
            sc = new Scanner(System.in);
            System.out.println("Enter float: ");
            buy = sc.nextFloat();

        }

        catch (Exception e) {
            flag = 1;
        }
        try {
            sc = new Scanner(System.in);
            System.out.println("Enter float:");
            sell = sc.nextFloat();
        } catch (Exception e) {
            flag = 1;
        }
        try {
            sc = new Scanner(System.in);
            System.out.println("Enter int: ");
            quant = sc.nextInt();

        } catch (Exception e) {
            flag = 1;
        }

        bb = (buy * quant) * bro * 0.01;
        bst = 0.1036 * bb;
        buytot = bst + bb;

        sb = sell * quant * bro * 0.01;
        ssst = sell * quant * 0.00025;
        seltot = sb + ssst + bst;

        surcharge = ((buy * quant) + (sell * quant)) * 0.00006;

        subtot = surcharge + buytot + seltot;

        pro = (sell - buy) * quant;
        if (flag == 1) {
            System.out.println("Invalid Input");
        }

        else if (pro > subtot) {
            System.out.println("profit");
            System.out.println(Double.parseDouble(new DecimalFormat("##.##")
                    .format(pro - subtot)));
        }

        else if (subtot > pro) {
            System.out.println("loss");
            System.out.println(Double.parseDouble(new DecimalFormat("##.##")
                    .format(subtot - pro)));

        }

    }
}