java编译找不到符号 int age=in.nexint()_找不到现有变量的符号(cannot find symbol for an existing variable)...

在Java代码中,由于在for循环内部声明了int min,导致min的作用域只限于循环内,因此在循环外部无法访问。解决方法是将min的声明移出循环,使其在整个方法范围内有效。
摘要由CSDN通过智能技术生成

找不到现有变量的符号(cannot find symbol for an existing variable)

public static void main(String[] args) {

int [] array= {4,5,7,1,5,4};

for(int j=0;j

int min;

min=array[0];

if(array[j]>min) {

min=array[j];

}

}

System.out.print(min);

}

}

你能帮我解释为什么min不能找到?

public static void main(String[] args) {

int [] array= {4,5,7,1,5,4};

for(int j=0;j

int min;

min=array[0];

if(array[j]>min) {

min=array[j];

}

}

System.out.print(min);

}

}

can you help me why min cannot be found?

原文:https://stackoverflow.com/questions/17889227

更新时间:2019-12-02 16:31

最满意答案

您试图从声明它的不同范围访问min 。

你在for -loop中声明它:

for(int j=0; j < array.length; j++) {

int min;

min=array[0];

if(array[j]>min) {

min=array[j];

}

} // min can't be used after this.

但是,您在循环之外使用它。 int min仅在for循环块的范围内有效(关闭循环的}结束可以访问min的块)。

将min声明移到循环之外,它应该可以工作。

int min = array[0];

for (int i : array) {

if (i > min) {

min = i;

}

}

System.out.println("The min is: " + min);

我用Java的for -e循环让你的代码在语法上更加甜美。

此外,您的代码中是否存在逻辑错误? 您调用变量min但是您指定的数字大于min到min。 听起来你正在寻找最大值,而不是最小值?

You are attempting to access min from a different scope from where it was declared.

You are declaring it inside your for-loop:

for(int j=0; j < array.length; j++) {

int min;

min=array[0];

if(array[j]>min) {

min=array[j];

}

} // min can't be used after this.

However, you are using it outside of the loop. int min is only valid within the scope of the for loop block (the } that closes the loop ends the block that min is accessible in).

Move the min declaration outside of the loop and it should work.

int min = array[0];

for (int i : array) {

if (i > min) {

min = i;

}

}

System.out.println("The min is: " + min);

I made your code a little more syntactically sweet with Java's for-each loop.

Also, is there a logic error in your code? You call the variable min but you are assigning numbers larger than min to min. Sounds like you're looking for max value, not minimum?

2013-07-26

相关问答

您需要将此变量声明为实例变量,以便在所有类方法中可访问。 我认为你已经在main方法或类方法中声明了这些变量,所以你需要将它们移出方法。 public class MyClass {

private double meters; // Distance as set by the user.

private String input; // Input by the user.

private char conversion; // Code for the type o

...

您试图从声明它的不同范围访问min 。 你在for -loop中声明它: for(int j=0; j < array.length; j++) {

int min;

min=array[0];

if(array[j]>min) {

min=array[j];

}

} // min can't be used after this.

但是,您在循环之外使用它。 int min仅在for循环块的范围内有效(关闭循环的}结束可以访问min的块)。 将m

...

对于红色代码RED在R.color中不可用。 请查看此http://developer.android.com/reference/android/R.color.html 对于你的问题,你可以试试 R.color.holo_red_dark 希望这对你有用。 For Red color code RED is not available in R.color. Check this http://developer.android.com/reference/android/R.color.ht

...

试试这个以片段绑定你的控件 @Nullable

@Override

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

View rootView = inflater.inflate(R.layout.home, container, false);

postlist = (ListView)

...

它正在处理我的代码,我已经测试过了...... 只需确保您已导入此内容: import android.widget.TextView;

然后制作一个清洁项目 。 编辑 这是你的SDK的一个问题,所以更好的解决方案是更新它。 It's working on my code that I've tested it... Just make sure that you've imported this : import android.widget.TextView;

And then make

...

看到格式将始终是一个字符和一个/两个数字(例如C25或C5),您可以这样做 public Term(String s) {

this.element = s.charAt(0);

this.atoms = Integer.parseInt(s.subString(1));

}

Seeing that the format will always be one character and one/two numbers (eg C25 or C5) you can do this

...

任何其他应用的插件? 你偶然有一个图书馆项目,它还带来了一个过时的Realm版本,例如RealmBasedRecyclerViewAdapter ? - compile 'com.github.thorbenprimke:realm-recycler-view:0.9.5'

该库已过时,建议使用http://github.com/realm/realm-android-adapters而不是RealmRecyclerViewAdapter any other applied plugins?

...

n变量的范围目前仅在括号内的do-while块内。 如果你希望它可以在更大的范围内访问,即使这意味着循环的条件,然后在循环外声明它。 int n;

do

{

n = s.nextInt();

...

The scope of the n variable is currently only inside the do-while block, within the braces. If you would like it to be accessible in a bigger

...

它的nextDouble而不是nextdouble。 您已在main中声明变量并在main之外使用它们。 对构造函数的调用应该在main方法中。 如果没有参数化构造函数,则无法以这种方式实例化类的对象。 您需要先初始化变量,然后才能使用它们。 编辑 public class Carsavings3 {

//These are your class attributes.

private double equalAmounts;

private double l;

...

你应该尝试使你的字母真正的字符: case 'V':

System.out.println("The spectrum color is Violet");

break;

你现在正在做的是检查选择是否是变量V ,当然,它不存在。 You should try making your letters actual chars: case 'V':

System.out.println("The spectrum color is Violet"

...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值