java计算器(重新输入值)

java计算器(重新输入值),java,calculator,Java,Calculator,我对交给我的java计算器作业有一些问题。我被告知要制作一个计算器,它可以执行非常基本的函数,捕捉异常,并允许您立即更正操作数或运算符的值(这就是我遇到的问题)。例如,这是控制台中应该发生的事情: j * 6 Catch exception and print error message here and asking for new first operand 4 Answer: 4 * 6 = 24 或 到目前为止,我掌握了以下代码: import java.util.*; public

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

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

到目前为止,我掌握了以下代码:

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);

}
}

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

现在假设您创建了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
}
对于需要执行的每种类型的异常,都需要执行此操作。我看到三种可能的情况

  • 失踪操作员
  • 缺少左操作数
  • 缺少右操作数
  • 可能存在这些问题的组合,但是您编写的异常处理应该能够在处理过程中(一个接一个)处理这些问题

    注意:您已经使用Scanner类来读取用户输入。我假设你了解扫描器的基本知识,但你的后续问题与此相矛盾。我真的会研究Java Scanner类。请查看以下内容:


    用一个巨大的try块捕捉所有东西被认为是不好的做法。把试抓块放在你需要的地方

    另外,人们并不真的喜欢为他们做别人的家庭作业。如果你有一个特定的问题,那很好,但是说“我必须为一个作业做这个,我怎么做所有的事情”可能不会成功。

    你应该试试这个

    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);
                }
            }
        }
    }
    

    问题是什么…?我提供了这个控制台示例,您可以在其中立即重新输入以前无效的值,我需要有关如何编写计算器来实现这一点的想法/示例。在您说“//使用扫描仪获取操作数”的部分,我将如何做?这是几个星期来让我头晕目眩的部分。刚刚更新了我的帖子。你真的需要学习Scanner类是如何工作的。好吧,我想我已经掌握了重新输入值的部分。我会到处玩,看看能得到什么。谢谢你的帮助。只要你能从技术角度理解我的解决方案,你就可以走了。你仍然可以问问题。如果您有更详细的问题,我们会更容易帮助您。|=^)第一部分:说明。第二部分:110%的情况并非如此。
    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);
                }
            }
        }
    }