java计算器(重新输入值)

时间:2013-07-24 14:36:58

标签: java calculator

我遇到了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);

}
}

3 个答案:

答案 0 :(得分:0)

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

How to create custom exceptions in 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. 缺少右操作数
  4. 可能存在这些问题的组合,但您编写的异常处理应该能够照顾它(逐个)。

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

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

答案 1 :(得分:0)

一个巨大的尝试块可以捕捉到一切,这被认为是不好的做法。将try catch块放在您需要的地方。

人们也不喜欢为他们做其他的功课。如果你有一个特定的问题或问题,那很好,但是说,“我必须为一项任务做这个,我怎么做所有这些”可能不会飞。

答案 2 :(得分: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);
            }
        }
    }
}