+ 3
In what manner we can use enums ?
how we can use enums in our program
2 Answers
+ 11
Very sinple example:
https://code.sololearn.com/cRlhGLZUpL64/?ref=app
0
class Main {
public static void main(String[] args) {
Player player1 = new Player(Difficulty.EASY);
Player player2 = new Player(Difficulty.MEDIUM);
Player player3 = new Player(Difficulty.HARD);
}
}
enum Difficulty {
EASY(3000),
MEDIUM(2000),
HARD(1000);
public final int bullets;
private Difficulty(int bullets) {
this.bullets = bullets;
}
}
public class Player{
Player(Difficulty diff){
//your code goes here
int bullets = diff.bullets;
/*
switch (diff) {
case EASY:
bullets = 3000; break;
case MEDIUM:
bullets = 2000; break;
case HARD:
bullets = 1000; break;
}
*/
System.out.format("You have %d bullets", bullets);
}
}