【问题标题】:Java compile error: cannot find symbol [duplicate]Java编译错误:找不到符号[重复]
【发布时间】:2012-08-27 13:29:34
【问题描述】:

嘿,我刚开始写第一本关于 java 的编程书,所以这应该很容易解决。 乱用我对条件句的新知识,我得到了标题错误。

代码如下:

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\nhttp://www.youtube.com/watch?v=Vzbc4mxm430\nyet" );

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

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

当我尝试编译时:

Music.java:13: error: cannot find symbol
y = input.nextInt();



symbol: variable input
location: class Music
1 error

【问题讨论】:

  • 那么,你在哪里定义一个名为input的变量?
  • +1 不明白为什么有人会 -1 这个完整的问题。

标签: java compiler-errors


【解决方案1】:

错误消息告诉您变量“输入”在您的范围内不存在。您可能想使用您的 Scanner 对象,但您将其命名为“x”,而不是“输入”。

Scanner input = new Scanner( System.in );

应该修复它。

【讨论】:

    【解决方案2】:

    您还没有在这里定义变量input。你应该有:

    Scanner input = new Scanner( System.in );
    

    【讨论】:

    • downvoter - 愿意发表评论吗?
    • 我的回答也一样。我希望我知道为什么。
    【解决方案3】:

    你使用了变量输入,如

    y=input.nextInt();
    

    你不能这样做,因为它不是一个变量。我相信你的意思是它是“x”,或者你可以替换

    Scanner x = new Scanner( System.in );
    

    Scanner input = new Scanner( System.in );
    

    【讨论】:

      【解决方案4】:

      或者,您可以更改:

      y = input.nextInt();
      

      收件人:

      y = x.nextInt();
      

      然后它将起作用。

      这是因为input 没有在代码中的任何地方定义。提供的代码表明您希望它是Scanner 类的一个实例。但是Scanner类的实例实际上定义为x而不是input

      【讨论】:

        【解决方案5】:
         Scanner x = new Scanner( System.in ); 
         int y = x.nextInt();
        

        【讨论】:

          【解决方案6】:
          Scanner input = new Scanner( System.in );
          int y = input.nextInt();
          

          (或)

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

          【讨论】:

          • 再解释一下会有用。
          【解决方案7】:

          这是简单的修复 y = x.nextInt();而不是 y = input.nextInt();

          【讨论】:

          • 这个问题在两年多前发布后两分钟得到了充分的回答。您现在没有在此处添加任何新内容。
          猜你喜欢
          • 2013-03-08
          • 2013-01-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-01-31
          • 2019-05-31
          • 2013-12-06
          • 2013-12-09
          相关资源
          最近更新 更多