调用一个对象列表到一个方法中,以检查枚举值。

huangapple go评论164阅读模式
英文:

Calling a list of objects into an method to check for enum values

问题

我刚开始学习Java,并且有一个名为 listPlayer 的对象列表,这是一个示例:

  1. Player{FirstName='Marco', LastName='Reus', Team='Dortmund', Country='Germany', Fitness='Unfit', RecoveryTime='Slow'}

我还有一个枚举,它的值代表不同的健康状况范围:

  1. public enum LevelOfFitness {
  2. /**
  3. * 代表每位球员不同健康水平的枚举
  4. */
  5. CAREER_IN_DOUBT(8), INDEFINITELY_INJURED(7), INJURED(6), UNFIT(5), CLOSE_TO_FITNESS(4),
  6. NEAR_MATCH_FIT(3),
  7. MATCH_FIT(2), DATA_DEFICIENT(1), NOT_EVALUATED(0);
  8. private int value;
  9. LevelOfFitness(int aValue) {
  10. this.value = aValue;
  11. }
  12. public int getValue() {
  13. return value;
  14. }
  15. }

我还有一个方法,循环检查在 listPlayer 中找到的名字是否与枚举值中的名字相同:

  1. public LevelOfFitness from(String value){
  2. LevelOfFitness found = null;
  3. for(LevelOfFitness level : LevelOfFitness.values()){
  4. if(level.name().equalsIgnoreCase(value)){
  5. return level;
  6. }
  7. }
  8. throw new IllegalStateException("Not able to find fitness level for " + value);
  9. }

我在理解如何将这个枚举方法调用到我的主程序中,并将 listPlayer 调用到循环中以便返回一个健康水平方面遇到了困难。例如,循环遍历的第一个球员检查健康水平(不适合),并返回水平 5。非常感谢。

英文:

im new to java and have a list of objects called listPlayer heres a sample

  1. Player{FirstName='Marco', LastName='Reus', Team='Dortmund', Country='Germany', Fitness='Unfit', RecoveryTime='Slow'}

i also have an enum that values a range of fitness levels

  1. public enum LevelOfFitness {
  2. /**
  3. * Enum that represent the different level of Fitness of each player
  4. */
  5. CAREER_IN_DOUBT(8), INDEFINITELY_INJURED(7), INJURED(6), UNFIT(5), CLOSE_TO_FITNESS(4),
  6. NEAR_MATCH_FIT(3),
  7. MATCH_FIT(2), DATA_DEFICIENT(1), NOT_EVALUATED(0);
  8. private int value;
  9. LevelOfFitness (int aValue) {
  10. this.value = aValue;
  11. }
  12. public int getValue () {
  13. return value;
  14. }

}

i also have a method that loops to check if the name found in listPlayer is the same as the one found in the enum values.

  1. public LevelOfFitness from(String value){
  2. LevelOfFitness found = null;
  3. for(LevelOfFitness level : LevelOfFitness.values()){
  4. if(level.name().equalsIgnoreCase(value)){
  5. return level;
  6. }
  7. }
  8. throw new IllegalStateException("Not able to find fitness level for " + value);
  9. }

im struggling to understand how to call this enum method into my main and call listPlayer into the loop so it can return a level of fitness.For example the first player to loop through check level (unfit) and return 5 as the level many thanks.

答案1

得分: 1

  1. 我假设你的 `from(String)` 方法在 `enum LevelOfFitness` 中声明。
  2. 你可以将 `from(String)` 声明为一个 `static` 方法,并且可以这样调用:`LevelOfFitness.from("string");`
  3. ```java
  4. public static void main(String[] args){
  5. List<Player> playerList = new ArrayList<>();
  6. playerList.add(new Player("Marco", "Reus", "Dortmund", "Germany", "Unfit", "Slow"));
  7. playerList.add(new Player("Name1", "Name2", "Team1", "Country1", "not_evaluated", "time1"));
  8. playerList.add(new Player("Name1", "Name2", "Team1", "Country1", "error_fitness", "time1"));
  9. for(Player player : playerList){
  10. try{
  11. LevelOfFitness lof = LevelOfFitness.from(player.getFitness());
  12. System.out.println(lof.getValue());
  13. }catch(Exception e){
  14. e.printStackTrace();
  15. }
  16. }
  17. }
  18. ----- 输出 -----
  19. 5
  20. 0
  21. java.lang.IllegalStateException: 无法找到 error_fitness 的适应级别
英文:

I'm assuming your from(String) method is declared in enum LevelOfFitness.

You care make from(String) a static method, and call like: LevelOfFitness.from(&quot;string&quot;);;

  1. public static void main(String[] args){
  2. List&lt;Player&gt; playerList = new ArrayList&lt;&gt;();
  3. playerList.add(new Player(&quot;Marco&quot;, &quot;Reus&quot;, &quot;Dortmund&quot;, &quot;Germany&quot;, &quot;Unfit&quot;, &quot;Slow&quot;));
  4. playerList.add(new Player(&quot;Name1&quot;, &quot;Name2&quot;, &quot;Team1&quot;, &quot;Country1&quot;, &quot;not_evaluated&quot;, &quot;time1&quot;));
  5. playerList.add(new Player(&quot;Name1&quot;, &quot;Name2&quot;, &quot;Team1&quot;, &quot;Country1&quot;, &quot;error_fitness&quot;, &quot;time1&quot;));
  6. for(Player player : playerList){
  7. try{
  8. LevelOfFitness lof = LevelOfFitness.from(player.getFitness());
  9. System.out.println(lof.getValue());
  10. }catch(Exception e){
  11. e.printStackTrace();
  12. }
  13. }
  14. }
  15. ----- output -----
  16. 5
  17. 0
  18. java.lang.IllegalStateException: Not able to find fitness level for error_fitness

huangapple
  • 本文由 发表于 2020年8月18日 21:32:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/63469775.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定