利用 switch...case 编写简单计算器的 Java 程序
在本文中,我们将了解如何使用 switch-case 构造一个简单的计算器。switch 语句计算一个表达式,将表达式的值与 case 子句进行匹配,并执行与该 case 关联的语句。
以下是我们要执行的算术操作。
- 加法
- 减法
- 乘法
- 除法
- 取整除
- 模
下面演示了同样的内容 −
输入
假设我们的输入是 −
The two inputs: 40.0 and 12.0 Operator:%
输出
预期的输出将是 −
The result is 40.0 % 12.0 = 4.0
算法
Step 1 - START Step 2 - Declare three values namely my_input_1, my_input_2 and my_result and declare a character value namely operator. Step 3 - Read the required values from the user/ define the values Step 4 - Define case statements which takes ‘operator’ value as switch case to calculate the sum, difference, multiplication, division, modulus. Step 5 - Pass the operator value to the case statements to calculate the arithmetic operation between the two inputs ‘my_input_1’ and ‘my_input_2’ Step 7 - Display the result Step 8 - Stop
示例 1
在这里,输入由用户根据提示输入。您可以在我们的coding ground 工具 中实时地尝试此示例。
import java.util.Scanner; public class OperatorSwitch { public static void main(String[] args) { char operator; Double my_input_1, my_input_2, my_result; System.out.println("Required packages have been imported"); Scanner my_scanner = new Scanner(System.in); System.out.println("A reader object has been defined "); System.out.println("Enter the first number"); my_input_1 = my_scanner.nextDouble(); System.out.println("Enter the second number"); my_input_2 = my_scanner.nextDouble(); System.out.println("Enter any of the following operator: +, -, *, /, %"); operator = my_scanner.next().charAt(0); switch (operator) { case '+': my_result = my_input_1 + my_input_2; System.out.println(my_input_1 + " + " + my_input_2 + " = " + my_result); break; case '-': my_result = my_input_1 - my_input_2; System.out.println(my_input_1 + " - " + my_input_2 + " = " + my_result); break; case '*': my_result = my_input_1 * my_input_2; System.out.println(my_input_1 + " * " + my_input_2 + " = " + my_result); break; case '/': my_result = my_input_1 / my_input_2; System.out.println(my_input_1 + " / " + my_input_2 + " = " + my_result); break; case '%': my_result = my_input_1 % my_input_2; System.out.println(my_input_1 + " % " + my_input_2 + " = " + my_result); break; default: System.out.println("The operator you have selected is invalid"); break; } } }
输出
Required packages have been imported A reader object has been defined Enter the first number 40 Enter the second number 12 Choose any of the following operator: +, -, *, /, % % 40.0 % 12.0 = 4.0
示例 2
在这里,整数已被预先定义好,其值被访问并在控制台中显示。
public class OperatorSwitch { public static void main(String[] args) { char operator; Double my_input_1, my_input_2, my_result; my_input_1 = 40.0; my_input_2 = 12.0; operator = '%'; System.out.println("The two numbers are defined as " +my_input_1 +" and " +my_input_2); System.out.println("The operator is defined as " +operator); switch (operator) { case '+': my_result = my_input_1 + my_input_2; System.out.println(my_input_1 + " + " + my_input_2 + " = " + my_result); break; case '-': my_result = my_input_1 - my_input_2; System.out.println(my_input_1 + " - " + my_input_2 + " = " + my_result); break; case '*': my_result = my_input_1 * my_input_2; System.out.println(my_input_1 + " * " + my_input_2 + " = " + my_result); break; case '/': my_result = my_input_1 / my_input_2; System.out.println(my_input_1 + " / " + my_input_2 + " = " + my_result); break; case '%': my_result = my_input_1 % my_input_2; System.out.println(my_input_1 + " % " + my_input_2 + " = " + my_result); break; default: System.out.println("The operator you have selected is invalid"); break; } } }
输出
The two numbers are defined as 40.0 and 12.0 The operator is defined as % 40.0 % 12.0 = 4.0
广告