如何在数组中添加枚举类型值

时间:2013-09-19 21:35:05

标签: java enums

这是我尝试添加所有内容的方法。我试图做的是加一系列硬币,这些硬币被命名为penny,dime ...等。我在枚举中给了他们所有的价值。但是如何访问数组中的每个硬币,然后将它们的值相加?

public double totalValue(Coin[] coins)
{
    double sum = 0;

    //computes and returns the monetary value of all the coins in the jar
    for(int i = 0; i < coins.length; i++) 
    {
        double coinValue = coins[i].CoinName.getCoinValue();
        sum = sum + coins[i];
        System.out.println(sum);
    }
    return sum; //change this!
}

这里是定义枚举值的地方。

public enum CoinName
{
     PENNY(.01), NICKEL(.05), DIME(.10), QUARTER(.25), FIFTY_CENT(.50), DOLLAR(1.0);

private double value;
private double coinValue;

private CoinName(double value)
{
    this.coinValue = value;
}

public double getCoinValue() 
{

    return coinValue;
}

}

/////我刚刚加了我的硬币课。

import java.util.Random;
public class Coin
{

public static long SEED = System.currentTimeMillis();
public static Random RANDOM = new Random(SEED);

//private instance variables denomination, year,  and sideUp: year is an int,        denomination is of type  CoinName and  sideUp is of type  CoinSide
private CoinName denomination;
private CoinSide sideUp;
private int year;

public Coin(CoinName denomination,int year) 
{
    this.denomination = denomination;
    this.year = year;

    int side = Coin.RANDOM.nextInt(2);
    if (side == 0)
    {
        sideUp = CoinSide.HEADS;
    }
    else 
        sideUp = CoinSide.TAILS;
}

//Accessors for denomination, year and sideUp

public CoinName setDenomination() 
{   
    int i = 0;
    i = Coin.RANDOM.nextInt(6);
    if (i == 0)
    {
    denomination = CoinName.PENNY;
    }
    if (i == 1)
    {
    denomination = CoinName.NICKEL;
    }
    if (i == 2)
    {
    denomination = CoinName.DIME;
    }
    if (i == 3)
    {
    denomination = CoinName.QUARTER;
    }
    if (i == 4)
    {
    denomination = CoinName.FIFTY_CENT;
    }
    if (i == 5)
    {
    denomination = CoinName.DOLLAR;
    }

    return denomination;
}

public CoinName getDenomination()
{
    return denomination;
}

public void setSideUp(CoinSide sideUp)
{
    sideUp = sideUp;
}

public CoinSide getSideUp()
{
    return sideUp;
}

public void setYear(int year)
{
    year = RANDOM.nextInt(2013-1873) + 1873;
}

public int getYear()
{
    return year;
}

//the standard toString method that prints out a coin in the format “PENNY/1990/HEADS”
public String toString()
{
    return denomination + "/" + year + "/" + sideUp;
}

//the standard equals method that checks if two Coin objects are equal – they are equal if the denominations are identical
public boolean equals(Object obj)
{
    if (obj instanceof Coin){
        Coin d = (Coin)obj;
        if (this.getDenomination()==d.getDenomination())
            return true;
        else
            return false;
    }
    return false;
}
public void toss()
{
    //flip the coin
    //Use the object RANDOM to generate random numbers
    int side = Coin.RANDOM.nextInt(2);
    if (side == 0)
    {
        sideUp = CoinSide.HEADS;
    }
    else 
        sideUp = CoinSide.TAILS;


}

}

2 个答案:

答案 0 :(得分:1)

Coindenomination,其中包含硬币的值。根据你如何定义事物,总结硬币数组的值,你必须先得到面额,然后从中提取值:

for(int i = 0; i < coins.length; i++) 
{
    CoinName denomination = coins[i].getDenomination();
    sum = sum + denomination.getCoinValue();
    System.out.println(sum);
}

请注意,要使其工作,硬币数组必须是满的,没有null值,并且每个硬币必须有面额。

答案 1 :(得分:0)

假设您的Coin类的CoinName属性类型为CoinName,那么您需要更改该行

sum = sum + coins[i];

使用之前得到的coinValue行。所以改为

sum = sum + coinValue;

或通常这将是

sum += coinValue;

你的Enum还有两个双打,其中一个不需要,删除value