[英]How can I loop this based on the scanner input?
我正在为第二学期的Java课程分配作业,但在尝试弄清楚如何最好地循环这种方法时遇到了一些麻烦。 我需要在每个循环的开头弹出“选择一个选项”行,并允许用户输入a,b,c或q。 如果用户选择a,b,c,我希望它执行操作,然后返回“选择选项”行并重新开始,如果用户输入q,则应退出。 我应该使用哪种循环?
public void go() throws Exception {
Scanner kb = new Scanner(System.in);
ArrayList<Task> list = new ArrayList<Task>();
TaskManager taskMgr = new TaskManager();
System.out.println("Welcome to MyTaskManager");
System.out.println("choose an option \n a: Add a new item \n b: Display tasks by date \n c: Display tasks by keyword \n q: quit");
System.out.println("Enter your choice");
String choice = kb.nextLine();
if (choice.equalsIgnoreCase("a")) {
taskMgr.makeTasks();
}
else if (choice.equalsIgnoreCase("b")) {
System.out.println("enter the date you wish to view: ");
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
String date=kb.nextLine();
Date dueDate = sdf.parse(date);
taskMgr.findTasksByDate(dueDate);
taskMgr.displayTaskReportByDate(dueDate);
}
else if (choice.equalsIgnoreCase("c")) {
System.out.println(" Enter a keyword: ");
String keyword=kb.nextLine();
System.out.println("Task that contain the term "+keyword);
taskMgr.findTasksByKeyword(keyword);
taskMgr.displayTaskReportByKeyword(keyword);
}
else if (choice.equalsIgnoreCase("q")) {
System.exit(0);
}
else {
System.exit(0);
}
}
您似乎错过了一个循环。 因此,要使用您当前的代码-
while (true) { // <-- or for(;;) {
System.out.println("choose an option \n a: Add a new item \n b: Display tasks by date \n c: Display tasks by keyword \n q: quit");
System.out.println("Enter your choice");
String choice = kb.nextLine();
if (choice.equalsIgnoreCase("a")) {
taskMgr.makeTasks();
} else if (choice.equalsIgnoreCase("b")) {
System.out.println("enter the date you wish to view: ");
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
String date=kb.nextLine();
Date dueDate = sdf.parse(date);
taskMgr.findTasksByDate(dueDate);
taskMgr.displayTaskReportByDate(dueDate);
} else if (choice.equalsIgnoreCase("c")) {
System.out.println(" Enter a keyword: ");
String keyword=kb.nextLine();
System.out.println("Task that contain the term "+keyword);
taskMgr.findTasksByKeyword(keyword);
taskMgr.displayTaskReportByKeyword(keyword);
} else if (choice.equalsIgnoreCase("q")) {
// System.exit(0);
break; //<-- ends the loop.
} // no else and quit. we want to loop.
}
您可以使用while(true)
循环来执行此操作-它永远不会停止(直到退出)。 这是因为while()
通常在其括号内有一个值,如果循环应停止,则该值将变为false。 使用while(true){ACTION}
您具有经典的无限循环,该循环在计算机科学中经常用于需要用户输入的各种应用程序。
public void go() throws Exception {
Scanner kb = new Scanner(System.in);
ArrayList<Task> list = new ArrayList<Task>();
TaskManager taskMgr = new TaskManager();
System.out.println("Welcome to MyTaskManager");
while(true){
System.out.println("choose an option \n a: Add a new item \n b: Display tasks by date \n c: Display tasks by keyword \n q: quit");
System.out.println("Enter your choice");
String choice = kb.next();
if (choice.equalsIgnoreCase("a")) {
taskMgr.makeTasks();
}
else if (choice.equalsIgnoreCase("b")) {
System.out.println("enter the date you wish to view: ");
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
String date=kb.nextLine();
Date dueDate = sdf.parse(date);
taskMgr.findTasksByDate(dueDate);
taskMgr.displayTaskReportByDate(dueDate);
}
else if (choice.equalsIgnoreCase("c")) {
System.out.println(" Enter a keyword: ");
String keyword=kb.nextLine();
System.out.println("Task that contain the term "+keyword);
taskMgr.findTasksByKeyword(keyword);
taskMgr.displayTaskReportByKeyword(keyword);
}
else if (choice.equalsIgnoreCase("q")) {
System.exit(0);
}
else {
System.exit(0);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.