为什么java不识别变量?

XxS*_*678 -4 java

有谁知道为什么java不理解变量"增量"?我的任务是找出用户的服务年限和工资.之后,我将为每个人分配一个增量.但是当我想打印变量"increment"时,java无法识别它.我可以知道为什么吗?

import javax.swing.JOptionPane;

public class Q4 {

    public static void main(String[] args) {

         int increment;
        String yearsString = JOptionPane.showInputDialog(null,
                "Please enter your years of service");

        String salaryString = JOptionPane.showInputDialog(null,
                "Please enter your salary");

        int years = Integer.parseInt(yearsString);
        double salary = Double.parseDouble(salaryString);


        if(years < 10) {
            if(salary < 1000.0) {
                increment = 100;
            }

            else if (salary< 2000.0) {
                increment = 200;
            }

            else {
                increment = 300;
            }
        }

        else if (years > 10) {
            if(salary<1000) {
                increment = 200;
            }

            else if (salary < 2000) {
                increment = 300;
            }

            else {
                increment = 400;
            }
        }
         JOptionPane.showMessageDialog(null,
                 "Your increment is "+increment);

    }

}
Run Code Online (Sandbox Code Playgroud)

dav*_*xxx 5

问题不在于变量的"识别",而在于它的声明.
如果编译器认为它可能没有被赋值,则不能使用局部变量.

在实际代码中,increment可能不会像您为某些特定条件语句中的值一样赋值.

因此increment声明中的值具有默认值:

int increment = 0;
Run Code Online (Sandbox Code Playgroud)