为什么这段代码在 if 语句中给我错误

Why This Code Give me Error in if Statment

为什么只在 if 语句中的 Extra word 中给我错误

switch (Class) {
  case 'F':
       value = 30;
       Extra = Weight-value ;
       break ;
  case 'B':
      value = 25 ;
      Extra = Weight - value ;

      break ;

   case 'E':
       value = 20 ;
       Extra = Weight - value ;

       break;
    default  :
        System.out.println("You  Not  Enter The  Right Class ");
}
System.out.println("Extra is " );

if ( Extra >=0 )
{
    Total = Extra * 10 ;
    System.out.println("You Have Extra  weight "+Extra +"And You Should Pay "+Total );

}

else
{
    System.out.println("No  Extra Weight to Pay Your  Weight is   "+Weight);
}

因为您正在尝试使用 Extra 的值,但有可能 Extra 从未被分配过值(如果 Class 不是 'F''B''C',因此遵循通过 switchdefault 路径)。局部变量在使用前一定要赋值


我强烈建议遵循标准 Java naming conventions。局部变量不应以大写字符开头。