我需要在Java中创建一个可运行的待办事项列表,该列表允许用户执行以下操作:
新增项目
删除项目
显示清单
删除所有任务
退出程序
我目前在将商品添加到列表中时遇到问题。每次输入要添加的项目时,都会收到以下输出:
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));
}
}
最佳答案
您缺少添加从列表中删除的实际操作。 @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方法进一步改进。