2013-09-25 18 views
0

我正在写一个简单的Java程序,以熟悉方法,记录一个人遇到的人。举例来说,如果我在伦敦开会艾莉森,我会记录以下(格式:姓名,性别,何时,何地):多种方法中的控制流程程序

艾莉森,F,apr2013,伦敦

该方案是建立如下:

的用户提供不同的机会:

  1. 日志人
  2. 搜索名为[名]
  3. 搜索所有的人所有的人遇到[地方]
  4. 获取可用的命令列表
  5. 退出

这里是我的代码骨架:

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

对此的任何帮助都非常感谢!

+0

作为一个技术说明,你可以说',而(真)',因为如果'mycommand的== 5'的if语句中的一个将'exit',虽然它并不特别好的做法是在程序中间退出。 – Dukeling

回答

1
public void someMethod() { 
    while(isRunning) { 

    chooseCommand(); 

    } 
} 
在chooseCommand

则()失去了环路,使选择5组isRunning =假,而不是退出(),并使用一个开关语句prettyness。

例如

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

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