1

我的任务说要尝试并抓住这个:

  • 如果用户没有输入至少 3 个令牌
  • 如果用户没有为令牌一和三输入有效数字。

我尝试对其进行研究,但似乎找不到会发生这种情况的异常。现在我的例外似乎适用于上述两个条件,但我的教练希望我有两个捕获,而不是一个。

我希望能够使该程序崩溃,以便我最终可以再次抛出并捕获或修改该程序,以便异常会专门捕获一件事(仅当用户未输入至少 3 个令牌时)。

顺便说一句,如果我尝试执行上述任一操作来使该程序崩溃,它总是会给我错误消息,因此它显然涵盖了我上面的两件事。

我的例外涵盖了我需要捕获的两件事,但我需要两个捕获,而不是一个。

import java.util.NoSuchElementException;
import java.util.Scanner;
import java.util.StringTokenizer;
public class Driver {
    public static void main (String[] args) {
        Scanner sc = new Scanner(System.in);
        StringTokenizer st;
        String input, tok1, tok2, tok3;
        double dtok1, dtok3;
        boolean loop = true;

        //input
        System.out.println("Simple calculator program");
        System.out.println("This program will only except a math expression with");
        System.out.println("2 operands and 1 operator. Exceptable operators");
        System.out.println("are +, -, *, /, and %. For example, 2*3 or 6%4.");
        while (loop) {
            System.out.print("\nplease enter an expression or q to quit: ");
            try {
                input = sc.next();

                if (input.equalsIgnoreCase("q")) {
                    loop = false;
                } else {
                    //calculations
                    st = new StringTokenizer(input,"+-*/%",true);
                    tok1 = st.nextToken();
                    tok2 = st.nextToken();
                    tok3 = st.nextToken();
                    dtok1 = Double.parseDouble(tok1);
                    dtok3 = Double.parseDouble(tok3);
                    System.out.println(input + "=" + 
                    singleExpression(dtok1,dtok3,tok2));
                }
            } catch (Exception NoSuchElementException) {
                System.out.println ("Error! Please enter 2 operands and 1 operator!");
            }
        }
    }

    static double singleExpression (double num1, double num2, String operator) {
        double output = 0.0;
        if (operator.equals("+")) {
            output = num1 + num2;
        } else if (operator.equals("-")) {
            output = num1 - num2;
        } else if (operator.equals("*")) {
            output = num1 * num2;
        } else if (operator.equals("/")) {
            output = num1 / num2;
        } else if (operator.equals("%")) {
            output = num1 % num2;
        } else {
            output = 0;
        }
        return output;
    }
}
4

2 回答 2

1

st.nextToken()NoSuchElementException如果此标记器的字符串中没有更多标记,将抛出 a 。你已经看到了。

Double.parseDoubleNumberFormatException如果字符串不包含可解析的双精度数,将抛出 a 。

2 条评论:

于 2012-05-16T09:19:05.647 回答
1

我认为您使用异常类作为变量名,应该是Class var。

异常会捕获任何东西(数字和少数元素)

try{
..
}  catch (NoSuchElementException nse) {
    System.out.println ("Exception for the String Tokenizer");
}catch (NumberFormatException nfe) {
    System.out.println ("Exception for the Number format");
}
catch (Exception otherException) {
    System.out.println ( "Something else.. " + otherException.getMessage() );
}
于 2012-05-16T22:28:08.000 回答