英文:
Prompting user with a print after each Switch-case except case: "0 exit"
问题
以下是您要翻译的内容:
"Instead of making it blank each time the user is finished with a case, I've added a println("Next command?>") in the runCommandLoop method as a cheap solution to my problem. But that also means when case "0" or "exit" is choosen, "Next command?>" is printed out one last time BEFORE shutting down.
Simply put I want it to prompt after every case except "0 shutdown".
Calling the startup method is another option as well I guess but that doesn't make much difference either.
private void startup() {
System.out.println("\nCommand?>\n" + "1: register new dog\n" + "2: increase age\n" + "3: list dogs\n"
+ "4: remove dog\n" + "0: exit\n");
}
private void runCommandLoop() {
boolean done;
do {
String command = readCommand();
done = handleCommand(command);
System.out.println("\nNext command?>");
} while (!done);
}
private String readCommand() {
String command = input.registerString();
return command;
}
private boolean handleCommand(String command) {
switch (command) {
case "1":
case "register new dog":
dogList.registerDog();
break;
case "2":
case "increase age":
dogList.increaseAge();
break;
case "3":
case "list dogs":
dogList.listDogs();
break;
case "4":
case "remove dog":
dogList.removeDog();
break;
case "0":
case "exit":
return true;
default:
printMenu();
}
return false;
}
private void printMenu() {
System.out.println("Error: wrong command\n" + "The available commands are:\n" + "1: register new dog\n"
+ "2: increase age\n" + "3: list dogs\n" + "4: remove dog\n" + "0: exit");
return;
}
private void closeDown() {
System.out.println("Shutting down...");
input.closeInput();
}
private void run() {
startup();
runCommandLoop();
closeDown();
}
public static void main(String[] args) {
Interface startup = new Interface();
startup.run();
}"
请注意,我已经将代码部分保留为原文,只翻译了您要求的文本部分。
英文:
Instead of making it blank each time the user is finished with a case, I've added a println("Next command?>") in the runCommandLoop method as a cheap solution to my problem. But that also means when case "0" or "exit" is choosen, "Next command?>" is printed out one last time BEFORE shutting down.
Simply put I want it to prompt after every case except "0 shutdown".
Calling the startup method is another option as well I guess but that doesn't make much difference either.
private void startup() {
System.out.println("\nCommand?>\n" + "1: register new dog\n" + "2: increase age\n" + "3: list dogs\n"
+ "4: remove dog\n" + "0: exit\n");
}
private void runCommandLoop() {
boolean done;
do {
String command = readCommand();
done = handleCommand(command);
System.out.println("\nNext command?>");
} while (!done);
}
private String readCommand() {
String command = input.registerString();
return command;
}
private boolean handleCommand(String command) {
switch (command) {
case "1":
case "register new dog":
dogList.registerDog();
break;
case "2":
case "increase age":
dogList.increaseAge();
break;
case "3":
case "list dogs":
dogList.listDogs();
break;
case "4":
case "remove dog":
dogList.removeDog();
break;
case "0":
case "exit":
return true;
default:
printMenu();
}
return false;
}
private void printMenu() {
System.out.println("Error: wrong command\n" + "The available commands are:\n" + "1: register new dog\n"
+ "2: increase age\n" + "3: list dogs\n" + "4: remove dog\n" + "0: exit");
return;
}
private void closeDown() {
System.out.println("Shutting down...");
input.closeInput();
}
private void run() {
startup();
runCommandLoop();
closeDown();
}
public static void main(String[] args) {
Interface startup = new Interface();
startup.run();
}
In case you need to see what the dogList methods do (the arraylist variable name is just temporary):
public void registerDog() {
String name = input.registerStringLoop("Name");
String breed = input.registerStringLoop("Breed");
int weight = input.registerIntPrompt("Weight");
int age = input.registerIntPrompt("Age");
Dog dog = new Dog(name, breed, age, weight);
doggoList.add(dog);
System.out.println(dog.getName() + " has been added to the register.\n" + dog.toString());
}
public void listDogs() {
if (doggoList.size() == 0) {
System.out.println("Error: no dogs found");
} else {
System.out.print("Smallest tail length to display?>");
int smallestTailLength = input.registerInt();
sortDogs();
if (smallestTailLength == 0) {
System.out.println(doggoList);
} else
for (Dog s : doggoList) {
if (s.getTailLength() >= smallestTailLength)
System.out.println(s);
}
}
}
public void increaseAge() {
System.out.print("Enter the name of the dog?>");
String dogName = input.registerString();
for (Dog s : doggoList) {
if (s.getName().equalsIgnoreCase(dogName)) {
s.increaseAge();
sortDogs();
System.out.println(s.getName() + " is now " + s.getAge() + " years old.");
return;
}
}
Thanks in advance.
答案1
得分: 0
应该尽可能简单,如下所示:
if (!done){
System.out.println("\n下一个命令?>");
}
英文:
It should be as simple as
if (!done){
System.out.println("\nNext command?>");
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论