System.out.Scanner.System.out.println()System.out.println("Xin chào Java!");
System.out.println(2025);
System.out.print()System.out.print("Xin chào ");
System.out.print("Java");
👉 Kết quả: Xin chào Java
System.out.printf()printf trong C.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.
ScannerCần import:
import java.util.Scanner;
Tạo đối tượng:
Scanner sc = new Scanner(System.in);
nextLine() → nhập 1 dòng (chuỗi).nextInt() → nhập số nguyên.nextFloat() → nhập số thực float.nextDouble() → nhập số thực double.nextBoolean() → nhập giá trị true/false.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();
}
}
👉 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();
}
}
👉 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();
}
}
👉 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();
}
}
👉 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();
}
}
👉 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();
}
}