我試圖做一個簡單的故事冒險文本遊戲。我創建了一個名爲'key'的布爾值並將其設置爲false。在一個單獨的方法中,如果用戶鍵入1,則應該將鍵布爾值設置爲true,並將true值返回給start方法。但是,我不確定我會如何做到這一點。下面是這兩種方法的代碼:如何將布爾值返回給另一個方法?
啓動方式:
static void start() throws IOException {
boolean key = false;
Scanner in = new Scanner(System.in);
System.out.println("You are in a dark room with 3 doors.");
System.out.println("Pick 1. 2. or 3.");
int number;
number = in.nextInt();
if(number == 1) {
room1();
}
}
}
方法鍵的值更改爲true:
static void room1() throws IOException {
Scanner in = new Scanner(System.in);
System.out.println("You have picked room 1.");
System.out.println("You find a dead man's corpse.");
System.out.println("Do you: 1. Loot the corpse or 2. Go back to the starting area");
int number;
boolean key;
number=in.nextInt();
if(number == 1) {
System.out.println("You find a key to another door, perhaps back at the starting area?");
key = true;
start();
}
}
任何幫助,不勝感激!
哪裏是你單獨的方法? –
我道歉,不知道你指的是什麼。 – goldenness