导航菜单

Java项目实战

通过实战项目巩固Java开发技能

90%
控制台应用实战
实现一个简单的命令行记账本,支持添加、查询和删除账目。
import java.util.*;
public class Ledger {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        ArrayList<String> records = new ArrayList<>();
        while (true) {
            System.out.println("1. 添加 2. 查询 3. 删除 0. 退出");
            int op = sc.nextInt(); sc.nextLine();
            if (op == 1) {
                System.out.print("输入账目:");
                records.add(sc.nextLine());
            } else if (op == 2) {
                for (int i = 0; i < records.size(); i++)
                    System.out.println(i + ": " + records.get(i));
            } else if (op == 3) {
                System.out.print("输入要删除的编号:");
                int idx = sc.nextInt();
                if (idx >= 0 && idx < records.size()) records.remove(idx);
            } else if (op == 0) break;
        }
    }
}