Java:在计算器上在二进制和十进制"modes"之间切换



我基本上创建了两个独立的卷积计算器,一个计算十进制数,另一个计算二进制数。

用户在命令行上输入他们想要计算的方程式,结果就会打印出来。

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

有一个名为Calculator的主类,它将接受用户的输入。所以这个类将有你的主要方法

如果输入是dec,则读取下一行并传递给十进制计算器

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

如果输入是exit,则退出。

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

试着按照我的建议写一些代码。如果你有困难,我可以帮你。

最新更新