Java:在计算器

时间:2015-10-30 21:01:48

标签: java binary decimal calculator

我基本上创建了2个独立的复杂计算器,一个计算十进制数,另一个计算二进制数。

用户输入他们想要在命令行上计算的等式,然后打印结果。

>1+1
>2

或二进制

>0b111 + 0b101
>0b1100

我遇到的麻烦是能够在动态计算二进制数和十进制数之间切换。我最终想让它像这样工作: (默认情况下,它将采用十进制模式)

>1+1
>2
>bin
>0b111 + 0b101 = 0b1100
>dec
>5 * 10
>50
>exit

(在任何时候输入exit都会退出程序)

有没有办法可以解决这个问题?

//Decimal Calculator
import java.util.*;
public class DecArithmetic
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        while(true)
        {
            String equation = input.nextLine();
            if (equation.equalsIgnoreCase("exit"))
            {
                System.exit(0);
            }
            equation = equation.replaceAll("\\s+","");
            int opLocation = equation.indexOf("+");
            if(opLocation == -1)
            {
                opLocation = equation.indexOf("-");
            }
            if(opLocation == -1)
            {
                opLocation = equation.indexOf("*");
            }
            if(opLocation == -1)
            {
                opLocation = equation.indexOf("/");
            }
            if(opLocation == -1)
            {
                opLocation = equation.indexOf("|");
            }
            if(opLocation == -1)
            {
                opLocation = equation.indexOf("&");
            }
            if(opLocation == -1)
            {
                opLocation = equation.indexOf("^");
            }
            String number = equation.substring(0,opLocation);
            int operandOne = Integer.parseInt(number);

            number = equation.substring(opLocation + 1);
            int operandTwo = Integer.parseInt(number);

            String op = equation.substring(opLocation, opLocation+1);

            int result = calculate(op, operandOne, operandTwo);
            System.out.println(result);
        }
    }
    public static int calculate(String operator, int operandOne, int operandTwo)
    {
        if(operator.equals("+") == true)
        {
            return operandOne + operandTwo;
        }
        else if(operator.equals("-") == true)
        {
            return operandOne - operandTwo;
        }
        else if(operator.equals("*") == true)
        {
            return operandOne * operandTwo;
        }
        else if(operator.equals("/") == true)
        {
            return operandOne / operandTwo;
        }
        else if(operator.equals("^") == true)
        {
            return operandOne ^ operandTwo;
        }
        else if (operator.equals("|") == true)
        {
            return operandOne | operandTwo;
        }
        else if(operator.equals("&") == true)
        {
            return operandOne & operandTwo;
        }
        else
        {
            Scanner scan = new Scanner(System.in);
            System.out.println("Please enter a valid operator. (+, -, *, /, ^, |, &)");
            return calculate(scan.nextLine(), operandOne, operandTwo);
        }
    }
}
//Binary Calculator
import java.util.*;
public class BinaryArithmetic
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        while(true)
        {
            String equation = input.nextLine();
            if (equation.equalsIgnoreCase("exit"))
            {
                System.exit(0);
            }
            equation = equation.replaceAll("\\s+","");
            equation = equation.replaceAll("0b","");
            int opLocation = equation.indexOf("+");
            if(opLocation == -1)
            {
                opLocation = equation.indexOf("-");
            }
            if(opLocation == -1)
            {
                opLocation = equation.indexOf("*");
            }
            if(opLocation == -1)
            {
                opLocation = equation.indexOf("/");
            }
            if(opLocation == -1)
            {
                opLocation = equation.indexOf("|");
            }
            if(opLocation == -1)
            {
                opLocation = equation.indexOf("&");
            }
            if(opLocation == -1)
            {
                opLocation = equation.indexOf("^");
            }
            String number = equation.substring(0,opLocation);
            int bin1 = Integer.parseInt(number);
            String b1 = Integer.toString(bin1);
            int operandOne = Integer.parseInt(b1, 2);

            number = equation.substring(opLocation + 1);
            int bin2 = Integer.parseInt(number);
            String b2 = Integer.toString(bin2);
            int operandTwo = Integer.parseInt(b2, 2);

            String op = equation.substring(opLocation, opLocation+1);
            String result = Integer.toBinaryString(calculate(op, operandOne, operandTwo));
            System.out.println("0b"+result);

        }
    }
    public static int calculate(String operator, int operandOne, int operandTwo)
    {
        if(operator.equals("+") == true)
        {
            return operandOne + operandTwo;
        }
        else if(operator.equals("-") == true)
        {
            return operandOne - operandTwo;
        }
        else if(operator.equals("*") == true)
        {
            return operandOne * operandTwo;
        }
        else if(operator.equals("/") == true)
        {
            return operandOne / operandTwo;
        }
        else if(operator.equals("^") == true)
        {
            return operandOne ^ operandTwo;
        }
        else if (operator.equals("|") == true)
        {
            return operandOne | operandTwo;
        }
        else if(operator.equals("&") == true)
        {
            return operandOne & operandTwo;
        }
        else
        {
            Scanner scan = new Scanner(System.in);
            System.out.println("Please enter a valid operator. (+, -, *, /, ^, |, &)");
            return calculate(scan.nextLine(), operandOne, operandTwo);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

有一个名为Calculator的主类,它将从用户那里获取输入。所以这个课将有你的主要方法

如果输入为dec,则读取nextLine并传递给Decimal Calculator

如果输入是bin,则读取nextLine并传递给二进制计算器

如果输入退出,则退出。

如果输入既不是dec,也不是bin也不是exit,那么你假设用户输入了一个十进制方程并调用了Decimal计算器。

尝试根据我的建议编写一些代码。如果你挣扎,我可以帮助你。