"变量可能没有被初始化"(在if语句中).我使用了switch语句和if。(JAVA)

问题描述 投票:0回答:1

我在做一个作业,要找工人的净工资。我对不同技术水平的工人使用了一个开关语句来定义他们的工资率+加问他们想要什么扣除计划(他们可以有多个)。然后,我结束了switch语句,并使用if语句来计算总的扣除额。然而,我一直得到一个错误信息,例如,每个扣除计划。(变量DenIns可能没有被初始化) 。我不知道是什么原因造成的。将感谢任何帮助

Scanner read= new Scanner(System.in);
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;




}
    }
java if-statement switch-statement
1个回答
1
投票

你定义了一行ints。

int MedIns, DisIns, DenIns;

这些ints还没有值,在你使用它们之前,这是对基元值的要求。

接下来,你执行一个开关,在2种情况下设置值。

为了便于解释,简化了代码

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();

}

现在,在两种情况下,数值从未被分配。即 case '1' 还有一种情况是,当这两种情况都不符合时(即: default)

你可以通过两种方式来解决这个问题。

1. 在一开始就初始化。

int MedIns=0, DisIns=0, DenIns=0;

这样他们就不会不被分配,因为你在一开始就分配了值。

2. 在每一个开关的情况下初始化。

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;
}

0
投票
int DenIns=0;

将变量初始化为一个起始值。

© www.soinside.com 2019 - 2024. All rights reserved.