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

时间:2018-01-11 07:08:19

标签: java project

我试图建立一个基于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 个答案:

答案 0 :(得分:0)

这是因为你没有要求再次从用户那里阅读。

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

或者将input.nextInt();移到switch区域之外(位于其下方)。

答案 1 :(得分:0)

由于userChoice始终为1,因此它始终循环。

答案 2 :(得分:0)

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");

            }
        }



    }



}