多方法程序中的控制流程

时间:2013-09-25 14:41:58

标签: java loops control-flow

我正在编写一个简单的Java程序,以熟悉方法,记录一个人遇到的人。例如,如果我在伦敦遇见艾莉森,我会记录以下内容(格式:姓名,性别,时间,地点):

艾利森,F,apr2013,伦敦

该计划的构建如下:

为用户提供了不同的机会:

  1. 记录人
  2. 搜索名为[name]
  3. 的所有人
  4. 搜索遇到的所有人[地方]
  5. 获取可用命令列表
  6. 退出
  7. 这是我的代码的骨架:

        public void chooseCommand() throws FileNotFoundException {
        System.out.println("Enter command: ");
        text = input.next();
        myCommand = Integer.parseInt(text);
    
        while (myCommand !=5) {
            if (myCommand == 1) {
                writeToFile();    //Log new person
            }
            // Search for person type
            else if (myCommand == 2) {
                searchFor();    // Search for person by name
            }
            // Search for place
            else if (myCommand == 3) {
                searchFor();    // Search for person by place
            }
            // help
            else if (myCommand == 4) {
                showCommands();  // get a list of available commands        
            }
            else if (myCommand == 5) {
                exit();
            }
            // default
            else {
                System.out.println("Command not found");
            }
        }
    }
    

    这很好用。但是,在我选择五个选项之一(记录新人,搜索名称,搜索地点,帮助,退出)之后,我想回到chooseCommand()方法,以便再次提供相同的选项而不是让最初选择的方法无限循环。也就是说,在我登录一个新人之后,我希望能够获得新的选择,而不是必须永远记录新的人,而不会杀死该程序。

        // REGISTER
    public void writeToFile() {
        // Write to file
        try {
            BufferedWriter output = new BufferedWriter(new FileWriter(file, true)); 
            System.out.println("Enter sighting: ");
    
            for (int i = 0; i < personBlank.length; i++) {
                System.out.println(personInfo[i] + ": ");
                personEntry = input.next();
                personBlank[i] = personEntry;
            }
    
            // change to toString() method
            observed = personBlank[0] + "," + personBlank[1] + "," + personBlank[2] + "," + personBlank[3];
    
            if (observed.equals(escape)) {
                exit();
            }
            else {
                output.write(observed);    // log new person 
                output.newLine();
                output.close();
            }
            back();
        }
        catch (IOException e){
            e.printStackTrace();
        }
    }
    

    对此有任何帮助,我们非常感谢!

2 个答案:

答案 0 :(得分:1)

public void someMethod() {
  while(isRunning) {

  chooseCommand();

  }
}

然后在chooseCommand()中输掉循环,make option 5设置isRunning = false而不是exit(),并使用switch语句来表示漂亮。

e.g。

public void chooseCommand() throws FileNotFoundException {
    System.out.println("Enter command: ");
    text = input.next();
    myCommand = Integer.parseInt(text);

    switch (myCommand) {
        case 1: 
            writeToFile();    //Log new person
            break;

        case 2:
        // Search for place
            break;

        case 3: 
            searchFor();    // Search for person by place
            break;

        // help
        case 4:
            showCommands();  // get a list of available commands 
            break;       

        case 5: 
            this.isRunning = false;
            break;

        default:
            System.out.println("Command not found");

    }
}

答案 1 :(得分:0)

in the place of your code where chooseCommand() was called you could use a boolean and   check that boolean is true to call chooseCommand()

java pseudocode
------------------
boolean continue=true;

while(continue)
{
System.out.println("Do you want to continue?");

Scanner scan=new Scanner(System.in);

if(scan.nextLine().equals("true"))

chooseCommand();

else 

continue = false;
}