gpt4 book ai didi

java - int 变量未初始化

转载 作者:行者123 更新时间:2023-12-01 18:40:03 24 4
gpt4 key购买 nike

private int getBirthdayYear(){
Random r1 = new Random();
int x = r1.nextInt(1000);
int year = 1000+ x;
return birthdayYear;
}
private String getBirthday(){
int year = getBirthdayYear();
Random r2 = new Random();
int y = r2.nextInt(11);
String monthStr;
int month = 1 + y;
switch (month) {
case 1: monthStr = "January";
break;
case 2: monthStr = "February";
break;
case 3: monthStr = "March";
break;
case 4: monthStr = "April";
break;
case 5: monthStr = "May";
break;
case 6: monthStr = "June";
break;
case 7: monthStr = "July";
break;
case 8: monthStr = "August";
break;
case 9: monthStr = "September";
break;
case 10: monthStr = "October";
break;
case 11: monthStr = "November";
break;
case 12: monthStr = "December";
break;
default: monthStr = "Invalid";
break;
}


Random r3 = new Random();
**int day;// day variable declared as int**
if(month == 1||month == 3||month == 5||month == 7||month == 8||month == 10||month == 12){
int z1 = r3.nextInt(30);
day = 1 + z1;
}
else if(month == 2||month == 4||month == 6||month == 9||month == 11){
int z2 = r3.nextInt(29);
day = 1 + z2;
}
else if(month == 2 & year%4 ==0){
int z3 = r3.nextInt(28);
day = 1 + z3;
}
else if(month == 2 & year%4 != 0){
int z4 = r3.nextInt(27);
day = 1 + z4;
}

**birthday = monthStr + " " + day + "th " + year;// error here. day variable is not initialized
return birthday;**
}

这是一个生成随机生日的代码,我还不确定它是否有效。我还在努力。现在,当我尝试编译这段代码时,它说 day 变量尚未初始化。不过,我已经说过了 int day; 。我应该怎么做才能解决这个问题?

最佳答案

在 Java 中,您不能使用未初始化的变量。 “未初始化”意味着变量可能尚未设置为任何内容。如果编译器无法证明该变量始终已初始化,则会产生错误。

int a;

// a is uninitialized

if(r.nextInt(10) < 2)
a = 5;

// a has an 80% chance of still being uninitialized

System.out.println(a); // error: a might not be initialized.

就您而言:

int day;
// day is uninitialized
if(month == 1||month == 3||...){
int z1 = r3.nextInt(30);
day = 1 + z1;
}
else if(month == 2||month == 4||month == 6||month == 9||month == 11){
int z2 = r3.nextInt(29);
day = 1 + z2;
}
else if(month == 2 & year%4 ==0){
int z3 = r3.nextInt(28);
day = 1 + z3;
}
else if(month == 2 & year%4 != 0){
int z4 = r3.nextInt(27);
day = 1 + z4;
}
// if the month wasn't between 1 and 12, day is still uninitialized

编译器不够智能,无法检测到 month 始终在 1 到 12 之间。修复方法是确保 day 始终设置为某个值,即使 month 不在该范围内。

一种方法:

int day;
// day is uninitialized
if(month == 1||month == 3||...){
int z1 = r3.nextInt(30);
day = 1 + z1;
}
else if(month == 2||month == 4||month == 6||month == 9||month == 11){
int z2 = r3.nextInt(29);
day = 1 + z2;
}
else if(month == 2 & year%4 ==0){
int z3 = r3.nextInt(28);
day = 1 + z3;
}
else if(month == 2 & year%4 != 0){
int z4 = r3.nextInt(27);
day = 1 + z4;
}
// THIS PART IS NEW
else
day = -1; // this line will never actually run

现在编译器可以知道,无论发生什么,day 始终会在 if 语句完成后设置为某个值。

请注意,day = -1; 从未实际运行。它只是让编译器可以证明 day 始终具有值。

修复该错误的另一种方法是将 int day; 更改为 int day = -1; - 出于同样的原因。

关于java - int 变量未初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20307957/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com