Java如何得到每个枚举的所有值的总结 [英] Java How to get the sum up all the of the values of each enum
本文介绍了Java如何得到每个枚举的所有值的总结的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试了解如何获取每个枚举的所有值,并将其全部添加到一个变量中。我的代码。
I am trying to understand how I could get all of the values for each enum, and add all of it together in one variable. my code.
enum Junk {
Wrapper(0),
SilverCoins(150),
Dresser(250),
Sofa(250),
ChippedBat(0),
BrokenMicrowave(0),
SilverWare(35),
Newspapers(0),
Book(20),
Clothes(60),
Plates(40),
Cups(25),
DiningTable(200),
Chair(30),
BoxOfPaper(10),
BucketofPaint(10),
Watch(30);
}
我想知道如何添加所有
推荐答案
以下是您可能期望的完整代码。
Following is your complete code that you may expect.
package enumtest;
enum Junk
{
Wrapper(0),
SilverCoins(150),
Dresser(250),
Sofa(250),
ChippedBat(0),
BrokenMicrowave(0),
SilverWare(35),
Newspapers(0),
Book(20),
Clothes(60),
Plates(40),
Cups(25),
DiningTable(200),
Chair(30),
BoxOfPaper(10),
BucketofPaint(10),
Watch(30);
private int value;
private Junk(int value)
{
this.value=value;
}
public int getValue()
{
return(value);
}
}
final public class Main
{
public static void main(String...args)
{
int sum=0;
for(Junk o:Junk.values())
{
sum+=o.getValue();
}
System.out.println("sum = "+sum);
}
}
如果您尝试尝试,将在控制台上显示 sum = 1110
。
这篇关于Java如何得到每个枚举的所有值的总结的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文