import java.util.Scanner;

public class Testing {

    public static void main(String [] args)  {

        Scanner user_input = new Scanner( System.in );

        String start;
        System.out.print("(Don't put capital letters) ");

        String color;
        System.out.print("Enter Your Favorite Color: ");
        color = user_input.next ( );

        String animal;
        System.out.print("Enter Your Favorite Animal: ");
        animal = user_input.next ( );

        String preference;
        preference = color + " and that your favorite animal is a " + animal;

        System.out.println("From the information I gathered, I figured out that you like the color " + preference);

        class A {
            public void Main(String[] args) {
                Scanner input = new Scanner(System.in);
                    System.out.println("Do you want to hear a joke?");
                String a = user_input.next ( );

                if (a.equalsIgnoreCase("yes")) ;
                    System.out.println("What did the fish say when he ran into a wall? Dam");

                if(a.equalsIgnoreCase("no")) ;
                    System.out.println("Oh..."); }

        }
    }
}


这是错误

Testing.java:29: error: local variable user_input is accessed from within inner class; needs to be declared final
    String a = user_input.next ( );
               ^


1个错误

我只是在弄乱代码,错误不断增加,所以我决定问一个比我更有经验的人。我已经使用Java一天了,这是我创建的第一个程序,我已经搜索了至少2个小时的解决方案,但找不到解决方案,所以我决定问一下。我之前可以编译它,但是它没有“ user_input.next”,因此它的行为就像“ A类”下面的整个代码行是不可见的,它不允许我键入任何内容,从而使其无用。然后我输入user_input,以便我可以输入一些内容,但是出现了一些错误,但这是一个不断弹出的错误。

最佳答案

该错误很可能是由于您声明扫描仪(input),然后使用其他类(user_input)中的扫描仪引起的

试试这个代码

import java.util.Scanner;

public class Testing
{
    public void input1()
    {
        Scanner user_input = new Scanner( System.in );

        System.out.print("(Don't put capital letters) ");

        String color;
        System.out.print("Enter Your Favorite Color: ");
        color = user_input.next();

        String animal;
        System.out.print("Enter Your Favorite Animal: ");
        animal = user_input.next();

        String preference;
        preference = color + " and that your favorite animal is a " + animal;

        System.out.println("From the information I gathered, I figured out that you like the color " + preference);
    }

    public void input2()
    {
        Scanner input = new Scanner(System.in);
        System.out.println("Do you want to hear a joke?");
        String a = input.next();

        if(a.equalsIgnoreCase("yes"))
        {
            System.out.println("What did the fish say when he ran into a wall? Dam");
        }
        else if(a.equalsIgnoreCase("no"))
        {
            System.out.println("Oh...");
        }
    }

    public static void main(String[] args)
    {
        Testing tS = new Testing();
        tS.input1();
        tS.input2();
    }
}


在此代码中,有两种方法; input1input2。这些方法包含您尝试与命令控制台进行的两次对话。然后,在main方法中,该类被初始化,并且它运行两个方法。

关于java - 从内部类内部访问局部变量user_input;需要宣布为最终,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34240696/

10-14 22:48