创建一个java计算器

时间:2015-11-07 16:43:24

标签: java

我的任务之一是创建一个计算器程序来实现一个解决方案,要求用户输入一行包含修复后表达式的文本。程序应该验证输入然后评估它显示评估的表达式(使用中缀表示法)和结果。 如果验证失败,则应显示适当的错误消息。例如,错误:表达式无效。

这是我到目前为止的代码:(如何更改代码,以便循环,在这个循环中,应该反复提示用户输入表达式,直到他们输入空行,即按下没有输入的返回,此时程序应该终止?

import java.util.Scanner;

public class Calculator {

        public static void main(String[] args) {
            try (Scanner scanner = new Scanner(System.in)) {
                System.out.println("Enter a post-fix expression of the form 3 5 *:");
                double n1 = scanner.nextDouble();
                double n2 = scanner.nextDouble();
                String operation = scanner.next();

                switch (operation)  {
                case "+":
                    System.out.println("Your answer is " + (n1 + n2));
                    break;

                case "-":
                    System.out.println("Your answer is " + (n1 - n2));
                    break;

                case "/":
                    System.out.println("Your answer is " + (n1 / n2));
                    break;

                case "*":
                    System.out.println("Your answer is " + (n1 * n2));
                    break;

                }
            }
        }
}

3 个答案:

答案 0 :(得分:1)

在while循环中写入。

while(true) {
   System.out.println("Enter a post-fix expression of the form 3 5 *:");
   double n1 = scanner.nextDouble();
   double n2 = scanner.nextDouble();
   String operation = scanner.next();
   switch (operation ) {
         case "*":
           // Do operation
           continue;
         case "+":
           // Do operation
           continue;
         case "":
            break;
    }
}

答案 1 :(得分:1)

如果我理解您的要求,下一个代码应该可以正常工作。 它会检查你是否按下回车键。我也在检查例外情况。

import java.util.Scanner;

public class Calculator {

    public static void main(String[] args) {
        System.out.println("Enter a post-fix expression of the form 3 5 *:");
        Scanner scanner = new Scanner(System.in);
        String line = scanner.nextLine();
        while (!line.equals("")) {
            try {
                Scanner scannerInternal = new Scanner(line);

                double n1 = scannerInternal.nextDouble();
                double n2 = scannerInternal.nextDouble();
                String operation = scannerInternal.next();
                switch (operation) {
                    case "+":
                        System.out.println("Your answer is " + (n1 + n2));
                        break;

                    case "-":
                        System.out.println("Your answer is " + (n1 - n2));
                        break;

                    case "/":
                        System.out.println("Your answer is " + (n1 / n2));
                        break;

                    case "*":
                        System.out.println("Your answer is " + (n1 * n2));
                        break;

                }
            }
            catch (Exception ex){
                System.out.println("Error: Invalid expression.");
            }
            line = scanner.nextLine();
        }
    }
}

答案 2 :(得分:-1)

您可以使用while循环包装逻辑。类似的东西:

bool get_user_input = true;
while(get_user_input){
Scanner ... 
.... switch(operation){
....}
if(user_input_satisfies){
get_user_input = false; //your finished getting input from scanner
}