[特殊字符] 高质量 Java 综合题 × 10(附应用场景 + 多知识点考核)
✅ 题目 1:用户登录系统模拟
📌 场景: 实现一个简单的用户登录验证系统,包含用户信息封装、集合存储及判断逻辑。
🧪 涉及知识点:
-
类与封装
-
集合(Map)
-
字符串操作
-
控制流
题目:
已知用户信息如下:
用户名:admin,密码:123456
用户名:user1,密码:pass1
请你使用 Java 代码实现一个登录方法 boolean login(String username, String password)
,要求:
-
用户数据预先存在一个
HashMap<String, String>
中; -
验证成功返回
true
,否则返回false
; -
用户名或密码错误都输出提示。
✅ 答案示意:
public class LoginSystem {private static Map<String, String> userDB = new HashMap<>();static {userDB.put("admin", "123456");userDB.put("user1", "pass1");}public static boolean login(String username, String password) {if (!userDB.containsKey(username)) {System.out.println("用户不存在!");return false;}if (!userDB.get(username).equals(password)) {System.out.println("密码错误!");return false;}return true;}
}
🧠 解析:
考察了 Map 使用、逻辑判断、字符串比较以及封装 static 初始化块的写法。
✅ 题目 2:商品折扣系统(多态 + 抽象类)
📌 场景: 不同类型的商品有不同的折扣策略。
🧪 涉及知识点:
-
抽象类与继承
-
方法重写
-
多态
题目:
定义一个抽象类 Product
,包含 String name
和 double price
,以及一个抽象方法 double getFinalPrice()
。
派生两个子类:
-
ElectronicProduct
,打8折 -
FoodProduct
,无折扣
实现一个主程序,接收一个 Product[]
数组,输出每个商品名称与最终价格。
✅ 答案示意:
abstract class Product {String name;double price;Product(String name, double price) {this.name = name;this.price = price;}abstract double getFinalPrice();
}class ElectronicProduct extends Product {ElectronicProduct(String name, double price) {super(name, price);}double getFinalPrice() {return price * 0.8;}
}class FoodProduct extends Product {FoodProduct(String name, double price) {super(name, price);}double getFinalPrice() {return price;}
}// 主程序
Product[] products = {new ElectronicProduct("Laptop", 5000),new FoodProduct("Bread", 10)
};for (Product p : products) {System.out.println(p.name + ":" + p.getFinalPrice());
}
✅ 题目 3:学生成绩处理(面向对象 + 集合 + 排序)
📌 场景: 某班级有多个学生,要求按成绩排序后输出前3名。
🧪 涉及知识点:
-
类设计
-
Comparable/Comparator
-
List 排序
题目:
定义一个 Student
类,包含姓名和成绩,创建一个 List<Student>
,输入数据后按成绩降序排序,输出前三名学生。
✅ 答案示意:
class Student {String name;int score;Student(String name, int score) {this.name = name;this.score = score;}public String toString() {return name + ": " + score;}
}List<Student> list = new ArrayList<>();
list.add(new Student("Alice", 92));
list.add(new Student("Bob", 85));
list.add(new Student("Tom", 96));
list.add(new Student("Jerry", 88));list.sort((a, b) -> b.score - a.score);for (int i = 0; i < 3 && i < list.size(); i++) {System.out.println(list.get(i));
}
✅ 题目 4:订单编号生成器(字符串处理 + Random + 时间)
📌 场景: 实现唯一订单编号规则。
🧪 涉及知识点:
-
日期处理(
LocalDateTime
) -
字符串拼接
-
Random 随机数生成
题目:
请用 Java 编写一个生成唯一订单号的方法,规则如下:
格式:yyyyMMddHHmmss + 4位随机数字,例如:20250418173910 + 2038
✅ 答案示意:
public static String generateOrderId() {String timePart = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));int rand = new Random().nextInt(9000) + 1000;return timePart + rand;
}
✅ 题目 5:文件操作(读取 + 处理数据)
📌 场景: 读取一个文本文件,每一行是一个学生成绩,输出平均分。
🧪 涉及知识点:
-
File IO
-
异常处理
-
数值处理
✅ 示例内容(scores.txt)
Tom 88
Alice 92
Jerry 78
✅ 答案示意:
int total = 0, count = 0;
try (BufferedReader br = new BufferedReader(new FileReader("scores.txt"))) {String line;while ((line = br.readLine()) != null) {String[] parts = line.split(" ");total += Integer.parseInt(parts[1]);count++;}System.out.println("平均分:" + (total * 1.0 / count));
} catch (IOException e) {e.printStackTrace();
}
✅ 题目 6:线程交替打印
📌 场景: 两个线程交替打印数字 1-10。
🧪 涉及知识点:
-
多线程
-
synchronized + wait/notify
-
控制共享资源
✅ 答案示意(简化版本):
class Printer {private int count = 1;private boolean flag = true;public synchronized void printOdd() throws InterruptedException {while (count <= 10) {if (!flag) wait();System.out.println("奇数:" + count++);flag = false;notify();}}public synchronized void printEven() throws InterruptedException {while (count <= 10) {if (flag) wait();System.out.println("偶数:" + count++);flag = true;notify();}}
}
✅ 题目 7:银行账户转账(封装 + 异常)
📌 场景: 实现两个账户之间的转账功能,余额不足抛异常。
🧪 涉及知识点:
-
类设计
-
自定义异常
-
事务控制思维
✅ 异常类 + 主体逻辑:
class InsufficientBalanceException extends Exception {public InsufficientBalanceException(String msg) {super(msg);}
}class Account {private String name;private double balance;Account(String name, double balance) {this.name = name;this.balance = balance;}public void transferTo(Account other, double amount) throws InsufficientBalanceException {if (amount > balance) throw new InsufficientBalanceException("余额不足");this.balance -= amount;other.balance += amount;}
}
✅ 题目 8:接口组合使用(函数式接口 + Lambda)
📌 场景: 使用 Lambda 实现过滤符合条件的字符串
🧪 涉及知识点:
-
接口
-
Lambda 表达式
-
Stream 操作
✅ 示例题目:过滤字符串长度大于3的元素
List<String> list = Arrays.asList("a", "abcd", "hello", "hi");
list.stream().filter(s -> s.length() > 3).forEach(System.out::println);
✅ 题目 9:实现 Comparable 的排序问题
📌 场景: 为自定义类实现 Comparable,实现默认排序规则。
🧪 涉及知识点:
-
接口实现
-
compareTo方法
-
TreeSet 排序
✅ 示例代码:
class Book implements Comparable<Book> {String title;double price;Book(String title, double price) {this.title = title;this.price = price;}public int compareTo(Book other) {return Double.compare(this.price, other.price);}
}
✅ 题目 10:命令行词频统计工具
📌 场景: 用户输入一段字符串,统计每个单词出现次数
🧪 涉及知识点:
-
Scanner 输入
-
Map 集合
-
字符串拆分
✅ 示例代码:
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();
String[] words = input.split("\\s+");Map<String, Integer> freq = new HashMap<>();
for (String word : words) {freq.put(word, freq.getOrDefault(word, 0) + 1);
}for (Map.Entry<String, Integer> entry : freq.entrySet()) {System.out.println(entry.getKey() + ": " + entry.getValue());
}