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

时间:2015-12-12 14:02:54

标签: java variables github git-bash

service_category.Items.Add(categoria.key, categoria.value)

这是错误

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

        }
    }
}

1错误

我一直在搞乱代码,错误也在上升,所以我决定问一个比我更有经验的人。我一直在使用java只有一天,这是我创建的第一个程序,我已经搜索了至少2个小时的解决方案但是找不到一个,所以我决定问。我以前编译它,但没有" user_input.next"所以它的行为就像下面的整行代码" A类"是看不见的,它不会让我输入任何东西,使它变得毫无用处。我放入user_input以便我可以输入一些东西,但是出现了错误,但这是一个不断弹出的错误。

2 个答案:

答案 0 :(得分:0)

错误很可能是由您声明扫描程序(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方法中初始化类,并运行两个方法。

答案 1 :(得分:0)

我希望你能这样做

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

}

}