本文目录导读:

我来为您提供几个经典且实用的Java简单设计案例,从易到难逐步进阶。
学生成绩管理系统(基础入门)
import java.util.ArrayList;
import java.util.Scanner;
public class StudentGradeSystem {
private static class Student {
String name;
int id;
double score;
Student(String name, int id, double score) {
this.name = name;
this.id = id;
this.score = score;
}
@Override
public String toString() {
return "学号:" + id + " 姓名:" + name + " 成绩:" + score;
}
}
private ArrayList<Student> students = new ArrayList<>();
private Scanner scanner = new Scanner(System.in);
public void addStudent() {
System.out.print("请输入学号: ");
int id = scanner.nextInt();
System.out.print("请输入姓名: ");
String name = scanner.next();
System.out.print("请输入成绩: ");
double score = scanner.nextDouble();
students.add(new Student(name, id, score));
System.out.println("添加成功!");
}
public void showAll() {
if (students.isEmpty()) {
System.out.println("暂无学生信息");
return;
}
System.out.println("=== 学生成绩列表 ===");
for (Student s : students) {
System.out.println(s);
}
}
public void calculateAverage() {
if (students.isEmpty()) {
System.out.println("暂无学生信息");
return;
}
double sum = 0;
for (Student s : students) {
sum += s.score;
}
System.out.printf("平均成绩: %.2f\n", sum / students.size());
}
public void searchStudent() {
System.out.print("请输入学号查询: ");
int id = scanner.nextInt();
for (Student s : students) {
if (s.id == id) {
System.out.println("找到学生: " + s);
return;
}
}
System.out.println("未找到该学生");
}
public void menu() {
while (true) {
System.out.println("\n=== 学生成绩管理系统 ===");
System.out.println("1. 添加学生");
System.out.println("2. 查看所有学生");
System.out.println("3. 计算平均成绩");
System.out.println("4. 查询学生");
System.out.println("5. 退出");
System.out.print("请选择: ");
int choice = scanner.nextInt();
switch (choice) {
case 1: addStudent(); break;
case 2: showAll(); break;
case 3: calculateAverage(); break;
case 4: searchStudent(); break;
case 5:
System.out.println("再见!");
return;
default: System.out.println("无效选择");
}
}
}
public static void main(String[] args) {
new StudentGradeSystem().menu();
// 使用示例
}
}
简易银行账户系统(面向对象设计)
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class BankSystem {
// 账户类
static class Account {
private String accountNumber;
private String name;
private double balance;
private String openDate;
public Account(String accountNumber, String name, double initialDeposit) {
this.accountNumber = accountNumber;
this.name = name;
this.balance = initialDeposit;
this.openDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
}
// 存款
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
System.out.println("存款成功!当前余额: " + balance);
} else {
System.out.println("存款金额必须大于0");
}
}
// 取款
public void withdraw(double amount) {
if (amount > 0 && amount <= balance) {
balance -= amount;
System.out.println("取款成功!当前余额: " + balance);
} else if (amount > balance) {
System.out.println("余额不足,当前余额: " + balance);
} else {
System.out.println("取款金额必须大于0");
}
}
public void showInfo() {
System.out.println("=== 账户信息 ===");
System.out.println("账号: " + accountNumber);
System.out.println("户名: " + name);
System.out.println("余额: " + balance);
System.out.println("开户日期: " + openDate);
}
public String getAccountNumber() {
return accountNumber;
}
}
// 银行系统
static class Bank {
private Account[] accounts = new Account[100];
private int accountCount = 0;
private Scanner scanner = new Scanner(System.in);
public void createAccount() {
if (accountCount >= 100) {
System.out.println("账户已满,无法创建新账户");
return;
}
System.out.print("请输入姓名: ");
String name = scanner.next();
System.out.print("请输入开户金额: ");
double initialDeposit = scanner.nextDouble();
String accountNumber = "2024" + String.format("%03d", accountCount + 1);
accounts[accountCount++] = new Account(accountNumber, name, initialDeposit);
System.out.println("开户成功!账号: " + accountNumber);
}
private Account findAccount(String accountNumber) {
for (int i = 0; i < accountCount; i++) {
if (accounts[i].getAccountNumber().equals(accountNumber)) {
return accounts[i];
}
}
return null;
}
public void deposit() {
System.out.print("请输入账号: ");
String accountNumber = scanner.next();
Account account = findAccount(accountNumber);
if (account == null) {
System.out.println("账号不存在");
return;
}
System.out.print("请输入存款金额: ");
double amount = scanner.nextDouble();
account.deposit(amount);
}
public void withdraw() {
System.out.print("请输入账号: ");
String accountNumber = scanner.next();
Account account = findAccount(accountNumber);
if (account == null) {
System.out.println("账号不存在");
return;
}
System.out.print("请输入取款金额: ");
double amount = scanner.nextDouble();
account.withdraw(amount);
}
public void showAccountInfo() {
System.out.print("请输入账号: ");
String accountNumber = scanner.next();
Account account = findAccount(accountNumber);
if (account == null) {
System.out.println("账号不存在");
} else {
account.showInfo();
}
}
public void menu() {
while (true) {
System.out.println("\n=== 银行账户管理系统 ===");
System.out.println("1. 开户");
System.out.println("2. 存款");
System.out.println("3. 取款");
System.out.println("4. 查询账户信息");
System.out.println("5. 退出");
System.out.print("请选择: ");
int choice = scanner.nextInt();
switch (choice) {
case 1: createAccount(); break;
case 2: deposit(); break;
case 3: withdraw(); break;
case 4: showAccountInfo(); break;
case 5:
System.out.println("感谢使用!");
return;
default: System.out.println("无效选择");
}
}
}
}
public static void main(String[] args) {
Bank bank = new Bank();
bank.menu();
}
}
日历与记事本(文件操作)
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;
public class CalendarNote {
private Map<String, List<String>> notes = new HashMap<>();
private static final String FILE_NAME = "notes.txt";
private Scanner scanner = new Scanner(System.in);
public CalendarNote() {
loadNotes();
}
// 添加记事
public void addNote() {
System.out.print("请输入日期(格式: yyyy-MM-dd): ");
String date = scanner.next();
scanner.nextLine(); // 消耗换行符
System.out.print("请输入记事内容: ");
String content = scanner.nextLine();
notes.computeIfAbsent(date, k -> new ArrayList<>()).add(content);
saveNotes();
System.out.println("记事添加成功!");
}
// 查看指定日期的记事
public void showNotes() {
System.out.print("请输入日期(格式: yyyy-MM-dd): ");
String date = scanner.next();
List<String> dayNotes = notes.get(date);
if (dayNotes == null || dayNotes.isEmpty()) {
System.out.println("该日期没有记事");
} else {
System.out.println("=== " + date + " 的记事 ===");
int i = 1;
for (String note : dayNotes) {
System.out.println(i++ + ". " + note);
}
}
}
// 显示本月日历
public void showCalendar() {
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
cal.set(year, month, 1);
int firstDayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
int daysInMonth = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
System.out.println("\n " + year + "年" + (month + 1) + "月");
System.out.println("一 二 三 四 五 六 日");
// 打印空格
for (int i = 1; i < firstDayOfWeek; i++) {
System.out.print(" ");
}
int today = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
for (int day = 1; day <= daysInMonth; day++) {
String dayStr = String.format("%2d", day);
if (day == today) {
System.out.print("[" + dayStr + "]"); // 标记今天
} else {
System.out.print(" " + dayStr + " ");
}
System.out.print(" ");
if ((day + firstDayOfWeek - 1) % 7 == 0) {
System.out.println();
}
}
System.out.println("\n");
}
// 保存笔记到文件
private void saveNotes() {
try (PrintWriter writer = new PrintWriter(new FileWriter(FILE_NAME))) {
for (Map.Entry<String, List<String>> entry : notes.entrySet()) {
writer.println(entry.getKey());
for (String note : entry.getValue()) {
writer.println(note);
}
writer.println("---END---");
}
} catch (IOException e) {
System.out.println("保存失败: " + e.getMessage());
}
}
// 从文件加载笔记
private void loadNotes() {
File file = new File(FILE_NAME);
if (!file.exists()) return;
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
String line;
while ((line = reader.readLine()) != null) {
String date = line;
List<String> dayNotes = new ArrayList<>();
while ((line = reader.readLine()) != null && !line.equals("---END---")) {
dayNotes.add(line);
}
notes.put(date, dayNotes);
}
} catch (IOException e) {
System.out.println("加载失败: " + e.getMessage());
}
}
public void menu() {
while (true) {
System.out.println("\n=== 日历与记事本 ===");
System.out.println("1. 查看本月日历");
System.out.println("2. 添加记事");
System.out.println("3. 查看记事");
System.out.println("4. 退出");
System.out.print("请选择: ");
int choice = scanner.nextInt();
switch (choice) {
case 1: showCalendar(); break;
case 2: addNote(); break;
case 3: showNotes(); break;
case 4:
System.out.println("再见!");
return;
default: System.out.println("无效选择");
}
}
}
public static void main(String[] args) {
new CalendarNote().menu();
}
}
简易计算器(GUI版)
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Calculator extends JFrame implements ActionListener {
private JTextField display;
private double num1 = 0, num2 = 0, result = 0;
private char operator;
private boolean isOperatorClicked = false;
public Calculator() {
setTitle("简易计算器");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout(5, 5));
// 显示区域
display = new JTextField(20);
display.setFont(new Font("Arial", Font.BOLD, 20));
display.setHorizontalAlignment(JTextField.RIGHT);
display.setEditable(false);
add(display, BorderLayout.NORTH);
// 按钮面板
JPanel buttonPanel = new JPanel(new GridLayout(4, 4, 5, 5));
String[] buttons = {
"7", "8", "9", "/",
"4", "5", "6", "*",
"1", "2", "3", "-",
"0", ".", "=", "+"
};
for (String text : buttons) {
JButton button = new JButton(text);
button.setFont(new Font("Arial", Font.BOLD, 18));
button.addActionListener(this);
buttonPanel.add(button);
}
add(buttonPanel, BorderLayout.CENTER);
// 清除按钮
JButton clearButton = new JButton("C");
clearButton.setFont(new Font("Arial", Font.BOLD, 18));
clearButton.addActionListener(e -> {
display.setText("0");
num1 = num2 = result = 0;
isOperatorClicked = false;
});
add(clearButton, BorderLayout.SOUTH);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if (command.charAt(0) >= '0' && command.charAt(0) <= '9' || command.equals(".")) {
if (isOperatorClicked) {
display.setText(command);
isOperatorClicked = false;
} else {
String text = display.getText();
if (text.equals("0") && !command.equals(".")) {
display.setText(command);
} else {
display.setText(text + command);
}
}
} else if (command.equals("=")) {
num2 = Double.parseDouble(display.getText());
calculate();
display.setText(String.valueOf(result));
isOperatorClicked = true;
} else {
if (!isOperatorClicked) {
num1 = Double.parseDouble(display.getText());
operator = command.charAt(0);
isOperatorClicked = true;
}
}
}
private void calculate() {
switch (operator) {
case '+': result = num1 + num2; break;
case '-': result = num1 - num2; break;
case '*': result = num1 * num2; break;
case '/':
if (num2 != 0) {
result = num1 / num2;
} else {
display.setText("错误: 除数不能为0");
return;
}
break;
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(Calculator::new);
}
}
简易购物车系统(综合应用)
import java.util.*;
public class ShoppingCartSystem {
// 商品类
static class Product {
private String id;
private String name;
private double price;
private int stock;
public Product(String id, String name, double price, int stock) {
this.id = id;
this.name = name;
this.price = price;
this.stock = stock;
}
public String getId() { return id; }
public String getName() { return name; }
public double getPrice() { return price; }
public int getStock() { return stock; }
public void setStock(int stock) { this.stock = stock; }
@Override
public String toString() {
return String.format("编号:%-5s 名称:%-10s 价格:%.2f 库存:%d", id, name, price, stock);
}
}
// 购物车项
static class CartItem {
private Product product;
private int quantity;
public CartItem(Product product, int quantity) {
this.product = product;
this.quantity = quantity;
}
public Product getProduct() { return product; }
public int getQuantity() { return quantity; }
public void setQuantity(int quantity) { this.quantity = quantity; }
public double getSubtotal() {
return product.getPrice() * quantity;
}
}
// 购物车
static class ShoppingCart {
private List<CartItem> items = new ArrayList<>();
public void addItem(Product product, int quantity) {
for (CartItem item : items) {
if (item.getProduct().getId().equals(product.getId())) {
item.setQuantity(item.getQuantity() + quantity);
product.setStock(product.getStock() - quantity);
return;
}
}
items.add(new CartItem(product, quantity));
product.setStock(product.getStock() - quantity);
}
public void removeItem(String productId) {
items.removeIf(item -> item.getProduct().getId().equals(productId));
}
public double getTotalPrice() {
return items.stream().mapToDouble(CartItem::getSubtotal).sum();
}
public void showCart() {
if (items.isEmpty()) {
System.out.println("购物车为空");
return;
}
System.out.println("\n=== 购物车 ===");
int i = 1;
for (CartItem item : items) {
System.out.printf("%d. %s x %d = %.2f\n",
i++, item.getProduct().getName(),
item.getQuantity(), item.getSubtotal());
}
System.out.printf("总计: %.2f\n", getTotalPrice());
}
public List<CartItem> getItems() {
return items;
}
}
public static void main(String[] args) {
// 初始化商品
List<Product> products = new ArrayList<>(Arrays.asList(
new Product("P001", "苹果", 5.5, 100),
new Product("P002", "香蕉", 3.5, 150),
new Product("P003", "牛奶", 15.0, 80),
new Product("P004", "面包", 8.0, 60),
new Product("P005", "鸡蛋", 1.5, 200)
));
ShoppingCart cart = new ShoppingCart();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("\n=== 购物车系统 ===");
System.out.println("1. 查看商品列表");
System.out.println("2. 加入购物车");
System.out.println("3. 查看购物车");
System.out.println("4. 结算");
System.out.println("5. 退出");
System.out.print("请选择: ");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.println("\n=== 商品列表 ===");
for (Product p : products) {
System.out.println(p);
}
break;
case 2:
System.out.print("请输入商品编号: ");
String productId = scanner.next();
Product selected = null;
for (Product p : products) {
if (p.getId().equals(productId)) {
selected = p;
break;
}
}
if (selected == null) {
System.out.println("商品不存在");
break;
}
System.out.print("请输入数量: ");
int quantity = scanner.nextInt();
if (quantity <= 0 || quantity > selected.getStock()) {
System.out.println("无效数量或库存不足");
break;
}
cart.addItem(selected, quantity);
System.out.println("添加成功!");
break;
case 3:
cart.showCart();
break;
case 4:
cart.showCart();
if (!cart.getItems().isEmpty()) {
System.out.println("确认结算吗?(y/n)");
String confirm = scanner.next();
if (confirm.equalsIgnoreCase("y")) {
System.out.println("结算成功!总金额: " + cart.getTotalPrice());
cart.getItems().clear();
}
}
break;
case 5:
System.out.println("欢迎下次光临!");
return;
default:
System.out.println("无效选择");
}
}
}
}
设计建议
学习路线:
- 学生成绩系统 - 掌握基础语法和集合
- 银行账户系统 - 理解面向对象思想
- 日历记事本 - 学习文件操作
- GUI计算器 - 了解图形界面
- 购物车系统 - 综合应用
进阶优化方向:
- 加入数据库存储(MySQL/SQLite)
- 使用设计模式(工厂、单例等)
- 添加异常处理机制
- 实现用户登录注册
- 使用多线程处理并发访问
这些案例涵盖了Java开发的常见场景,通过实践能快速提升编程能力。
版权声明:NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播