本文區區持續低於Java的回憶隨機選擇的枚舉和總結這一切
Java How to get the sum up all the of the values of each enum
完整的源代碼
我有這個For循環之下,我今天要問你們的是我如何回憶所有隨機選擇的枚舉,並總結它們的所有值。
for (int i = amount; i > 0; --i){//determines the amount of cycles.
Junk randomX = Junk.values()[random.nextInt(Junk.values().length)];
//randomly picks an enum
System.out.println(randomX);
}
下面我該如何解決我做這基本上宣佈INT總和,初始化它,並把它在for循環收集枚舉
for (int i = amount; i > 0; --i){
Junk randomX = Junk.values()[random.nextInt(Junk.values().length)];
//randomly picks an enum
System.out.println(randomX);
for(Junk o : Junk.values()){
sum += o.getValue();
//sorry, I had sum declared and initialized outside of the loop.
System.out.println(sum);
}
}
可悲的值時,輸出不希望
Dresser
0
150
400
650
650
650
...
我明白,我基本上是問別人幫我在爲我編寫的代碼,但我不確定是怎麼回事做到這一點。
另外我明白,可能不可能考慮獲得枚舉值的總和的唯一方法是通過在for循環之外執行它,然後這將使解決方案變得不可能,因爲它只會獲得所有的價值。
也許除了我可以使用的for循環之外,還有一些東西可以推薦我使用。
謝謝。沒有意識到這一點 – Streak324 2011-12-24 17:17:27