所以我正在创建这个文本基础游戏,其中用户在我用不同地方创建的地图上用计算机隐藏和搜索以进行搜索。我目前面临的困境是当计算机不在搜索区域时如何返回上一级菜单。这是我的代码的一部分,我想返回上一个菜单。
if(userinput1.equalsIgnoreCase(forrest)){
System.out.println("You have come to the forrest. There are three paths. Left, Right, or Straight. Where would you like to go?");
String userinput2 = keyboard.nextLine(); // 3 paths in a forrest
while(!((userinput2.equalsIgnoreCase("left") || userinput2.equalsIgnoreCase("right") ||
userinput2.equalsIgnoreCase("straight")) )){ // looping for a valid input
System.out.println("Invalid input. Try again");
userinput2 = keyboard.nextLine();
}
if(userinput2.equalsIgnoreCase("left")){ // the left path in the forrest
System.out.println("You gone left");
System.out.println("You come across a tree house. What would you like to do?");
System.out.println("1:Search the tree house.");
System.out.println("2:Search the bushes around the tree house");
System.out.println("3:Return");
int userInputForrest1 = keyboard.nextInt();
while(!(userInputForrest1 == 1 || userInputForrest1 == 2 || userInputForrest1 == 3)){
System.out.println("Invalid input. Try again");
System.out.println("1:Search the tree house.");
System.out.println("2:Search the bushes around the tree house");
System.out.println("3:Return");
userInputForrest1 = keyboard.nextInt();
}
答案 0 :(得分:0)
创建显示部件的功能
e.g。
displayLeft()// will display options for left
displayRight()//will do the same for right etc. and navigate based on user input
我不知道这是否是最好的方式,但显然它比顺序编码更好!