【问题标题】:Control Flow in Multiple Method Program多方法程序中的控制流程
【发布时间】:2013-09-25 14:41:57
【问题描述】:

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

艾莉森,F,2013 年 4 月,伦敦

程序构建如下:

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

  1. 记录人
  2. 搜索所有名为 [name] 的人
  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();
    }
}

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

【问题讨论】:

  • 作为技术说明,您可以只说while (true),因为如果myCommand == 5,其中一个if 语句将exit,尽管exit 不是特别好的做法程序的中间。

标签: java loops control-flow


【解决方案1】:
public void someMethod() {
  while(isRunning) {

  chooseCommand();

  }
}

然后在chooseCommand()中丢失循环,使选项5设置isRunning = false而不是exit(),并使用switch语句来美观。

例如

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

    }
}

【讨论】:

    【解决方案2】:
    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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-10
      • 1970-01-01
      相关资源
      最近更新 更多