逐行输入评估与操作员以及如何存储更改值,直到用户键入“。”

问题描述 投票:0回答:2

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

我不知道如何制作它以便它继续输入直到用户输入“。”

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

样本输入:

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

    }
}
java methods operators
2个回答
0
投票

您的代码非常接近,因为您以正确的顺序使用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);

}

0
投票

这是一个简单的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);
        }
    }

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

如果用户输入的数字不是数字而不是句点,那么检查输入是否是您提供的逻辑的有效操作,并将其应用为operatorMethod(a,currentTotal)

© www.soinside.com 2019 - 2024. All rights reserved.