变量未初始化 - 无法返回值

时间:2013-04-23 21:10:28

标签: java class

在下面;我试图打印用户输入的一些信息。我相信我有一个范围问题。当用户选择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;
    }
}

3 个答案:

答案 0 :(得分:2)

确实,变量newChar在这里初始化:

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

但是当你选择“a”时,这段代码才会被执行。选择“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;
            }
    }
}

答案 1 :(得分:1)

您需要在newChar语句之前为switch变量指定内容。这里最合乎逻辑的解决方案是添加

Character newChar = null;

switch语句之外的行,如果qnewChar,请检查null案例,并在其中打印某种No Character has been created yet消息情况下。

答案 2 :(得分:0)

它的范围问题 你已经在选择A中声明了newChar但是没有选择v

案例“a”:                         字符newChar = new Character();

所以声明newChar in while或choice v