Java “变量可能尚未初始化”(在if语句中)。我使用了switch语句后跟if。(爪哇)
我正在做一个家庭作业来计算工人的净工资。我使用了一个针对不同技能水平的工人的转换声明来定义他们的工资率+加号,询问他们想要什么样的扣减计划,他们可以有多个扣减计划。然后我结束了switch语句,并使用if语句来计算总扣减额。但是,我不断收到每个扣减计划的错误消息,例如:变量DenIns可能尚未初始化。我不确定是什么原因造成的。非常感谢您的帮助Java “变量可能尚未初始化”(在if语句中)。我使用了switch语句后跟if。(爪哇),java,if-statement,switch-statement,Java,If Statement,Switch Statement,我正在做一个家庭作业来计算工人的净工资。我使用了一个针对不同技能水平的工人的转换声明来定义他们的工资率+加号,询问他们想要什么样的扣减计划,他们可以有多个扣减计划。然后我结束了switch语句,并使用if语句来计算总扣减额。但是,我不断收到每个扣减计划的错误消息,例如:变量DenIns可能尚未初始化。我不确定是什么原因造成的。非常感谢您的帮助 Scanner read= new Scanner(System.in); 将变量初始化为起始值。定义一行整数: int MedIns、DISIS、De
Scanner read= new Scanner(System.in);
将变量初始化为起始值。定义一行整数: int MedIns、DISIS、DenIns 这些整数还没有值,在使用它们之前,原语值需要该值 接下来,执行一个开关,在两种情况下设置值: 可解释性的简化代码 现在有两种情况下,这些值从未分配。即:案例“1”,并且当所有案例均未满足默认条件时 您可以通过两种方式解决此问题: 一,。在开始时初始化: int MedIns=0,DISIS=0,DenIns=0 这样就永远不会不赋值,因为您从一开始就赋值 二,。在开关的每种情况下进行初始化:
这是因为java有在访问或使用局部变量之前初始化它们的规则。这是在编译时检查的。如果我理解正确,在switch语句中声明变量的值不会初始化变量?switch语句中有三种情况。如果输入1、2或3,则没有默认情况。因此,可能会发生这样的情况:MedIns的值永远不会在代码中设置。因此,在创建变量时,应该将所有变量设置为默认值。否则,您必须在switch语句的每种情况下设置所有值,并且您的switch还需要一个默认情况。此外,情况“3”中缺少中断。这不是问题,但形式不好。正如另外两位所指出的,不能保证在switch语句中设置任何值,因此如果您以后想访问它们,必须先初始化它们。您好,我非常感谢您的帮助。我删除了netpay,但错误依然存在。我确实在switch语句中分配了它们的值,为什么它不起作用?只是想澄清一下,错误在梅迪恩斯,丹尼斯,迪尼斯。非常感谢。@maryamp。编辑了答案,我认为这回答了您的问题,如果您遇到问题,请告诉我:
int skill, rate,MedIns, DisIns, DenIns;
double hours,regpay,overtime,totpay,totdeduct,netpay, ded1, ded2, ded3 ;
System.out.println("Enter your skill level.");
skill= read.nextInt();
System.out.println("Enter hours worked.");
hours= read.nextInt();
switch (skill)
{
case '1':
rate = 30;
regpay = 40 * rate;
overtime = (hours - 40) * 1.5 * rate;
totpay=regpay+ overtime;
break;
case '2':
rate=40;
regpay= 40*rate;
overtime=(hours-40)*1.5*rate;
totpay=regpay+overtime;
System.out.println("Enter 1 if you have medical insurance");
MedIns=read.nextInt();
System.out.println("Enter 1 if you have dental insurance");
DenIns=read.nextInt();
System.out.println("Enter 1 if you have disability insurance");
DisIns=read.nextInt();
break;
case '3':
rate=50;
System.out.println("Enter 1 if you have medical insurance");
MedIns=read.nextInt();
System.out.println("Enter 1 if you have dental insurance");
DenIns=read.nextInt();
System.out.println("Enter 1 if you have disability insurance");
DisIns=read.nextInt();
}
regpay= 40*rate;
overtime=(hours-40)*1.5*rate;
totpay=regpay+overtime;
if (MedIns == 1) {
ded1 = 60;
}
else {
ded1 = 0;
}
if (DenIns == 1) {
ded2 = 40;
}
else {
ded2 = 0;
}
if (DisIns == 2) {
ded3 = 25;
}
else {
ded3 = 0;
}
totdeduct = ded1 + ded2 + ded3;
}
}
int DenIns=0;
switch (skill)
{
case '1':
// VALUES ARE NOT SET
break;
case '2':
// here the values are assigned
MedIns=read.nextInt();
DenIns=read.nextInt();
DisIns=read.nextInt();
break;
case '3':
// here as well
MedIns=read.nextInt();
DenIns=read.nextInt();
DisIns=read.nextInt();
}
switch (skill)
{
case '1':
MedIns=123
DenIns=1234
DisIns=12345
break;
case '2':
// here the values are assigned
MedIns=read.nextInt();
DenIns=read.nextInt();
DisIns=read.nextInt();
break;
case '3':
// here as well
MedIns=read.nextInt();
DenIns=read.nextInt();
DisIns=read.nextInt();
break; // important, else this case will 'fall through' to the default block
default: // will perform when skill matches none of the top cases
MedIns=0;
DenIns=0;
DisIns=0;
}