无法解析为变量 [英] cannot be resolved to variable

查看:270
本文介绍了无法解析为变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Java编程的新手,请休息一下.

I'm new to coding with Java so please give me a break.

运行此代码时,

public class operators {

public static int getMonthNumber(String monthName) {
    int monthNumber;
    switch (monthName) {
    case "January": monthNumber=1;
    break;
    case "February": monthNumber=2;
    break;
    default: monthNumber=0;

    }


}
public static void main(String[] args) {
    operators.getMonthNumber("January");
    System.out.println(monthNumber);
        }
}

我收到此消息,"monthNumber无法解析为变量".我不明白为什么它不只输出整数1.我知道解决方法是替换

I get this message, "monthNumber cannot be resolved to a variable". I don't understand why it doesn't just print the integer 1. I know the solution would be replace

operators.getMonthNumber("January");

与此

int returnedMonthNumber=operators.getMonthNumber("January");

为什么不用分配变量给整数就只打印整数1?

Why can't it just print the integer 1 without having to assign a variable to the integer?

推荐答案

正如其他人所说,此站点不是一门学习该语言基础知识的学校.但是迟早会有人们会来这里寻找您确切问题的答案.

As others have stated, this site is not a school for learning the basics of the language. But sooner or later people will propably come here in search for an answer to your exact question.

您对Java的工作方式有极大的误解.在您的代码中,您犯了两个错误.首先,您尝试访问一个可能已经消失的变量(如果可以访问的话).其次,您尝试访问一个在调用位置看不到的变量.您的代码:

You have a huge misunderstand of how Java works. In your code, you are making two mistakes. First, you try to acess a variable that would be already gone, if you could acess it. And second, you try to access a variable you can't "see" from where you are calling. Your code:

public static void main(String[] args) {
  operators.getMonthNumber("January");
  System.out.println(monthNumber);
} 

有关第一个问题的更多信息:您可以在方法getMonthNumber中声明变量monthNumber.因此,它仅在方法期间存在.因此,在调用方法时,变量就在那里.方法之后,变量消失了.它被扔掉了.但是在您的方法中将返回变量值的副本.因此,您可以将副本放入新变量中,然后使用它或直接使用它.那就是你用

More info on first problem: You declare the variable monthNumber in your method getMonthNumber. So it only exists for the duration of the method. So when calling the method, the variable is there. After the method, the variable is gone. It gets thrown away. But a copy of the value of the variable is returned in your method. So you can take the copy and put it into a new variable and use it or use it directly. That's what you pointed out with

int返回的MonthNumber = operators.getMonthNumber("January");

int returnedMonthNumber=operators.getMonthNumber("January");

我知道没有一种编程语言可以像您期望的那样工作.如果有的话,它将不会真正流行,因为这可能会导致很多错误,因为错字可能导致程序使用您不想使用的完全不同的变量.

There is no programming language i know of that works the way you expect it to work. And if there was one, it wouldnt be really popular because this could cause a lot of bugs because a typo could lead to the program using a completely different variable you didnt want to use.

有关第二个问题的更多信息:一般而言,变量只能在方括号内访问.也就是说,在{}中声明的变量只能在相同的括号中或在这些括号中的括号中访问.示例:

More info on second problem: Generally speaking, variables are only accessible within brackets. That means, variables declared within {} are only accessible within the same brackets, or brackets within those brackets. Example:

public static void main(String[] args) { // first bracket

    final int dollar = 2;
    System.out.println("i can access dollar: " + dollar);

    {
        final int euro = 33;
        System.out.println("I can access dollar: " + dollar + ", and i can access euro: " + euro);
    }

    // you have to remove +euro to compile this:
    System.out.println("But i cant access euro because i am outside the brackets where euro is declared: " + euro);
}

请牢记此规则,因为它始终有效.它与方法,ifs,while/for/do while等一起使用.如果编译器看不到变量,则可能是拼写错误或错误的块.

Keep this rule in mind as it always works. It works with methods, with ifs, with while/for/do while etc. If the compiler can't see a variable, it's propably a typo or the wrong block.

我已经看到很多初学者都说过像您所说的话.他们想知道为什么编译器不知道我想要什么?这很明显!".但这实际上并不明显.您期望的行为只是在您的脑海中有用".这样可以节省一半的代码.但是在任何更高级的示例中,您都会看到按照您期望的方式实现它是一个非常糟糕的主意.每个初学者都会在某个时候意识到这一点:如果您想知道为什么编译器不会执行您想要执行的操作,那么您只是没有告诉他执行您想要执行的操作.如果您知道我的意思.

I have seen a lot of beginners saying something like you said. They wonder "why won't the compiler know what i want? It's obvious!". But it's actually not obvious. The behaviour you expect is only "useful" in your head. It might save you half a line of code. But in any more advanced example you will see it would be a really bad idea to implement it the way you expect it to be. And every beginner realizes this at some point: If you wonder why the compiler won't do something you want it to do, then you just didn't tell him to do what you want it to do. If you know what i mean.

这篇关于无法解析为变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆