You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何实现Java简易待办事项列表的循环添加与退出功能?

Hey there! Looks like you're off to a solid start with your Java to-do list program. Let's finish it up so it fully meets your requirements—accepting multiple items, asking to continue adding, and printing the complete list when you're done.

First, I'll assume you haven't fully implemented the ToDoList class yet, so I'll include a simple, functional version of that alongside the updated driver code.

Complete Implementation

ToDoList Class (Handles Storage and Printing)

import java.util.ArrayList;
import java.util.List;

public class ToDoList {
    private List<String> items;

    // Initialize the empty list
    public ToDoList() {
        items = new ArrayList<>();
    }

    // Add a new item to the list
    public void addItem(String item) {
        items.add(item);
    }

    // Print all items with numbered formatting
    public void printList() {
        System.out.println("\nYour Final To-Do List:");
        for (int i = 0; i < items.size(); i++) {
            System.out.printf("%d. %s%n", i + 1, items.get(i));
        }
    }
}

ToDoDriver Class (Updated User Interaction Logic)

import java.util.Scanner;

public class ToDoDriver {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        ToDoList list = new ToDoList();
        String userChoice;

        System.out.println("Let's build your to-do list!");
        
        // Use do-while to ensure at least one item is entered
        do {
            // Prompt for a to-do item
            System.out.print("Enter a to-do item: ");
            String newItem = input.nextLine();
            list.addItem(newItem);

            // Ask if user wants to add another item
            System.out.print("Want to add another item? (yes/no): ");
            userChoice = input.nextLine().trim().toLowerCase();

            // Validate input to avoid invalid responses
            while (!userChoice.equals("yes") && !userChoice.equals("no")) {
                System.out.print("Please enter 'yes' or 'no': ");
                userChoice = input.nextLine().trim().toLowerCase();
            }

        } while (userChoice.equals("yes")); // Repeat loop if user says "yes"

        // Print the completed list
        list.printList();
        
        input.close(); // Clean up scanner resource
        System.out.println("\nThanks for using the to-do list!");
    }
}

Key Details Explained

  • Loop Structure: The do-while loop ensures the user is prompted for at least one item, and repeats as long as they choose to add more.
  • Input Validation: A nested loop catches invalid responses (like "maybe" or typos) and asks the user to try again until they enter "yes" or "no".
  • Clean Separation: The ToDoList class handles all list operations (storage, printing), keeping the driver class focused on user interaction.
  • Readable Output: The final list is printed with numbered entries for clarity.

How It Runs

  1. The program greets you and asks for your first to-do item.
  2. After adding an item, it asks if you want to add another.
  3. If you say "yes", it repeats the input process. If "no", it prints your full list and exits.

This should cover all the functionality you wanted. Feel free to tweak the messages or add extra features (like deleting items) if you want to expand it further!

内容的提问来源于stack exchange,提问作者Ryan Asciutto

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 08:02:45