import java.util.Scanner; // needed for Scanner Class
public class MyClass
{
public static void main(String[] args)
{
boolean running = true;
GAME:
{
// Create a Scanner object for choice input.
Scanner console = new Scanner(System.in);
String gameName = "The Path";
PATHCHOICES:
while (running)
{
System.out.println("Enter your choice: Left, Right, or Run Away");
String choice = console.nextLine();
//make the do-while statement
do
{
if (choice.equals("Left"))
{
System.out.println("You choose to take the left fork in the road.");
break PATHCHOICES;
}
else if (choice.equals("Right"))
{
System.out.println("You choose to take the right fork in the road.");
break PATHCHOICES;
}
else if (choice.equals("Run Away"))
{
System.out.println("You choose to turn back and return the way you came.");
break PATHCHOICES;
}
else
{
System.out.println("please choose Left, Right, or Run Away");
choice = console.nextLine();
}
} while (choice != "Left" && choice != "Right" && choice != "Run Away");
}
DOORCHOICES:
while (running)
{
System.out.println("Enter your choice: Open The Door, Walk Away From The Door");
String choice = console.nextLine();
do
{
if (choice.equals("Open The Door"))
{
System.out.println("You open the door and walk into the next room.");
break DOORCHOICES;
}
else if (choice.equals("Walk Away From The Door"))
{
System.out.println("You walk away from the door and head back the way you came.");
break DOORCHOICES;
}
else
{
System.out.println("Open the door or Walk Away From The Door");
choice = console.nextLine();
}
} while (choice != "Open The Door" && choice != "Walk Away From The Door");
}
}
System.out.println("tacos");
}
}
我是否可以调用这些循环方法,而不必重复键入它们(复制粘贴(?如果可能的话,我希望能够从主GAME函数之外调用循环。这样,我就不必每次使用相同的循环时都复制粘贴、复制粘贴、拷贝粘贴。我有两个名为PATHCHOICES AND DOORCHOICES的循环,所以如果可能的话,我会使用什么语法?
是的,除了可以进行许多其他改进外,还可以创建方法。以下是一个包含方法和一些改进的解决方案。
首先,这里有一个名为";promptForDoor";其中:
- 第2-3行:定义要重用的几个字符串
- 第5行:在这里开始一个循环,简单的"而";,永远重复
- 第6行:打印输入选项(使用2-3中定义的字符串(;此外,使用";打印((";而不是";println((">
- 第9行:检查它们的输入是否与第2行的字符串匹配;equalsIgnoreCase((";因为用户输入什么并不重要("开门"或"开门"应该都有效(
- 第10-11行:打印一些信息,然后通过返回";选择";;这也结束了循环
- 第13-15行:逻辑与9-11相同
- 第17行:如果你在这里成功了,那是因为你在第9行或第13行没有匹配,所以只需再次重复整个循环(包括对用户的提示和读取输入(
1 private static String promptForDoor(Scanner scanner) {
2 String openTheDoor = "Open the door";
3 String walkAwayFromTheDoor = "walk away from the door";
4
5 while (true) {
6 System.out.print(openTheDoor + ", or " + walkAwayFromTheDoor + "? ");
7 String choice = scanner.nextLine();
8
9 if (choice.equalsIgnoreCase(openTheDoor)) {
10 System.out.println("You open the door and walk into the next room.");
11 return choice;
12
13 } else if (choice.equalsIgnoreCase(walkAwayFromTheDoor)) {
14 System.out.println("You walk away from the door and head back the way you came.");
15 return choice;
16 }
17 }
18 }
这里有另一个提示路径的方法,它的结构与其他方法相同。
1 private static String promptForPath(Scanner scanner) {
2 String left = "left";
3 String right = "right";
4 String runAway = "run away";
5
6 while (true) {
7 System.out.print(left + ", " + right + ", or " + runAway + "? ");
8 String choice = scanner.nextLine();
9
10 if (choice.equalsIgnoreCase(left)) {
11 System.out.println("You choose to take the left fork in the road.");
12 return choice;
13
14 } else if (choice.equalsIgnoreCase(right)) {
15 System.out.println("You choose to take the right fork in the road.");
16 return choice;
17
18 } else if (choice.equalsIgnoreCase(runAway)) {
19 System.out.println("You choose to turn back and return the way you came.");
20 return choice;
21 }
22 }
23 }
下面是一个运行它的例子:
Scanner scanner = new Scanner(System.in);
String path = promptForPath(scanner);
String door = promptForDoor(scanner);
System.out.println("path: " + path);
System.out.println("door: " + door);
除了示例输入+输出之外,还包括一个工作正常的大写输入("LEFT"(和一个意外输入("jjjj"(,该输入导致重新提示用户输入有效内容。最后,它打印出从用户获取的每个输入("左"one_answers"离开门"(。
left, right, or run away? LEFT
You choose to take the left fork in the road.
Open the door, or walk away from the door? jjjjj
Open the door, or walk away from the door? walk away from the door
You walk away from the door and head back the way you came.
path: LEFT
door: walk away from the door