未解决的编译问题:答案无法解析为变量 [英] Unresolved compilation problem: answer cannot be resolved to a variable

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

问题描述

使用我的下面的代码,为什么答案没有得到解决?



< pre>包计算; 

import java.io.IOException;

public class 减去{

public int sub( int nb11, int nb21) throws IOException {



if (nb11> nb21){

int answer = Integer.parseInt(null, nb11 - nb21);


} else
{

System.out.println( 您不能从较小的数字中减去一个大数字。);

String [] args1 = null;
Cal.main(args1);

}


return answer;
}

}











我尝试了什么:



尝试创建一个局部变量并对其进行初始化但仍然是计算结果未传递给答案。

解决方案

您的代码的问题(可能是其中一个问题)是 answer 的范围:如果在 块内,它存在,所以

  return  answer; 

语句只是一个错误(范围内没有 answer 变量)。


< blockquote>正如卡罗所说,这是一个范围问题。您应该在 if 语句之外声明该变量。此外,对 parseInt 的调用没有意义,因为你没有处理字符串值。

  public   int  sub( int  nb11, int  nb21) throws  IOException {
int answer = - 1 ;
if (nb11> nb21){
answer = nb11 - nb21;
}
else
// ...


查看你的代码!

你定义并设置<的值code> answer 仅在验证此条件(nb11> nb21)时才验证。



我最好的建议是使用调试器来查看并理解错误之前代码的作用。



http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html [ ^ ]

https://www.jetbrains .com / idea / help / debugging-your-first-java-application.html [ ^ ]



调试器在这里向您展示您的代码正在做,你的任务是与它应该做的事情进行比较。

调试器中没有魔法,它没有发现错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。


With my below code, why is answer not being resolved?

<pre>package calculate;

import java.io.IOException;

public class Subtract {

	public int sub(int nb11, int nb21) throws IOException {
		
		
		
		if (nb11 > nb21){		
			
			int answer = Integer.parseInt(null, nb11 - nb21);
			
			
		}else 
		{
			
			 System.out.println("You can not subtract a large number from a small number.");
	         
				String[] args1 = null;
				Cal.main(args1 );	
			
		}		
		
		
		return answer;
	}

}






What I have tried:

Tried creating a local variable and initialized it but still the result of the calculation is not passed to answer.

解决方案

The problem (maybe one of the problems) of your code is answer's scope: it 'lives' inside the if block, so the

return answer;

statement is just an error (there's no answer variable in scope).


As Carlo says, it is a question of scope. You should declare the variable outside the if statement. Also the call to parseInt makes no sense, since you are not dealing with a string value.

public int sub(int nb11, int nb21) throws IOException {
    int answer = -1;
    if (nb11 > nb21){
        answer = nb11 - nb21;
    }
    else
// ...


Look at your code !
You define and set the value of answer only when this condition (nb11 > nb21) is verified.

My best advise is to use a debugger to see and understand what your code is doing before the error.

http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


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

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