2013-04-23 69 views
1

在下面;我正在嘗試打印用戶輸入的一些信息。我相信我有一個範圍問題。當用戶選擇A時;他們能夠創造一個新角色,然後打印他們的名字。如果他們然後按V;它會失敗,例外。變量未初始化 - 無法返回值

了java.lang.RuntimeException:不可編譯的源代碼 - 變量newChar也許如果他們增加了一個新的字符尚未初始化

;爲什麼程序不允許我在選擇V中打印getFirstName,但是它在選擇A中是這樣做的?

/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package rpgmanager; 

import java.util.Scanner; 

/** 
* 
* @author Aaron 
*/ 
public class RpgManager { 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 
     // Set Run Boolean to true 
     boolean running = true; 
     // "Splash Screen here" 
     System.out.println("Welcome to character generator."); 
     // If the program is running; offer the user options 
     while(running) { 
       System.out.println("A: New Char ; V: View ; S: Save ; D: Delete ; Q: Quit"); 
       // Initialize the scanner 
       Scanner user_input = new Scanner(System.in); 
       // Prepare to accept a string 
       String decision; 
       // Get the user input 
       decision = user_input.next(); 
       // We switch the input to lowecase here and compare 
       switch (decision.toLowerCase()) { 
        case "a": 
         Character newChar = new Character(); 
         System.out.println(newChar.getFirstName()); 
          break; 
        case "s": 
          break; 
        case "d": 
          break; 
        case "v": 
         try { 
          System.out.println(newChar.getFirstName());  
         } catch(Exception e) { 
          System.out.println("You have an exception - "); 
          System.out.println(e); 
         } 
          break; 
        case "q": System.exit(0); // Kill the program 
           break; 
        default: System.out.println("You did not select a viable option."); 
          System.out.println("Try again."); 
           break; 
       } 
     } 
    } 
} 


/* 
* 
* 
*/ 
package rpgmanager; 

import java.util.Scanner; 

/** 
* 
* @author Aaron 
*/ 
public class Character { 
     private String First_Name; 
     private String Last_Name; 
     private String Height; 
     private int Weight; 
     private int Age; 

     public Character() { 
      System.out.println("Creating new character..."); 
      Scanner user_input = new Scanner(System.in); 
      System.out.println("What is your first name?"); 
      First_Name = user_input.next(); 
      System.out.println("What is your last name?"); 
      Last_Name = user_input.next(); 
      System.out.println("What is your height?"); 
      Height = user_input.next(); 
      System.out.println("What is your weight?"); 
      Weight = user_input.nextInt(); 
      System.out.println("What is your age?"); 
      Age = user_input.nextInt(); 
     } 

    /** 
    * 
    */ 
    public String getFirstName() { 
     return First_Name; 
    } 

    public String getLastName() { 
     return Last_Name; 
    } 
} 
+0

在'案「V」:',你甚至不會有'newChar'因爲'newChar'在'case「中定義爲」:「,並且你不能讓它們都與你當前的代碼一起運行。 – Justin 2013-04-23 21:12:21

回答

2

如此宣佈newChar事實上,可變newChar這裏初始化:

case "a": 
    Character newChar = new Character(); //Here 
    System.out.println(newChar.getFirstName()); 
    break; //"break" makes 

但是代碼塊只會當你選擇「是」執行。當您選擇「V」,然後執行以下代碼:

System.out.println(newChar.getFirstName()); //The variable newChar is not initialized 

我相信你正在尋找如下:

/** 
* @param args the command line arguments 
*/ 
public static void main(String[] args) { 
    // Set Run Boolean to true 
    boolean running = true; 
    // "Splash Screen here" 
    System.out.println("Welcome to character generator."); 
    // If the program is running; offer the user options 
    while(running) { 
      System.out.println("A: New Char ; V: View ; S: Save ; D: Delete ; Q: Quit"); 
      // Initialize the scanner 
      Scanner user_input = new Scanner(System.in); 
      // Prepare to accept a string 
      String decision; 
      // Get the user input 
      decision = user_input.next(); 
      // We switch the input to lowecase here and compare 
      Character newChar = new Character(); //Now the variable will always be initialized 
      switch (decision.toLowerCase()) { 
       case "a":       
        System.out.println(newChar.getFirstName()); 
        break; 
       case "s": 
        break; 
       case "d": 
        break; 
       case "v": 
        try { 
         System.out.println(newChar.getFirstName());  
        } catch(Exception e) { 
         System.out.println("You have an exception - "); 
         System.out.println(e); 
        } 
         break; 
       case "q": System.exit(0); // Kill the program 
        break; 
       default: System.out.println("You did not select a viable option."); 
        System.out.println("Try again."); 
        break; 
      } 
    } 
} 
+0

這工作完美。它使我很容易看到我需要修復。 – ILikeTurtles 2013-04-23 21:37:49

1

您需要switch語句之前分配的東西給newChar變量。這裏最合理的解決辦法是添加switch聲明之外的

Character newChar = null; 

線,然後在q情況下檢查是否newCharnull,並打印某種No Character has been created yet消息在這種情況下。

+0

但是後來我無法控制他們構建角色的時間......還是會有一種方法讓他們只在選擇時初始化新角色()才能這樣做? – ILikeTurtles 2013-04-23 21:13:30

+1

@Aaron:是的,看我更新的答案。 – Keppil 2013-04-23 21:17:05

0

其範圍問題 你聲明newChar在選擇A,但不是在選擇v

的情況下 「一」: 字符newChar =新的字符();

在同時或選擇v