简体   繁体   English

从内部类内部访问局部变量user_input; 需要宣布为最终

[英]local variable user_input is accessed from within inner class; needs to be declared final

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..."); }

        }
    }
}

and this is the error 这是错误

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 error 1个错误

I've just been messing around with the code and the errors go up and up so i decided to ask someone more experienced than myself. 我只是在弄乱代码,错误不断增加,所以我决定问一个比我更有经验的人。 I have been using java for only a day and this is my first program that i've created, i've searched for at least 2 hours for a solution but couldn't find one, so i decided to ask. 我已经使用Java一天了,这是我创建的第一个程序,我已经搜索了至少2个小时的解决方案,但找不到解决方案,所以我决定问一下。 I got it to compile before, but it was without "user_input.next" so it acted like the whole line of code below "class A" was invisible and it wouldn't let me type anything, making it useless. 我之前可以编译它,但是它没有“ user_input.next”,因此它的行为就像“ A类”下面的整个代码行是不可见的,它不允许我键入任何内容,从而使其无用。 I than put in user_input so that i could input something, but TONS of errors came up, but this is the one error that keeps popping up. 然后我输入user_input,以便我可以输入一些内容,但是出现了许多错误,但这是一个不断弹出的错误。

The error is most likely caused by you declaring a scanner ( input ) and then using the scanner from the other class ( user_input ) 该错误很可能是由于您声明一个扫描仪( input ),然后使用其他类的扫描仪( user_input )引起的

Try this code 试试这个代码

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();
    }
}

In this code there are two methods; 在此代码中,有两种方法: input1 and input2 . input1input2 These methods contain the two conversations you tried to have with your command console. 这些方法包含您尝试与命令控制台进行的两次对话。 Then in the main method the class is initialized and it runs the two methods. 然后在main方法中初始化该类,并运行两个方法。

I hope you want to do this 我希望你这样做

public class Test2 {
private static Scanner user_input = new Scanner(System.in);

private static boolean wantsToContinue(){
    System.out.println("Do you want to hear a joke?");
    String a = user_input.next();
    if(a.equalsIgnoreCase("yes"))
        return true;
    else
        return false;
}

public static void main(String[] args)
{    
    System.out.print("(Don't put capital letters) ");

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

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

    String 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);


    if(!wantsToContinue()){
        //If not want to continue just return
        System.out.println("Oh...");
        return;
    }

    //Continue with your next joke here
    System.out.println("What did the fish say when he ran into a wall? Dam");

}

} }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “从内部类访问变量需要声明为final”错误 - "variable is accessed from within inner class needs to be declared final" error 从内部类访问变量“ ”,需要声明为 final - Variable ' ' is accessed from within inner class, needs to be declared final 从内部 class 访问的变量需要声明为 final - Variable accessed from within inner class needs to be declared final 变量(dialogView)是从内部类中访问的,需要声明为final - Variable (dialogView) is accessed from within inner class, needs to be declared final 从内部类访问变量“i”,需要声明为 final - Variable 'i' is accessed from within inner class, needs to be declared final 变量'btnsave'是从内部类中访问的,需要声明为final - Variable 'btnsave' is accessed from within inner class, needs to be declared final Intellij不会报告从内部类内部访问了局部变量,需要将其声明为final - Intellij doesn't report that a local variable is accessed from within inner class and needs to be declared final 错误:从内部类访问局部变量 imagesLabel; 需要声明为 final (Java) - Error: local variable imagesLabel is accessed from within inner class; needs to be declared final (Java) 错误:从内部类中访问局部变量事件; 需要宣布最终 - Error: local variable event is accessed from within inner class; needs to be declared final 错误:从内部类访问局部变量 a; 需要宣布为最终 - Error: local variable a is accessed from within inner class; needs to be declared final
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM