找不到符号 - 变量无论如何

时间:2014-11-18 04:15:39

标签: java

我最近发现了编程,并且根据朋友的建议决定尝试让我的第一个项目成为疯狂的lib游戏。 我遇到了一个相当令人沮丧的事情 当我尝试编译时,“找不到符号 - 变量无论什么”。 我已经粘贴了下面的代码,希望有人可以给我一些反馈,说明我做错了什么。对人好点!我是全新的lol

 import java.util.Scanner; 
    public class MadLib 
    { 
     private static Scanner in = new Scanner(System.in);

    public static void main(String[] args) {
        System.out.println("What is your name >");
        String name = in.nextLine();
        System.out.println("What is your favourite colour >");
        String colour = in.nextLine();
        System.out.println("Name an animal >");
        String animal = in.nextLine();
        System.out.println("Give a number >");
        int nm = in.nextInt();

        madlib(name);
    }

    public static void madlib(String name) {
        System.out.print("Hi, my name is " + name + "!");
        System.out.print("Let me tell you a story, one day I was walking down the street,");
        System.out.print("and I saw a wild " + animal + " on the sidewalk! It was a very odd shade of " + colour + ".");
        System.out.print("It was scary!!");
    }
    }

4 个答案:

答案 0 :(得分:1)

您正尝试在animal方法中使用变量colourmadlib,但它们在该范围内不可见。您可以将它们作为参数传递:

public static void madlib (String name, String animal, String colour) 
{ 
System.out.print ("Hi, my name is " + name + "!"); 
System.out.print ("Let me tell you a story, one day I was walking down the street,"); 
System.out.print ("and I saw a wild " + animal + " on the sidewalk! It was a very odd shade of " + colour + "."); 
System.out.print ("It was scary!!"); 
}

然后将main中的方法调用更改为:

madlib (name, animal, colour); 

答案 1 :(得分:0)

madlib方法中,您在方法的上下文中引用了不存在的变量。它们可能存在于main方法中,但它们是局部变量,在main方法之外不可见。

最简单的解决方法是将必要的变量作为参数传递给madlib方法:

public static void madlib(String name, String animal, String colour) {
    ...
}

然后像这样调用方法:

madlib(name, animal, color);

答案 2 :(得分:0)

问题在于范围界定。变量动物& color在方法main的范围内,因此对main方法之外的任何内容都不可见。所以你对动物和动物的提及MadLib方法中的颜色会导致错误。

解决方案是使变量类变量或将它们传递给方法MadLib,就像使用名称一样。

答案 3 :(得分:0)

您的变量应该在类中声明,而不是主方法,以便所有方法都可以访问它们。在Google中查找可变范围。例如,这将起作用

     import java.util.Scanner; 
public class MadLib 
{    
      String name, colour, animal;
 private static Scanner in = new Scanner(System.in);
public static void main(String[] args) {
    System.out.println("What is your name >");
    name = in.nextLine();
    System.out.println("What is your favourite colour >");
     colour = in.nextLine();
    System.out.println("Name an animal >");
     animal = in.nextLine();
    System.out.println("Give a number >");
    int nm = in.nextInt();

    madlib();
}
     //You don't necessarily need add a parameter, since this method
     //can now access all of the variables from the class 
public static void madlib() {
    //Use System.out.println() so that each sentence is in its own new line
    System.out.println("Hi, my name is " + name + "!");
    System.out.println("Let me tell you a story, one day I was walking down the street,");
    System.out.println("and I saw a wild " + animal + " on the sidewalk! It was a very odd shade of " + colour + ".");
    System.out.println("It was scary!!");
}
}