import java.util.ArrayList;
import java.util.Iterator;
/**
* @author Stephanie Hoyt
* @version April 10, 2014
*/
public class Purse
{
private ArrayList<MyCoins> coins;
private int total;
/**
* Default constructor for objects of class Purse.
*/
public Purse()
{
coins = new ArrayList<MyCoins>();
}
/**
* Takes Coin as a parameter and adds Coin to the Purse.
**/
public void add(int coinValue)
{
coins.add(new MyCoins(coinValue));
total += coinValue;
}
/**
*
*/
public int getTotal()
{
return total;
}
/**
*
*/
public void showCoins()
{
coins = new ArrayList<MyCoins>();
Iterator<MyCoins> itr = coins.iterator();
while(itr.hasNext())
{
MyCoins c = itr.next();
System.out.println(c.getName());
}
}
}
所以我的問題是在showCoins()方法。我想打印出ArrayList的內容,硬幣。代碼編譯,但是當我運行它時沒有任何反應。所有其他方法工作正常。 該課程與另一個課程相關,我將在下面發佈。打印arraylist的內容時沒有輸出
public class MyCoins
{
private String myName;
private int myValue;
/**
* default constructor for MyCoins class.
*/
public MyCoins(String name, int value)
{
myName = name;
myValue = value;
}
/**
* Non-default constructor for MyCoins class.
*/
public MyCoins(int value)
{
myValue = value;
if(value == 1)
myName = new String("Penny");
else if(value == 5)
myName = new String("Nickel");
else if(value == 10)
myName = new String("Dime");
else if(value == 25)
myName = new String("Quarter");
else
throw new IllegalArgumentException("Enter the value of a US coin.");
}
/**
* Returns the current value of coins as an integer.
*/
public int getValue()
{
return myValue;
}
/**
* Returns myName as a String.
*/
public String getName()
{
return myName;
}
}
沒有主要功能。我們應該怎麼知道你究竟做錯了什麼? – fstd
您正在創建一個新的空'ArrayList'作爲該方法的第一行,並放棄當前的一行。因此......沒有什麼可輸出的。 –