Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Java

โปรแกรม Java เครื่องคิดเลขง่ายด้วย Switch Case


ในบทความนี้ เราจะทำความเข้าใจวิธีสร้าง เครื่องคิดเลขง่าย (Simple Calculator) ด้วย Switch Case ใน Java

รองรับการดำเนินการ 6 แบบ:

  • บวก (+)
  • ลบ (-)
  • คูณ (*)
  • หาร (/)
  • หารเอาเศษ (%)

ตัวอย่าง Input และ Output

Input:

The two inputs: 40.0 and 12.0
Operator: %

Output:

The result is 40.0 % 12.0 = 4.0

อัลกอริธึม

ขั้นตอนที่ 1 - เริ่มต้น
ขั้นตอนที่ 2 - ประกาศตัวแปร my_input_1, my_input_2, my_result (double) และ operator (char)
ขั้นตอนที่ 3 - อ่านค่าจากผู้ใช้ หรือกำหนดค่าเริ่มต้น
ขั้นตอนที่ 4 - ใช้ switch-case ตรวจสอบ operator คำนวณตามเคสที่ตรง
ขั้นตอนที่ 5 - แสดงผลลัพธ์พร้อมสมการ
ขั้นตอนที่ 6 - หยุด

ตัวอย่างที่ 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;
        }
    }
}

Output

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

Output

The two numbers are defined as 40.0 and 12.0
The operator is defined as %
40.0 % 12.0 = 4.0