爲什麼變量q2,q3未被初始化,但q1是?本地字符串變量未初始化錯誤
這是結果如何類似於:
WELCOME TO米切爾的TINY冒險!
你是在一個令人毛骨悚然的房子!你想去「樓上」還是去「廚房」 ?
廚房
還有很長的檯面與髒盤子無處不在。如你所料,有一臺 的冰箱。您可以打開 「冰箱」或查看「櫥櫃」。
冰箱
冰箱裏面你看到的食物和東西。它看起來很漂亮 討厭。你想吃點食物嗎? ( 「是」 或 「否」)
沒有
import java.util.Scanner;
public class App1 {
public static void main(String[] Args) {
Scanner keyboard = new Scanner(System.in);
String q1;
String q2;
String q3;
String a = "upstairs";
String b = "kitchen";
String c = "refrigerator";
String d = "cabinet";
String e = "yes";
String f = "no";
String g = "hallway";
String h = "bedroom";
String i = "bathroom";
System.out.println("Welcome To Mitchell's Tiny Adventure!");
System.out.println();
System.out.println("You are in a creepy house, would you like to go upstairs or into the kitchen?");
System.out.print(">");
q1 = keyboard.nextLine();
System.out.println();
if (q1.equals(a)) {
System.out.println("Upstairs you see a hallway. At the end of the hallway is the master");
System.out.println("\"bedroom\". There is also a \"bathroom\" off the hallway. Where would you like");
System.out.println("to go?");
System.out.print(">");
q2 = keyboard.nextLine();
System.out.println();
} else if (q1.equals(b)) {
System.out.println("There is a long countertop with dirty dishes everywhere. Off to one");
System.out.println("side there is, as you'd expect, a refrigerator. You may open the \"refrigerator\"");
System.out.println("or look in a \"cabinet\".");
System.out.print(">");
q2 = keyboard.nextLine();
System.out.println();
}
if (q2.equals(c)) {
System.out.println("Inside the refrigerator you see food and stuff. It looks pretty nasty.");
System.out.println("Would you like to eat some of the food? (\"yes\" or \"no\")");
System.out.print(">");
q3 = keyboard.nextLine();
System.out.println();
} else if (q2.equals(d)) {
System.out.println("Inside the cabinet is the boogie man, Are you scare? \"yes or no\"");
q3 = keyboard.nextLine();
System.out.println();
}if (q2.equals(g)){
System.out.println("You fall in the hallway can you make it? \"yes or no\"");
q3 = keyboard.nextLine();
System.out.println();
}else if (q2.equals(h)){
System.out.println("You run into the bedroom were there are monsters live are you afraid \"yes or no\"");
q3 = keyboard.nextLine();
System.out.println();
}else if(q2.equals(i)){
System.out.println("You run into the bathroom do you have to use it \"yes or no\"");
q3 = keyboard.nextLine();
System.out.println();
}
}
}
提示:不要讓像bc這樣的變量無人能夠理解,只要將它們命名爲廚房,樓上等 –