局部变量n可能尚未初始化&局部变量s可能尚未初始化?我接受他们作为用户输入

时间:2014-08-11 12:13:05

标签: java.util.scanner

首先我宣布了n。我从用户那里接受了n。然后打印它的价值。错误来了。

public class Class {

    public static void main(String[] args) {
        String n;
        double s;
        Scanner scan=new Scanner(System.in);
        System.out.println("Enter the Name:");
        if (scan.hasNextLine()) {
            n=scan.nextLine();
        }
        System.out.println("Enter the salary:");
        if (scan.hasNextDouble()) {
            s=scan.nextDouble();
        }
        System.out.println("Name is: "+n);
        //error: The local variable n may not have been initialised.
        System.out.println("Salary is: "+s);
        //error: The local variable s may not have been initialised.
        scan.close();
    }

}

1 个答案:

答案 0 :(得分:0)

初始化变量n,s:  字符串n =“”;     double s = 0;