有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何根据扫描器输入循环此操作?

我正在为我的第二学期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);
    }
}

共 (2) 个答案

  1. # 1 楼答案

    你好像错过了一个循环。因此,要使用您当前的代码-

    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.
    }
    
  2. # 2 楼答案

    您可以使用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);
    }
    }
    }