1. Overview
1.概述
In this tutorial, we’ll implement a Basic Calculator in Java supporting addition, subtraction, multiplication and division operations.
在本教程中,我们将在Java中实现一个支持加、减、乘、除运算的基本计算器。
We’ll also take the operator and operands as inputs and process the calculations based on them.
我们还将把运算符和操作数作为输入,并根据它们来处理计算结果。
2. Basic Setup
2.基本设置
First, let’s show some information about the calculator:
首先,让我们展示一些关于计算器的信息。
System.out.println("---------------------------------- \n" +
"Welcome to Basic Calculator \n" +
"----------------------------------");
System.out.println("Following operations are supported : \n" +
"1. Addition (+) \n" +
"2. Subtraction (-) \n" +
"3. Multiplication (*) \n" +
"4. Division (/) \n");
Now, let’s use java.util.Scanner to take the user inputs:
现在,让我们使用java.util.Scanner来获取用户输入。
Scanner scanner = new Scanner(System.in);
System.out.println("Enter an operator: (+ OR - OR * OR /) ");
char operation = scanner.next().charAt(0);
System.out.println("Enter the first number: ");
double num1 = scanner.nextDouble();
System.out.println("Enter the second number: ");
double num2 = scanner.nextDouble();
As we are taking inputs into the system, we need to validate them. For example, if the operator is not +, -, * or /, then our calculator should call out the bad input. Similarly, if we enter the second number as 0 for division operation, the results will not be good.
当我们把输入输入到系统中时,我们需要对它们进行验证。例如,如果运算符不是+、-、*或/,那么我们的计算器就应该呼出不良输入。同样,如果我们在除法运算中输入第二个数字为0,那么结果也不会好。
So, let’s implement these validations.
因此,让我们来实现这些验证。
First, let’s focus on the situation when the operator is invalid:
首先,我们来关注一下操作者无效时的情况。
if (!(operation == '+' || operation == '-' || operation == '*' || operation == '/')) {
System.err.println("Invalid Operator. Please use only + or - or * or /");
}
Then we can show errors for invalid operations:
然后我们可以显示无效操作的错误。
if (operation == '/' && num2 == 0.0) {
System.err.println("The second number cannot be zero for division operation.");
}
The user inputs are first validated. After that, the calculation result will be displayed as:
用户的输入首先被验证。之后,计算结果将显示为。
<number1> <operation> <number2> = <result>
<号码1> <操作> <号码2> = <结果>
3. Processing Calculations
3.加工计算
Firstly, we can use an if-else construct to process calculations
首先,我们可以使用if-else结构来处理计算结果
if (operation == '+') {
System.out.println(num1 + " + " + num2 + " = " + (num1 + num2));
} else if (operation == '-') {
System.out.println(num1 + " - " + num2 + " = " + (num1 - num2));
} else if (operation == '*') {
System.out.println(num1 + " x " + num2 + " = " + (num1 * num2));
} else if (operation == '/') {
System.out.println(num1 + " / " + num2 + " = " + (num1 / num2));
} else {
System.err.println("Invalid Operator Specified.");
}
Similarly, we can use a Java switch statement:
同样地,我们可以使用Java的switch语句。
switch (operation) {
case '+':
System.out.println(num1 + " + " + num2 + " = " + (num1 + num2));
break;
case '-':
System.out.println(num1 + " - " + num2 + " = " + (num1 - num2));
break;
case '*':
System.out.println(num1 + " x " + num2 + " = " + (num1 * num2));
break;
case '/':
System.out.println(num1 + " / " + num2 + " = " + (num1 / num2));
break;
default:
System.err.println("Invalid Operator Specified.");
break;
}
We can use a variable to store the calculation results. As a result, it can be printed at the end. In this case, System.out.println will be used only once.
我们可以使用一个变量来存储计算结果。因此,它可以在最后被打印出来。在这种情况下,System.out.println将只被使用一次。
Also, the maximum range for the calculations is 2147483647. Consequently, if we exceed it, we’ll overflow from an int data type. Therefore, it should be stored in a variable of a bigger data type, for example, a double data type.
另外,计算的最大范围是2147483647。因此,如果我们超过它,就会从int数据类型溢出。因此,它应该被存储在一个更大的数据类型的变量中,例如,double数据类型。
4. Conclusion
4.总结
In this tutorial, we implemented a Basic Calculator in Java, using two different constructs. We also made sure the inputs are validated before processing them further.
在本教程中,我们用Java实现了一个基本的计算器,使用了两个不同的结构。我们还确保了在进一步处理输入之前对其进行验证。
As always, the code is available over on GitHub.
像往常一样,代码可在GitHub上获得。