Eclipse 说变量age、agirl 和aboy 可能没有被初始化。我在第一个 if 语句之前初始化了变量,它们在 if 语句中得到了值。当我想在下一个 if 语句中使用它们时,eclipse 说局部变量可能尚未初始化。这是我的代码:
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");
}
}
}