在每个Switch-case结束后,除了case: “0 exit”之外,提示用户打印。

huangapple go评论73阅读模式
英文:

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

huangapple
  • 本文由 发表于 2020年8月8日 18:04:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/63314134.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定