บทความนี้สอนวิธีเขียนโปรแกรมคํานวณภาษีเงินได้ด้วย Java โดยเป็นภาษีเงินได้แบบบุคคลธรรมดาในประเทศไทย เริ่มจากแนะนำสูตรคำนวณหาเงินได้สุทธิ และภาษีที่ต้องจ่าย ตารางอัตราภาษีแบบขั้นบันได และสุดท้ายแนะนำวิธีเขียนโปรแกรมคํานวณภาษี Java พร้อมแสดงผลลัพธ์ และคำอธิบายโค้ดโปรแกรม
ตัวอย่าง สูตรคำนวณหาเงินได้สุทธิ และภาษีที่ต้องจ่าย
1. เงินได้สุทธิ หาได้จากค่าเงินได้, ค่าใช้จ่าย และค่าลดหย่อน
เงินได้สุทธิ = เงินได้ - ค่าใช้จ่าย - ค่าลดหย่อนnet_income = income - expenses - deductions
2. ภาษีที่ต้องจ่าย หาได้จากค่าเงินได้สุทธิ (สูตรด้านบน) และอัตราภาษี (ตามตารางด้านล่าง)
ภาษีที่ต้องจ่าย = เงินได้สุทธิ x อัตราภาษีtax = net_income x tax
ตัวอย่าง ตารางวิธีคำนวณภาษีเงินได้บุคคลธรรมดา แบบคิดอัตราภาษีแบบขั้นบันได
| เงินได้สุทธิ (บาท) | อัตราภาษี | 
| 1 - 150,000 | ได้รับยกเว้น | 
| 150,001 - 300,000 | 5% | 
| 300,001 - 500,000 | 10% | 
| 500,001 - 750,000 | 15% | 
| 750,001 - 1,000,000 | 20% | 
| 1,000,0001 - 2,000,000 | 25% | 
| 2,000,001 - 5,000,000 | 30% | 
| 5,000,001 บาทขึ้นไป | 35% | 
ตัวอย่าง วิธีเขียนโปรแกรมคํานวณภาษีเงินได้ด้วย Java
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        double income = 0.0;
        System.out.print("Enter your net income: ");
        income = scanner.nextDouble();
        
        double expenses = 0.0;
        System.out.print("Enter your expenses: ");
        expenses = scanner.nextDouble();
        double deductions = 0.0;
        System.out.print("Enter your deductions: ");
        deductions = scanner.nextDouble();
        double net_income = income - expenses - deductions;
        double tax = 0.0;
        if (net_income <= 150000) {
            tax = 0;
        } else if (net_income <= 300000) {
            tax = net_income * 0.05;
        } else if (net_income <= 500000) {
            tax = net_income * 0.1;
        } else if (net_income <= 750000) {
            tax = net_income * 0.15;
        } else if (net_income <= 1000000) {
            tax = net_income * 0.20;
        } else if (net_income <= 2000000) {
            tax = net_income * 0.25;
        } else if (net_income <= 5000000) {
            tax = net_income * 0.30;
        } else {
            tax = net_income * 0.35;
        }
        System.out.println("Your net income is: " + net_income + " THB");
        System.out.println("Your tax is: " + tax + " THB");
    }
}ตัวอย่างที่ 1.
Enter your net income: 100000
Enter your expenses: 500
Enter your deductions: 500
Your net income is: 99,000.0 THB
Your tax is: 0.0 THBตัวอย่างที่ 2.
Enter your net income: 1000000
Enter your expenses: 100000
Enter your deductions: 0
Your net income is: 900,000.0 THB
Your tax is: 180,000.0 THBจากตัวอย่างวิธีเขียนโปรแกรมคํานวณภาษีเงินได้ด้วย Java และผลลัพธ์สามารถอธิบายได้ดังนี้
1. มีการรับค่าจากผู้ใช้ง่ายได้แก่ เงินได้ (income), ค่าใช้จ่าย (expenses) และค่าลดหย่อน (deductions)
2. หาค่าเงินได้สุทธิ (net_income) เท่ากับ income - expenses - deductions
3. นำ net_income ไปเข้าเงื่อนไข if เพื่อหาภาษีเงินได้ที่ต้องจ่าย คือ net_income x tax
4. กรณี net_income เท่ากับ 99,000 เข้าเงื่อนไข if (net_income <= 150000) ค่า tax เท่ากับ 0
5. กรณี net_income เท่ากับ 900,000 เข้าเงื่อนไข if (net_income <= 1000000) ค่า tax เท่ากับ 180,000
6. แสดงค่า net_income และ tax ออกสู่หน้าจอ