Bài 4: Nhạp xuất trong java

1. Giới thiệu về nhập xuất trong Java


2. Xuất dữ liệu

a) System.out.println()

System.out.println("Xin chào Java!");
System.out.println(2025);

b) System.out.print()

System.out.print("Xin chào ");
System.out.print("Java");

👉 Kết quả: Xin chào Java

c) System.out.printf()

int age = 20;
double score = 8.75;
System.out.printf("Tuổi: %d - Điểm: %.2f\n", age, score);

👉 %d: số nguyên, %f: số thực, %.2f: làm tròn 2 chữ số thập phân, %s: chuỗi.


3. Nhập dữ liệu với Scanner

Cần import:

import java.util.Scanner;

Tạo đối tượng:

Scanner sc = new Scanner(System.in);

Một số phương thức hay dùng:

Ví dụ:

import java.util.Scanner;

public class DemoNhapXuat {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Nhập tên: ");
        String name = sc.nextLine();

        System.out.print("Nhập tuổi: ");
        int age = sc.nextInt();

        System.out.print("Nhập điểm: ");
        double score = sc.nextDouble();

        System.out.print("Bạn có phải sinh viên không (true/false): ");
        boolean isStudent = sc.nextBoolean();

        System.out.printf("Tên: %s - Tuổi: %d - Điểm: %.2f - Sinh viên: %b\n", name, age, score, isStudent);

        sc.close();
    }
}

4. Bài tập thực hành

Bài 1: In thông tin cá nhân

👉 Nhập tên, tuổi, địa chỉ và in ra màn hình.

import java.util.Scanner;

public class Bai1_ThongTin {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Nhập họ tên: ");
        String name = sc.nextLine();

        System.out.print("Nhập tuổi: ");
        int age = sc.nextInt();
        sc.nextLine(); // bỏ dòng trống sau khi nhập số

        System.out.print("Nhập địa chỉ: ");
        String address = sc.nextLine();

        System.out.println("===== Thông tin =====");
        System.out.println("Họ tên: " + name);
        System.out.println("Tuổi: " + age);
        System.out.println("Địa chỉ: " + address);

        sc.close();
    }
}

Bài 2: Cộng hai số

👉 Nhập 2 số nguyên, in ra tổng, hiệu, tích, thương.

import java.util.Scanner;

public class Bai2_CongHaiSo {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Nhập số a: ");
        int a = sc.nextInt();

        System.out.print("Nhập số b: ");
        int b = sc.nextInt();

        System.out.println("Tổng = " + (a + b));
        System.out.println("Hiệu = " + (a - b));
        System.out.println("Tích = " + (a * b));
        System.out.println("Thương = " + ((b != 0) ? (a / (double)b) : "Không chia được"));

        sc.close();
    }
}

Bài 3: Tính chu vi, diện tích hình tròn

👉 Nhập bán kính r, in chu vi, diện tích (lấy 2 chữ số thập phân).

import java.util.Scanner;

public class Bai3_HinhTron {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Nhập bán kính r: ");
        double r = sc.nextDouble();

        double chuVi = 2 * Math.PI * r;
        double dienTich = Math.PI * r * r;

        System.out.printf("Chu vi = %.2f\n", chuVi);
        System.out.printf("Diện tích = %.2f\n", dienTich);

        sc.close();
    }
}

Bài 4: Kiểm tra tuổi hợp lệ

👉 Nhập tuổi, kiểm tra xem có hợp lệ không (0–120).

import java.util.Scanner;

public class Bai4_KiemTraTuoi {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Nhập tuổi: ");
        int age = sc.nextInt();

        if (age >= 0 && age <= 120) {
            System.out.println("Tuổi hợp lệ.");
        } else {
            System.out.println("Tuổi không hợp lệ!");
        }

        sc.close();
    }
}

Bài 5: Máy tính mini (nâng cao)

👉 Nhập 2 số và phép toán (+, -, *, /), in kết quả.

import java.util.Scanner;

public class Bai5_MayTinhMini {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Nhập số thứ nhất: ");
        double a = sc.nextDouble();

        System.out.print("Nhập số thứ hai: ");
        double b = sc.nextDouble();

        sc.nextLine(); // bỏ ký tự xuống dòng

        System.out.print("Nhập phép toán (+, -, *, /): ");
        String op = sc.nextLine();

        switch (op) {
            case "+":
                System.out.println("Kết quả = " + (a + b));
                break;
            case "-":
                System.out.println("Kết quả = " + (a - b));
                break;
            case "*":
                System.out.println("Kết quả = " + (a * b));
                break;
            case "/":
                if (b != 0) {
                    System.out.println("Kết quả = " + (a / b));
                } else {
                    System.out.println("Lỗi: Không chia được cho 0!");
                }
                break;
            default:
                System.out.println("Phép toán không hợp lệ!");
        }

        sc.close();
    }
}