【发布时间】:2019-05-21 19:55:41
【问题描述】:
在编写了一段时间后,我突然想到 switch 语句可能不是运行我的代码的最佳函数,例如,我不能将 case 语句简化为它们自己的方法,因为它们将是孤立的 case。我想知道是否有办法简化这段代码以及我可以用什么方法来做到这一点。
我尝试过写几个 if 语句,但是这会导致更多的混乱和无序,即使我可以将所有内容都放入他们自己的方法中。 这导致我使用 switch 语句,但是我无法简化它。
这里是一段代码。这包括总体 switch 语句和第一种情况。
while (true) //游戏循环 {
if (user.equalsIgnoreCase("exit")) { //if player types exit at any time the game quits
System.exit(0);
} else { //else the game runs
map();
Commands(); //runs the command method
playerDam(); //runs player damage method
//Handles the story
switch (question) //if question is equal to...
{
case "1": //introduction
System.out.println("Welcome to Archewind" +
"\nIf in need of help, press help at any time for list of commands. \nIf you would like to exit type exit at any time." +
"\nAre you ready to begin? \n[1]Yes\n[2]No");
switch (user = in .next()) //if user types one... if two...
{
case "1": //this happens
System.out.println("\n");
System.out.println("Great. Good luck");
question = "1.5";
gametic = 1;
break;
case "2": //this happens
System.out.println("\n");
System.out.println("Oh... well um... I don't really know what to do here. People usually say yes.");
System.out.println("\nDo you think you're ready now? \n[1]Yes \n[2]No");
switch (user = in .next()) {
case "1":
System.out.println("\n");
System.out.println("Ok goood, I was getting worried there for a second. Good luck.");
question = "1.5";
gametic = 1;
break;
case "2":
System.out.println("\n");
System.out.println("Really? [1]Yes [2]No");
switch (user = in .next()) {
case "1":
System.out.println("\n");
System.out.println("This is just getting long. Do you know what you're doing to the programmer? He has to code all of this you know" +
"\nIm just going to give you a choice. Either say yes next time or I am going to shut down. I mean it." +
"\nStart game? \n[1]Yes \n[2]No");
switch (user = in .next()) {
case "1":
System.out.println("\n");
System.out.println("Alllrriiight.. Lets get this party started.");
question = "1.5";
gametic = 1;
break;
case "2":
System.out.println("\n");
System.out.println("Nope. I'm done.");
user = "exit";
break;
}
case "2":
System.out.println("Ok good. We can finally get this show on the road.");
}
}
question = "1.5";
gametic = 1;
break;
}
break;
这是另一个片段。
案例“1.5”:
System.out.println("\nNow, before we start you need to choose a class. Each one will offer diffrent benefits.");
int rank = getRank();
userClass = RANKS[rank];
System.out.println("Your chosen class is: " + userClass);
if (userClass.equals("Farmer")) {
playerhealth = playerhealth + 1;
inv.remove("Health Potion");
inv.remove("Water Bottle");
}
if (userClass.equals("Trader")); {
inv.add("Health Potion");
inv.remove("Water Bottle");
if (playerhealth == 6) {
playerhealth = playerhealth - 1;
}
}
if (userClass.equals("Wanderer")); {
inv.add("Water Bottle");
inv.remove("Health Potion");
if (playerhealth == 6) {
playerhealth = playerhealth - 1;
}
}
gametic = 2;
question = "2";
break;
我预计这会比实际情况要简单得多,但我似乎无法将案例陈述分解成它们自己的方法,所以一切都更有条理。
【问题讨论】:
标签: switch-statement code-organization case-statement