如何根据扫描仪输入进行循环?

时间:2014-04-23 01:18:37

标签: java loops

我正在为我的第二学期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 个答案:

答案 0 :(得分: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.
}

答案 1 :(得分:0)

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