이 기사에서는 switch-case를 사용하여 간단한 계산기를 구성하는 방법을 이해합니다. switch 문은 표현식을 평가하고 표현식의 값을 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
여기에서 입력은 프롬프트에 따라 사용자가 입력하고 있습니다. 이 예제는 코딩 접지 도구에서 라이브로 사용해 볼 수 있습니다. .
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