简体   繁体   English

Java局部变量不在if语句外初始化

[英]java Local variable not initializing outside if-statement

Eclipse says that the variable age, agirl and aboy may not have been initialized. Eclipse说变量年龄,agirl和aboy可能尚未初始化。 I initialized the variables before the first if statement and they got values in the if-statement. 我在第一个if语句之前初始化了变量,并且它们在if语句中获得了值。 When I want to use them in the next if-statement eclipse says the local variables may not have been initialized. 当我想在下一个if语句中使用它们时,说本地变量可能尚未初始化。 Here is my code: 这是我的代码:

import java.util.Scanner;

class Main{
public static void main(String args[]){
    Scanner input = new Scanner(System.in);
    String define;
    int aboy, agirl, age;
    System.out.println("Are you a boy or a girl?");
    define = input.next();

    if (define.equals("boy")){
        System.out.println("What is your age?");
        aboy = input.nextInt();
        age = aboy;
    }else if (define.equals ("girl")){
        System.out.println("What is your age?");
        agirl = input.nextInt();
        age = agirl;
    }else
        System.out.println("wrong answer");
    if (agirl >= 18 || aboy >= 16){
        System.out.println("You are a " + define + " and you are " + age  + " years old");
    }
}
}

This line 这条线

int aboy, agirl, age;

contains declarations, not initializations. 包含声明,而不是初始化。 Java will not initialize a local variable for you, and there is an execution path (the else ) where nothing is ever assigned to those variables, then you attempt to reference their nonexistent values. Java不会为您初始化局部变量,并且存在一条执行路径( else ),在该路径中没有任何内容分配给这些变量,然后您尝试引用它们不存在的值。

You must set values to them before you use them, in all execution paths. 在所有执行路径中使用它们之前,必须为它们设置值。 Initialize them to something when you declare them. 在声明它们时将它们初始化为某种东西。

Not only may you have an uninitialized variable, you're guaranteed to. 您不仅可以拥有未初始化的变量,而且可以保证

Look at your control flow: You first ask for a value for define , and then you execute exactly one of the blocks. 查看您的控制流:首先要求为define一个值,然后精确地执行其中一个块。 If define is "boy" , you don't initialize agirl ; 如果define"boy" ,则不初始化agirl if define is "girl" , you don't initialize aboy , and if define doesn't match either, you don't initialize any of your variables at all. 如果define"girl" ,则不初始化aboy ,如果define也不匹配,则根本不初始化任何变量。

It looks like you are trying to cleverly combine the functions of a boolean and an int by having "magic" values in your int s. 看起来您正在尝试通过在int具有“魔术”值来巧妙地组合booleanint的功能。 This is poor design because it's not clear how the magic works, but you can make your example run by initializing all of your int values to 0 : 这是一个糟糕的设计,因为尚不清楚魔术的工作原理,但是您可以通过将所有int值初始化为0来运行示例:

int aboy = 0, agirl = 0, age = 0;

Initializing is assigning the variable a value. 初始化是为变量分配一个值。 Declaring is creating the variable. 声明是创建变量。 They are not the same. 她们不一样。

The reason you need to initialize the variables is because it is possible they will not be initialized. 您需要初始化变量的原因是因为有可能它们不会被初始化。 All the if statements could be false, thus you need to give them a default value. 所有的if语句都可能为false,因此您需要给它们提供默认值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM