英文:
Calling a list of objects into an method to check for enum values
问题
我刚开始学习Java,并且有一个名为 listPlayer
的对象列表,这是一个示例:
Player{FirstName='Marco', LastName='Reus', Team='Dortmund', Country='Germany', Fitness='Unfit', RecoveryTime='Slow'}
我还有一个枚举,它的值代表不同的健康状况范围:
public enum LevelOfFitness {
/**
* 代表每位球员不同健康水平的枚举
*/
CAREER_IN_DOUBT(8), INDEFINITELY_INJURED(7), INJURED(6), UNFIT(5), CLOSE_TO_FITNESS(4),
NEAR_MATCH_FIT(3),
MATCH_FIT(2), DATA_DEFICIENT(1), NOT_EVALUATED(0);
private int value;
LevelOfFitness(int aValue) {
this.value = aValue;
}
public int getValue() {
return value;
}
}
我还有一个方法,循环检查在 listPlayer
中找到的名字是否与枚举值中的名字相同:
public LevelOfFitness from(String value){
LevelOfFitness found = null;
for(LevelOfFitness level : LevelOfFitness.values()){
if(level.name().equalsIgnoreCase(value)){
return level;
}
}
throw new IllegalStateException("Not able to find fitness level for " + value);
}
我在理解如何将这个枚举方法调用到我的主程序中,并将 listPlayer
调用到循环中以便返回一个健康水平方面遇到了困难。例如,循环遍历的第一个球员检查健康水平(不适合),并返回水平 5。非常感谢。
英文:
im new to java and have a list of objects called listPlayer
heres a sample
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
public enum LevelOfFitness {
/**
* Enum that represent the different level of Fitness of each player
*/
CAREER_IN_DOUBT(8), INDEFINITELY_INJURED(7), INJURED(6), UNFIT(5), CLOSE_TO_FITNESS(4),
NEAR_MATCH_FIT(3),
MATCH_FIT(2), DATA_DEFICIENT(1), NOT_EVALUATED(0);
private int value;
LevelOfFitness (int aValue) {
this.value = aValue;
}
public int getValue () {
return value;
}
}
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.
public LevelOfFitness from(String value){
LevelOfFitness found = null;
for(LevelOfFitness level : LevelOfFitness.values()){
if(level.name().equalsIgnoreCase(value)){
return level;
}
}
throw new IllegalStateException("Not able to find fitness level for " + value);
}
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
我假设你的 `from(String)` 方法在 `enum LevelOfFitness` 中声明。
你可以将 `from(String)` 声明为一个 `static` 方法,并且可以这样调用:`LevelOfFitness.from("string");`。
```java
public static void main(String[] args){
List<Player> playerList = new ArrayList<>();
playerList.add(new Player("Marco", "Reus", "Dortmund", "Germany", "Unfit", "Slow"));
playerList.add(new Player("Name1", "Name2", "Team1", "Country1", "not_evaluated", "time1"));
playerList.add(new Player("Name1", "Name2", "Team1", "Country1", "error_fitness", "time1"));
for(Player player : playerList){
try{
LevelOfFitness lof = LevelOfFitness.from(player.getFitness());
System.out.println(lof.getValue());
}catch(Exception e){
e.printStackTrace();
}
}
}
----- 输出 -----
5
0
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("string");
;
public static void main(String[] args){
List<Player> playerList = new ArrayList<>();
playerList.add(new Player("Marco", "Reus", "Dortmund", "Germany", "Unfit", "Slow"));
playerList.add(new Player("Name1", "Name2", "Team1", "Country1", "not_evaluated", "time1"));
playerList.add(new Player("Name1", "Name2", "Team1", "Country1", "error_fitness", "time1"));
for(Player player : playerList){
try{
LevelOfFitness lof = LevelOfFitness.from(player.getFitness());
System.out.println(lof.getValue());
}catch(Exception e){
e.printStackTrace();
}
}
}
----- output -----
5
0
java.lang.IllegalStateException: Not able to find fitness level for error_fitness
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论