我正在尝试制作一个基于控制台的待办事项列表。
import java.util.Scanner;
// Press Shift twice to open the Search Everywhere dialog and type `show whitespaces`,
// then press Enter. You can now see whitespace characters in your code.
public class Main {
public static final Scanner terminal = new Scanner(System.in);
public static void main(String[] args) {
to_do toDo = new to_do(terminal);
toDo.Menu_loop();
terminal.close();
}
}
import java.util.ArrayList;
import java.util.Scanner;
public class to_do {
private ArrayList <String> todo_list;
public static Scanner keyboard;
public to_do(Scanner scanner) {
keyboard = scanner;
todo_list = new ArrayList <String>();
}
public void Menu_loop() {
System.out.println("Welcome to to_o list please select a command:");
printMenu();
String userOption = "-1";
while (!userOption.equals("4")) {
userOption = keyboard.next();
switch (userOption) {
case "1" -> {
show_list();
printMenu();
}
case "2" -> {
add_list();
printMenu();
}
case "3" -> {
remove_list();
printMenu();
}
case "4" -> printQuitMessage();
default -> {
System.out.println();
System.out.println("Error non valid command");
printMenu();
}
}
}
}
public void show_list() {
if (todo_list == null) {
System.out.println("empty please add something");
} else {
for (String i: todo_list) {
System.out.println(i);
}
}
}
public void add_list() {
System.out.println("\n please write what you want to add: ");
String name = keyboard.next();
todo_list.add(name);
System.out.println("\n item: " + name + " added to list");
}
public void remove_list() {
System.out.println("\nplease write what you want to erase: ");
String name = keyboard.next();
if (!todo_list.contains(name) || todo_list == null) {
System.out.println("error: your task does not exist or your list is empty");
} else {
todo_list.remove(name);
System.out.println("item: " + name + " removed from list");
}
}
public void printMenu() {
System.out.println();
System.out.println("Enter 1 - check list");
System.out.println("Enter 2 - add task");
System.out.println("Enter 3 - remove task");
System.out.println("Enter 4 - Quit");
System.out.print("Choice:");
}
private void printQuitMessage() {
System.out.println("Closing the program bye bye!");
}
}
我的问题是函数
add_list
和show_list
。
当我尝试检查列表中是否存在任何项目时,开头总是null
。
它应该会收到一条消息说
Empty please add something
如下代码:
public void show_list() {
if(todo_list == null) {
System.out.println("empty please add something");
} else {
for(String i : todo_list) {
System.out.println(i);
}
}
}
但是我得到这个:
正如您在两个菜单打印之间看到的那样,当列表为空时它应该有消息
另一个问题是当我尝试输入字符串时: “喂狗” 当我尝试只检查单词 feed 是否存储在
add_list
.
public void add_list() {
System.out.println("\n please write what you want to add: ");
String name = keyboard.next();
todo_list.add(name);
System.out.println("\n item: " + name + " added to list");
}
Welcome to to_o list please select a command:
Enter 1 - check list
Enter 2 - add task
Enter 3 - remove task
Enter 4 - Quit
Choice:2
please write what you want to add:
feed the dog
item: feed added to list
Enter 1 - check list
Enter 2 - add task
Enter 3 - remove task
Enter 4 - Quit
Choice:
Error non valid command
Enter 1 - check list
Enter 2 - add task
Enter 3 - remove task
Enter 4 - Quit
Choice:
Error non valid command
Enter 1 - check list
Enter 2 - add task
Enter 3 - remove task
Enter 4 - Quit
Choice:1
feed
Enter 1 - check list
Enter 2 - add task
Enter 3 - remove task
Enter 4 - Quit
Choice:1
feed
Enter 1 - check list
Enter 2 - add task
Enter 3 - remove task
Enter 4 - Quit
Choice:2
please write what you want to add:
de
item: de added to list
Enter 1 - check list
Enter 2 - add task
Enter 3 - remove task
Enter 4 - Quit
Choice:1
feed
de
Enter 1 - check list
Enter 2 - add task
Enter 3 - remove task
Enter 4 - Quit