Java编译错误:找不到符号

Java compile error: cannot find symbol

本问题已经有最佳答案,请猛点这里访问。

嘿,我刚刚开始我的第一个Java编程书籍,所以这应该是一个简单的解决方案。我对条件句的新知识搞得一团糟,标题有误。

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import java.util.Scanner;

public class Music
{
    public static void main( String[] args )
    {

        Scanner x = new Scanner( System.in );

        int y;

        System.out.print("Which is better, rap or metal? 1 for rap, 2 for metal, 3 for neither" );
        y = input.nextInt();

        if ( y == 1 )
            System.out.print("Someone hasn't heard
http://www.youtube.com/watch?v=Vzbc4mxm430
yet"
);

        if ( y == 2 )
            System.out.print("Someone hasn't heard
http://www.youtube.com/watch?v=s4l7bmTJ7j8
yet"
);

        if ( y == 3 )
            System.out.print("=/
Music sucks anyway."
);
    }
}

当我试图编译时:

1
2
3
4
5
6
7
8
Music.java:13: error: cannot find symbol
y = input.nextInt();



symbol: variable input
location: class Music
1 error


that is the error message telling你输入你的变量不存在在你的扫帚。你可能想使用你的扫描仪的对象,但它是你的一个叫X,不输入。P></

1
Scanner input = new Scanner( System.in );

应该修复它。P></


You have not the input这里定义的变量。You should have:P></

1
Scanner input = new Scanner( System.in );


你可以alternatively,只是变化:P></

1
y = input.nextInt();

to:P></

1
y = x.nextInt();

然后它会的工作。P></

inputis not because this is the *定义的队列中。提供你期望队列suggests the en to be an instance of the Scanner舱。but the instance of class is as Scannerx& not input真的定义。P></


你在AS used the variable输入P></

1
y=input.nextInt();

你不能做这个,因为这是不可变的。我*你的意思是,你可以"X"or replaceP></

1
Scanner x = new Scanner( System.in );

withP></

1
Scanner input = new Scanner( System.in );

1
2
Scanner input = new Scanner( System.in );
int y = input.nextInt();

(前)P></

1
2
Scanner x = new Scanner( System.in );
int y = x.nextInt();


1
2
 Scanner x = new Scanner( System.in );
 int y = x.nextInt();


This is the simple fix y=x.nextint(instead of);y=();input.nextintP></