如何在java中制作一个待办事项列表?

问题描述 投票:0回答:1

我需要在java中创建一个有效的待办事项列表,允许用户。

  1. 添加一个项目

  2. 删除一个项目

  3. 显示列表

  4. 删除所有任务

  5. 退出程序

我目前在添加项目到我的列表时遇到了麻烦。每次我输入要添加的项目时,我都会收到这样的输出。

Exception in thread "main" java.util.InputMismatchException
at java.base/java.util.Scanner.throwFor(Scanner.java:939)
at java.base/java.util.Scanner.next(Scanner.java:1594)
at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
at Test.menu(Test.java:50)
at Test.main(Test.java:11)

这是我目前所尝试的,任何帮助都将是非常感激的。

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

public class Test {

public static void main(String[] args) {

    int menuItem = -1;

    while(menuItem !=0) {
        menuItem = menu();

        switch(menuItem) {

        case 1: 
            showList();
            break;

        case 2: 
            addItem();
            break;

        case 3: 
            removeItem();
            break;

        case 0: 
            break;

        default: 
            System.out.println("Enter a valid option");
        }

    }
}

public static int menu() {

int choice; 

Scanner keyboard = new Scanner(System.in);
System.out.println("Main Menu");
System.out.println();
System.out.println("0. Exit the program");
System.out.println("1. Display to-do list");
System.out.println("2. Add item to list");
System.out.println("3. Remove item from list");
System.out.println();
System.out.print("Enter choice: ");
choice = keyboard.nextInt();

return choice;
}

public static void showList() {

System.out.println("To-Do List");

Scanner input = new Scanner(System.in);
String line;
int number = 1;

while (input.hasNextLine()){
    line = input.nextLine();
    System.out.println(number + " ");
    System.out.println(line);
    ++number;
}

System.out.println();


}

public static void addItem() {

System.out.println("Add Item");

Scanner input = new Scanner(System.in);
System.out.println("Enter an item: ");
String item = input.nextLine();
System.out.println(item);


}

public static void removeItem() {

int choice;
showList();

Scanner input = new Scanner(System.in);
System.out.println("What do you want to remove?");
choice = input.nextInt();

ArrayList<String> items = new ArrayList<String>();
int number = 1;

Scanner input2 = new Scanner(System.in);
String item; 

while (input2.hasNextLine()) {
    item = input2.nextLine();

    if (number != choice) 
        items.add(item);

    ++number;   
}

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

    }
}
java arraylist java.util.scanner
1个回答
0
投票

你错过了从列表中添加和删除的实际操作。@DrMe通过这些程序你可以找到你做错的地方。希望能帮到你

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

public class TestToDoList {

    private static List<String> currentList = new ArrayList<String>();

    public static void main(String[] args) {

        int menuItem = -1;
        while (menuItem != 0) {
            menuItem = menu();
            switch (menuItem) {
            case 1:
                showList();
                break;
            case 2:
                addItem();
                break;
            case 3:
                removeItem();
                break;
            case 0:
                break;
            default:
                System.out.println("Enter a valid option");
            }
        }
    }

    public static int menu() {
        System.out.println();
        System.out.println("----------------------");
        System.out.println("Main Menu");
        System.out.println("----------------------");
        System.out.println("0. Exit the program");
        System.out.println("1. Display to-do list");
        System.out.println("2. Add item to list");
        System.out.println("3. Remove item from list");
        System.out.println();
        System.out.print("Enter choice: ");
        int choice = scanner.nextInt();
        return choice;
    }

    public static void showList() {
        System.out.println();
        System.out.println("----------------------");       
        System.out.println("To-Do List");
        System.out.println("----------------------");
        int number = 0;
        for (String item : currentList) {
            System.out.println(++number + " " + item);
        }
        System.out.println("----------------------");


    }

    public static void addItem() {
        System.out.println("Add Item");
        System.out.println("----------------------");
        System.out.print("Enter an item: ");
        Scanner scanner = new Scanner(System.in);
        String item = scanner.nextLine();
        currentList.add(item);
        showList();
    }

    public static void removeItem() {
        System.out.println("Remove Item");
        System.out.println("----------------------");
        Scanner scanner = new Scanner(System.in);
        System.out.print("What do you want to remove?");
        int index = scanner.nextInt();
        if((index-1)<0 || index>currentList.size()) {
            System.out.println("Wrong index number! Please enter in range of 1 to "+currentList.size());            
        }else {
            currentList.remove(index-1);
        }
        System.out.println("----------------------");
        showList();


    }
}

使用通用和utils方法进一步改进。

© www.soinside.com 2019 - 2024. All rights reserved.