当我运行以下程序时,出现'找不到符号'错误。这可能是由一个愚蠢的错误造成的,但我花了大约一个小时试图修复它,我不知道问题是什么。下面是代码:为什么我在这个程序中遇到'找不到符号'错误?
import java.util.*;
public class Purse{
private ArrayList<String> coins;
public Purse(){
coins = new ArrayList<String>();
}
public void addCoin(String coin){
if(coin == "Quarter" || coin == "Dime" || coin == "Nickel")
coins.add(coin);
}
public void removeCoin(String coin){
coins.remove(coin);
}
public void transfer(Purse other){
for(int i = 0; i < other.coins.size(); i++)
coins.add(other.coins.get(i));
other.remove(i);
}
}
,这里是它给我的错误:
Purse.java:23: error: cannot find symbol
other.remove(i);
^
symbol: variable i
location: class Purse
1 error
程序应该是“移动”从一个ArrayList的项目的方式。
也看到[我如何在Java中比较字符串(http://stackoverflow.com/questions/513832/how- do-i-compare-strings-in-java) – SomeJavaGuy
你需要用'for'循环的大括号。 – Kevin
您需要使用括号来包围您想要由'for'循环执行的代码。但除此之外,你不应该使用'=='比较字符串。 –