提问者:小点点

在待办事项列表中显示项目的困难


我试图用Java构建一个基于文本的待办事项列表,但在显示项目时遇到了一些困难。

当我运行代码并输入“1”时,待办事项列表的内容会显示给我,但它们一直在循环,并且永远不会停止。我假设这与检查 userChoice 变量的 while 循环有关,但我的问题是为什么即使在 break 语句之后列表仍在不断重申?我希望发生的是输入一个数字,执行操作,然后再次显示指令提示。

java代码:

package com.company;

import java.util.ArrayList;
import java.util.Scanner;

public class Main {

// create an arraylist to store users items
static ArrayList<String> toDoList = new ArrayList<String>(3);


public static void main(String[] args) {

    // greet the user
    System.out.println("**Your To-Do list** \n");

    // add default items to list
    toDoList.add("Buy Groceries");
    toDoList.add("Work Out");
    toDoList.add("Play CS");

    // user menu/instruction
    System.out.println("Please select from one of the following options: \n 1. Show to-do list \n 2. Add item " +
            "\n 3. Remove item \n 4. Exit program \n");

    // prompt user for their choice
    System.out.print("Enter your choice: ");

    // get user choice
    Scanner input = new Scanner(System.in);
    int userChoice = input.nextInt();

    while (userChoice != 4) {
        switch (userChoice) {
            case 1:
                getToDoList();
                break;

            case 2:
                // create method that allows you to add item to the toDolist
                break;

            case 3:
                // create method that allows you to remove item from the toDolist
                break;

            case 4:
                // create method that terminates application
                break;
        }
    }

}

// method that returns contents of the list
public static void getToDoList(){

    for (int i = 0; i < toDoList.size(); i++) {
        System.out.println(toDoList.get(i));
    }
}

}

共3个答案

匿名用户

这是因为您没有再次请求读取用户。

case 1:
    getToDoList();
    input.nextInt();
    break;

或者将 input.nextInt(); 移到开关块的外部(在其下方)。

匿名用户

因为 userChoice 始终为 1,所以它始终循环。

匿名用户

import java.util.ArrayList;
import java.util.Scanner;

public class Groceries {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        ArrayList<String> a=new ArrayList<String>();
        a.add("Food");
        a.add("Furniture");
        a.add("Plywood");



        while(true)
        {

            System.out.println("Enter your choice");
            System.out.println("Your choice List\n 1:getList 2.addinthelist 3.removefromlist 4.exit");

            Scanner sc=new Scanner(System.in);
            int a1=sc.nextInt();


            switch(a1)
            {
            case 1:
                System.out.println(a);
                break;

            case 2:
                System.out.println("List before addition of elemnt is :"+a);
                System.out.println("Enter element to be added into the string");
                String sss=sc.next();

                a.add(sss);
                System.out.println("List after addition of element is :"+a);

                    break;

            case 3:
                System.out.println("List before deletion of elemnt is "+a);
                System.out.println("Enter an index of an element to be removed");
                int abc=sc.nextInt();
                a.remove(abc);
                System.out.println("List after Deletion of an element is "+a);
                break;

            case 4:
                System.exit(0);


            default:
                System.out.println("You entered wrong number !! Please enter 4 to exit");

            }
        }



    }



}