转到 Java 中的备选方案?
Goto Alternatives in Java?
我以前用 TI-BASIC 编程,经常使用 goto 语句。我已经开始学习 java,并且无法弄清楚如何将程序的执行发送到另一条线。
这是我的完整代码:
package inputs;
import java.util.Scanner;
public class Inputs {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
String greeting = "Welcome to a new choose your own adventure game!";
String help = "Use commands such as up, down, left, and right to move around the world";
String error = "You can't do that";
room1();
}
public static String getNextLine() {
Scanner scan = new Scanner(System.in);
return scan.nextLine();
}
public static String getNextWord() {
Scanner scan = new Scanner(System.in);
String base = scan.nextLine();
String command = base;
//differentiate capital and lowcase
if ("north".equals(base)){
command = "North";
}
if ("south".equals(base)){
command = "South";
}
if ("east".equals(base)){
command = "East";
}
if ("west".equals(base)){
command = "West";
}
return command;
}
public static void room1(){
System.out.println("There is a faint smell of rotting flesh around you. you look hazily around the room. There is a door to the north. It may be your way out...");
String command = getNextWord();
while(1==1){
if ("North".equals(command)){
hall();
}
else{
System.out.println("You can't do that.");
}
}
}
public static void hall(){
System.out.println("You are in a hallway. The hallway continues in a northerly direction to another room. There is a room to your left");
while(1==1){
String command = getNextWord();
if ("North".equals(command)){
room3();
}
if ("East".equals(command)){
room2();
}
if ("South".equals(command)) {
room1();
}
else {
System.out.println("You can't do that.");
}
}
}
public static void room2(){
while(1==1){
String command = getNextWord();
if("get".equals(command)){
System.out.println("You picked up the key. Maby there is a door somewhere");
}
}
}
public static void room3(){}
}
如您所见,我正在尝试创建一个有 4 个房间的文字冒险游戏。如果可能的话,我还想在 room2 中找到一把在 room3 中使用的钥匙。老实说,我不知道如何解决这个问题...
*编辑
为了回应被标记为可能的重复,我的问题要求针对我的特定程序的特定操作方法,而不是总体操作方法
Java 没有 goto
声明,您也不需要。
目前,您将房间表示为方法。当前房间作为调用堆栈的最顶层。事先访问过的每个房间都低于该房间。这种方法对于简短的冒险风格游戏来说已经足够了。然而:
- 您正在学习 Java,并且 Java 支持面向对象编程。
- 如果有人玩你的游戏很长时间,最终调用堆栈会变得很大,导致堆栈溢出。
另一种方法是考虑游戏中的对象是什么。你有:
- 房间
- 库存
- 库存物品(如钥匙)
这些对象可以定义为 Java 类 或枚举。房间和房间之间的连接构成了graph中的节点和边。
您的方法可以是操作,例如将用户从一个房间转移到另一个房间,或者拿起或使用库存物品。
我以前用 TI-BASIC 编程,经常使用 goto 语句。我已经开始学习 java,并且无法弄清楚如何将程序的执行发送到另一条线。
这是我的完整代码:
package inputs;
import java.util.Scanner;
public class Inputs {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
String greeting = "Welcome to a new choose your own adventure game!";
String help = "Use commands such as up, down, left, and right to move around the world";
String error = "You can't do that";
room1();
}
public static String getNextLine() {
Scanner scan = new Scanner(System.in);
return scan.nextLine();
}
public static String getNextWord() {
Scanner scan = new Scanner(System.in);
String base = scan.nextLine();
String command = base;
//differentiate capital and lowcase
if ("north".equals(base)){
command = "North";
}
if ("south".equals(base)){
command = "South";
}
if ("east".equals(base)){
command = "East";
}
if ("west".equals(base)){
command = "West";
}
return command;
}
public static void room1(){
System.out.println("There is a faint smell of rotting flesh around you. you look hazily around the room. There is a door to the north. It may be your way out...");
String command = getNextWord();
while(1==1){
if ("North".equals(command)){
hall();
}
else{
System.out.println("You can't do that.");
}
}
}
public static void hall(){
System.out.println("You are in a hallway. The hallway continues in a northerly direction to another room. There is a room to your left");
while(1==1){
String command = getNextWord();
if ("North".equals(command)){
room3();
}
if ("East".equals(command)){
room2();
}
if ("South".equals(command)) {
room1();
}
else {
System.out.println("You can't do that.");
}
}
}
public static void room2(){
while(1==1){
String command = getNextWord();
if("get".equals(command)){
System.out.println("You picked up the key. Maby there is a door somewhere");
}
}
}
public static void room3(){}
}
如您所见,我正在尝试创建一个有 4 个房间的文字冒险游戏。如果可能的话,我还想在 room2 中找到一把在 room3 中使用的钥匙。老实说,我不知道如何解决这个问题...
*编辑 为了回应被标记为可能的重复,我的问题要求针对我的特定程序的特定操作方法,而不是总体操作方法
Java 没有 goto
声明,您也不需要。
目前,您将房间表示为方法。当前房间作为调用堆栈的最顶层。事先访问过的每个房间都低于该房间。这种方法对于简短的冒险风格游戏来说已经足够了。然而:
- 您正在学习 Java,并且 Java 支持面向对象编程。
- 如果有人玩你的游戏很长时间,最终调用堆栈会变得很大,导致堆栈溢出。
另一种方法是考虑游戏中的对象是什么。你有:
- 房间
- 库存
- 库存物品(如钥匙)
这些对象可以定义为 Java 类 或枚举。房间和房间之间的连接构成了graph中的节点和边。
您的方法可以是操作,例如将用户从一个房间转移到另一个房间,或者拿起或使用库存物品。