【问题标题】:How to update output with continuous user inputs until terminated with "."?如何使用连续的用户输入更新输出,直到以“。”终止?
【发布时间】:2020-06-01 15:14:51
【问题描述】:

Java 新手,并试图弄清楚如何保持用户给出的正在运行的计算表达式持续不断,直到他们输入“。”终止它,然后打印输出。我需要使用我已经放置的主要方法之外的方法。我不必担心操作顺序,但我对如何在跟踪其输出的同时进行连续数量的操作/数字感到困惑那么当他们输入“.”时如何最终打印结果。

import java.util.Scanner;

public class Evaluator {

    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);
        int num1;
        char expression;
        int num2;
        String numlist = "";

        System.out.println("Enter the expression: ");
        num1 = scan.nextInt();
        expression = scan.next().charAt(0);
        num2 = scan.nextInt();
        scan.close();

        if (expression == '+') {
            add(num1, num2);
        } else if (expression == '-') {
            subtract(num1, num2);
        } else if (expression == '*') {
            multiply(num1, num2);
        } else if (expression == '/') {
            divide(num1, num2);
        } else if (expression == '%') {
            modulus(num1, num2);
        } else if (expression == '.'){

        }
    }

    static int add(int num1, int num2) {
        return (num1 + num2);
    }

    static int subtract(int num1, int num2) {
        return (num1 - num2);
    }

    static int divide(int num1, int num2) {
        return (num1/num2);
    }

    static int multiply(int num1, int num2) {
        return (num1*num2);
    }

    static int modulus(int num1, int num2) {
        return (num1%num2);
    }
}

输出:

输入表达式:

1
+
26
*
2
%
19
/
5
.

结果是 3

【问题讨论】:

    标签: java if-statement math methods operators


    【解决方案1】:

    我建议以“0 +”开头,然后是用户输入的任何内容。因为这样你就不需要对第一个数字进行特殊处理了。

    import java.util.Scanner;
    
    class Main
    {
    
        public static void main(String[] args)
        {
            Scanner scan = new Scanner(System.in);
            System.out.println("Enter the expression: ");
    
            // Start with "0 +"
            int value = 0;
            String operator = "+";
    
            while (true) // loop
            {
                int operand = scan.nextInt();
    
                switch (operator)
                {
                    case "+": value += operand; break;
                    case "-": value -= operand; break;
                    case "*": value *= operand; break;
                    case "/": value /= operand; break;
                    case "%": value %= operand; break;
                    default: throw new UnsupportedOperationException(operator);
                }
    
                operator = scan.next();
    
                if (".".equals(operator))
                {
                    break; // exit the loop
                }
            }
    
            System.out.println("result=" + value);
            scan.close();
        }
    
    }
    

    但是,此算法不关心运算符顺序的数学规则。它只是像一个便宜的 3 欧元袖珍计算器一样从左到右处理输入。

    基于您的 if-else 构造和方法的其他示例:

    import java.util.Scanner;
    
    class Main
    {
    
        public static void main(String[] args)
        {
            Scanner scan = new Scanner(System.in);
            System.out.println("Enter the expression: ");
    
            // Start with "0 +"
            int value = 0;
            char operator = '+';
    
            while (true) // loop
            {
                int operand = scan.nextInt();
    
                if (operator == '+') {
                    value=add(value, operand);
                } else if (operator == '-') {
                    value=subtract(value, operand);
                } else if (operator == '*') {
                    value=multiply(value, operand);
                } else if (operator == '/') {
                    value=divide(value, operand);
                } else if (operator == '%') {
                    value=modulus(value, operand);
                } else {
                    throw new UnsupportedOperationException(String.valueOf(operator));
                }
    
                operator = scan.next().charAt(0);
    
                if (operator=='.')
                {
                    break; // exit the loop
                }
            }
    
            System.out.println("result=" + value);
            scan.close();
        }
    
        static int add(int num1, int num2) {
            return (num1 + num2);
        }
    
        static int subtract(int num1, int num2) {
            return (num1 - num2);
        }
    
        static int divide(int num1, int num2) {
            return (num1/num2);
        }
    
        static int multiply(int num1, int num2) {
            return (num1*num2);
        }
    
        static int modulus(int num1, int num2) {
            return (num1%num2);
        }
    }
    

    【讨论】:

    • 这很好,但是在使用我添加的主要方法时有什么办法吗?
    • 当然你可以写case "+": value=add(value,operand); break;或者甚至使用你的if-else结构。但不要忘记将运算结果存储在变量value中。
    • 那么我应该在哪里添加我的 if-else 构造? switch 构造不是已经处理了我的 if-else 正在做什么吗?
    • 我按照你的说法进行了编辑并添加到我的方法中,但现在它说
    • 在 java.base/java.util.Scanner.next 的 java.base/java.util.Scanner.throwFor(Scanner.java:939) 的线程“main”中出现异常 java.util.InputMismatchException (Scanner.java:1594) 在 java.base/java.util.Scanner.nextInt(Scanner.java:2258) 在 java.base/java.util.Scanner.nextInt(Scanner.java:2212) 在 Main.main(评估.java:17)
    【解决方案2】:

    类似:

    while(expression!='.'){
     if (expression == '+') {
            add(num1, num2);
        } else if (expression == '-') {
            subtract(num1, num2);
        } else if (expression == '*') {
            multiply(num1, num2);
        }else if (expression == '/') {
            divide(num1, num2);
        } else if (expression == '%') {
            modulus(num1, num2);
        }
    }
    

    这是你要问的吗?

    【讨论】:

    • 是的!谢谢!但是现在当我输入例如“2 + 4 - 1”的用户输入时。它不会打印出结果。我该怎么办?
    猜你喜欢
    • 1970-01-01
    • 2018-09-13
    • 2020-04-05
    • 2018-05-11
    • 2016-01-19
    • 1970-01-01
    • 2020-05-31
    • 2015-01-13
    • 1970-01-01
    相关资源
    最近更新 更多