2015-08-23 77 views
0

爲什麼變量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(); 
     } 

    }  
} 
+0

提示:不要讓像bc這樣的變量無人能夠理解,只要將它們命名爲廚房,樓上等 –

回答

1

每個變量在第一次被訪問之前都需要初始化。這可能會自動發生在常量和字段上,但需要手動完成局部變量。所以開發人員必須決定哪個值應該是變量的第一個值。大部分是null(用於對象類型)或0/false(用於基元類型)。

所以,現在你的問題:

爲什麼變量Q2,Q3未初始化,但Q1是什麼?

關於q1:你第一次嘗試閱讀 「中的」 變量的引用是在這裏:

if (q1.equals(a)) { 

但在這之前,你有這樣分配:

q1 = keyboard.nextLine(); 

在此處使用返回的keyboard.nextLine()方法調用引用來初始化變量q1

現在關於q2
(與q2q3的問題都是一樣的,所以我只是專注於q2)。

你有這種分配在兩個ifelse if塊:

q2 = keyboard.nextLine(); 

所以你嘗試把它分配給其他用戶輸入,如果q1等於「上樓」「廚房」。但是q2是什麼,如果q1不是他們?也許這是「花園」「樓下」,那麼q2將保持未初始化,編譯器可以檢測到,因此錯誤。

如果q1既不是他們,也必須考慮該怎麼辦。你可以再次詢問用戶,直到你得到一個有效的答案。或者如果不允許這種情況,您可以退出程序。它是由你決定。

您也可以初始化q2q3,並設置一定的默認值以避免錯誤並避免意外的異常,如NullPointerException。這是可以做到這樣的:

String q2 = ""; 
String q3 = ""; 
0

在使用之前必須初始化局部變量。嘗試

String q1 = ""; 
String q2 = ""; 
String q3 = ""; 
0

以及在第42行: if (q2.equals(c)) {
和線53: } else if (q2.equals(d)) { 嘗試調用未初始化變量(String q2)的equals()方法,儘量先初始化它們或者檢查它們的空值取決於你想如何處理每種情況

1

在第一部分中,如果q1所有的測試都是假的,q2將保持未初始化的,但你在第二部分中引用q2;同樣爲q3

如果存在允許在未初始化的情況下引用局部變量的代碼路徑,則會發生編譯錯誤。

注意,編譯器不檢查邏輯的代碼方面,因此,例如,這將無法編譯:

String a = "foo"; 
String s; 
if (a.equals("foo")) { 
    s = "bar"; 
} else if (!a.equals("foo")) { 
    s = "baz"; 
} 
System.out.println(s); // error 

即使我們可以看到,邏輯s必須初始化,編譯器僅看到布爾方法,這可能導致s未被分配。