0

我正在编写一个简单的 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();
    }
}

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

4

2 回答 2

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

    }
}
于 2013-09-25T14:54:40.700 回答
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;
}
于 2013-09-25T14:57:02.047 回答