0

我在交给我的 Java 计算器作业时遇到了一些问题。我被告知要制作一个计算器,它可以执行非常基本的功能,捕获异常并允许您立即更正操作数或运算符的值(这是我遇到的问题)。例如,这应该在控制台中发生:

j * 6
Catch exception and print error message here and asking for new first operand
4
Answer: 4 * 6 = 24

或者

8 h 9
Catch exception and print error message here asking for new operator
+
Answer: 8 + 9 = 17

这段代码是我到目前为止所拥有的:

import java.util.*;
public class Calculator{

static int _state = 3;

public static void main(String[] args){

_state = 3;

System.out.println("Usage: operand1 operator operand2");
System.out.println(" (operands are integers)");
System.out.println(" (operators: + - * /");
@SuppressWarnings("resource")
Scanner in = new Scanner(System.in);
do{

try{
int result = 0;
int operand1 = 0;
int operand2 = 0;

String operator = "";
char op = ' ';

operand1 = in.nextInt();

operator = in.next();
op = operator.charAt(0);
operand2 = in.nextInt();

switch (op){

    default:
        System.out.println("You didn't insert a proper operator");
        break;
    case '+': result = operand1 + operand2;
        System.out.println("Answer: " + operand1 + ' ' + op + ' ' + operand2 + " = " + result );
        break;

    case '-': result = operand1 - operand2;
        System.out.println("Answer: " + operand1 + ' ' + op + ' ' + operand2 + " = " + result );
        break;

    case '*': result = operand1 * operand2;   
        System.out.println("Answer: " + operand1 + ' ' + op + ' ' + operand2 + " = " + result );
        break;

    case '/': result = operand1 / operand2;   
        System.out.println("Answer: " + operand1 + ' ' + op + ' ' + operand2 + " = " + result );
        break;
        }

}
    catch(ArithmeticException e){
       System.out.println("You can not divide by zero. Input a valid divider.");
    }
    catch (InputMismatchException e) {
      System.out.println("You must use proper numerals.");
  }   
} while(_state == 3);

}
}
4

3 回答 3

0

拥有一个可以捕获所有内容的巨大 try 块被认为是不好的做法。将 try catch 块放在您需要的地方。

人们也不喜欢为他们做别人的功课。如果您有特定的问题或疑问,那很好,但是说“我必须为一项任务做这件事,我该怎么做”可能不会奏效。

于 2013-07-24T15:16:24.400 回答
0

在完成此操作之前,您需要做一些事情。我建议通过扩展 Exception 类(或 Exception 类的子类)来创建自己的异常。您可以通过阅读以下内容来做到这一点:

如何在 Java 中创建自定义异常?

现在假设您创建了 MissingOperatorException,那么您可以将它扔到 Switch/Case 语句中。

try{
    switch (op){

        default:
            System.out.println("You didn't insert a proper operator");
            throw MissingOperatorException;
            break;
        //some more code here
    }
}
//reach catch statements
catch(MissingOperatorException e){
   System.out.println("ERROR MESSAGE");
   //deal with case here by asking for new input
   //use scanner to get operand
   //you can do this by reusing code from above
   System.out.println("Please re-enter operand");
   operator = in.next();
   op = operator.charAt(0);
   //calculate answer again
   //print out answer
}

对于需要执行的每种异常类型,您都需要执行此操作。我看到三种可能的情况。

  1. 缺少运算符
  2. 缺少左操作数
  3. 缺少右操作数

可能存在这些问题的组合,但您编写的异常处理应该能够处理它(一个接一个)。

注意:您已使用 Scanner 类来读取用户输入。我假设您了解 Scanner 的基础知识,但您的后续问题与此相矛盾。我真的会研究 Java Scanner 类。请检查一下:

http://docs.oracle.com/javase/tutorial/essential/io/scanning.html

于 2013-07-24T14:52:08.997 回答
0

你应该试试这个

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        boolean ret = true;
        while(ret){

        Scanner scn = new Scanner(System.in);
        String answer = "";
        String answer2 = "";
        System.out.println("Welcome to java calculator.....");
        System.out.print("\nEnter the first number: ");
        double numA = scn.nextDouble();
        System.out.print("Enter the second number: ");
        double numB = scn.nextDouble();
        System.out.println("What you want to calculate?");
        double result = 0;

        answer = scn.next();

        if(answer.equals("+")){
            result = numA + numB;
        }

        if(answer.equals("-")){
            result = numA - numB;
        }

        if(answer.equals("*")){
            result = numA * numB;
        }

        if(answer.equals("/")){
            result = numA / numB;
        }

        System.out.println("The result is: " + result);

        System.out.println("Do you want to continue?");
        answer2 = scn.next();

        if(answer2.equalsIgnoreCase("y")){
            ret = true;
            }

        else{
            System.out.println("Good bye....:)");
            System.exit(0);
            }
        }
    }
}
于 2016-09-03T13:33:07.830 回答