简体   繁体   English

如何根据扫描仪的输入来循环显示?

[英]How can I loop this based on the scanner input?

I'm working on a assignment for my second semester java class and I'm having a little trouble trying to figure out how best to loop this. 我正在为第二学期的Java课程分配作业,但在尝试弄清楚如何最好地循环这种方法时遇到了一些麻烦。 I need the "choose an option" line to pop up at the beginning of each loop and allow the user to enter a,b,c or q. 我需要在每个循环的开头弹出“选择一个选项”行,并允许用户输入a,b,c或q。 If the user picks a,b,c I want to it do the action and then to return to the "choose an option" line and start over and if the user enters q it should quit. 如果用户选择a,b,c,我希望它执行操作,然后返回“选择选项”行并重新开始,如果用户输入q,则应退出。 What kind of loop should I use for this? 我应该使用哪种循环?

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);
    }
}

You seem to be missing a loop. 您似乎错过了一个循环。 So, to work with your current code - 因此,要使用您当前的代码-

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.
}

You can do this with while(true) loop - it never stops (until exit). 您可以使用while(true)循环来执行此操作-它永远不会停止(直到退出)。 That is because while() usually has a value in its bracket which changes to false if the loop should stop. 这是因为while()通常在其括号内有一个值,如果循环应停止,则该值将变为false。 with while(true){ACTION} you have the classic infinite loop, used often in computer science for a variety of applications that need user input. 使用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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM