我试图用java创建一个简单的待办事项列表。它要求用户输入一个项目,将其添加到列表中,然后询问他们是否要输入更多项目。如果是,则它会提示他们输入更多项目并再次运行,如果没有则会打印出列表并结束程序。 到目前为止我的代码是:
import java.util.Scanner;
public class ToDoDriver
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
ToDoList list = new ToDoList();
System.out.println("Enter items for the list when prompted.");
System.out.println("Add Item:");
String task = input.nextLine();
list.add(task);
boolean more;
System.out.println("Do you wish to add more items?");
more = Boolean.parseBoolean(input.nextLine());
while(more == true)
{
System.out.println("Do you wish to add more items?");
more = Boolean.parseBoolean(input.nextLine());
System.out.println("Add Item: ");
task = input.nextLine();
list.add(task);
}
System.out.println(list);
}
}
答案 0 :(得分:1)
此代码将执行您想要的操作。请注意,您必须说“是”或不会继续。
import java.util.Scanner;
public class ToDoDriver {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
ToDoList list = new ToDoList();
boolean stillAdding = false;
String task;
String continuing;
System.out.println("Enter items for the list when prompted.");
do {
System.out.println("Add Item:");
task = input.nextLine();
list.add(task);
System.out.println("Do you wish to add more items?");
continuing = input.nextLine();
if (continuing.equalsIgnoreCase("Yes") {
stillAdding = true;
} else {
stillAdding = false;
}
} while (stillAdding)
System.out.println("Done adding");
}
}
希望这有帮助!