使用运算符逐行输入评估以及如何存储变化的值直到用户键入“。”

Line by line input evaluation with operators and how to store the changing value until user types "."

我正在尝试编写一个程序来计算一次每行写入一个字符或值的数学方程式。用户将逐行输入交替的数字和运算符,以“.”结尾。这意味着我不会尝试从单个字符串进行评估(并假设输入将始终在数字和运算符之间交替)。

我不知道如何让它一直接受输入,直到用户键入“。”

我也不确定如何在用户键入公式时保持值不断变化以及如何存储它。

示例输入:

1

+

6

-

3

.

方程的解是:4


import java.util.Scanner;

class Evaluator {

static int add (int a, int b)
{
    return a + b;
}

static int multiply (int a, int b)
{
    return a * b;
}

static int divide (int a, int b)
{
    return a / b;
}

static int subtract (int a, int b)
{
    return a - b;
}

static int modulus (int a, int b)
{
    return a % b;
}


public static void main(String [] args)
{

Scanner input = new Scanner (System.in);

int a,b,c;

System.out.println("Enter the equation:");

a = input.nextInt();

String c = input.next();

b = input.nextInt();


    if (c.contains("+")) {
        int result = add (a,b); 
    }
    else if (c.contains("*")) {
        int result = multiply (a,b);    
    }
    else if (c.contains("/")) {
        int result = divide (a,b);
    }
    else if (c.contains("-")) {
        int result = subtract (a,b);
    }
    else if (c.contains("%")) {
        int result = modulus (a,b);
    }
    else if (c.contains(".")) {
        break;
    }


    System.out.print("The solution to your equation is: " + result);

    }
}

这是一个简单的 while 循环,您可以使用它来获取用户的输入。我检查一下它是数字还是其他东西。您可以使用此框架获取用户的输入并在有人按下“.”时退出。

    Scanner input = new Scanner (System.in);

    int a,b,currentTotal = 0;
    String inputFromUser = "nothing";

    while(!inputFromUser.equals("."))
    {
        inputFromUser = input.nextLine(); //grab line by line
        if(inputFromUser.matches("\d+")){
            //parse the number and set it to a value like a...
            System.out.println("You entered a number: " + inputFromUser);
        }
        else if(!inputFromUser.equals(".")){
            //check if you have value and try to apply your number to your current total
            System.out.println("You entered something other than a number: " + inputFromUser);
        }
    }

如果用户输入一个数字,将变量设置为该数字,也许是

如果用户输入的不是数字而不是句点,则使用您提供的逻辑检查输入是否有效,并像 operatorMethod(a, currentTotal)

一样应用它

您的代码非常接近,因为您以正确的顺序使用 Scanner next() 和 nextInt()(以匹配输入规则)。这里在一对输入周围添加了一个 while(true) 循环;要么用户输入“.”并且循环中断,或者用户输入一个运算符后跟下一个数字。通过在各种数学运算符中重复使用结果来保持最新。

    public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

    int b, result;

    System.out.println("Enter the equation:");

    result = input.nextInt();

    while (true) {

        String c = input.next();
        if (c.contains(".")) {
            break;
        }

        b = input.nextInt();

        if (c.contains("+")) {
            result = add(result, b);
        } else if (c.contains("*")) {
            result = multiply(result, b);
        } else if (c.contains("/")) {
            result = divide(result, b);
        } else if (c.contains("-")) {
            result = subtract(result, b);
        } else if (c.contains("%")) {
            result = modulus(result, b);
        } 
    }
    input.close();

    System.out.print("The solution to your equation is: " + result);

}