Java verwendet HashMap mit Switch -Anweisung

Post a reply

Smilies
:) :( :oops: :chelo: :roll: :wink: :muza: :sorry: :angel: :read: *x) :clever:
View more smilies

BBCode is ON
[img] is ON
[flash] is OFF
[url] is ON
Smilies are ON

Topic review
   

Expand view Topic review: Java verwendet HashMap mit Switch -Anweisung

by Guest » 07 Feb 2025, 01:02

Ich habe eine Konstantenklasse, in der ich Hashmaps von Konstanten wie: < /p>

gespeichert habeimport java.util.HashMap;
import java.util.Map;

/**
* Constantes de uso general en el programa.
*/
public final class Consts {

// Opciones del menu de juego.
public static final Map GAMETYPE;
static
{
GAMETYPE = new HashMap();
GAMETYPE.put(1, "MANUAL");
GAMETYPE.put(2, "AUTOMATIC");
GAMETYPE.put(3, "EXIT");
}

/**
*
* @param userType
* @return
*/
public static String valueOf(int userType) {
return GAMETYPE.get(userType);
}
/**
* Impide construir objetos de esta clase.
*/
private Consts(){
// Tampoco permite a la clase nativa llamar al constructor.
throw new AssertionError();
}
}
< /code>

Ich möchte diese Konstanten in einer Switch-Case-Anweisung in einer anderen Klasse verwenden wie: < /p>

userType = sc.nextInt();
switch(Consts.valueOf(userType)) {
case MANUAL:
System.out.println(">> You have selected the manual mode");
break;
case AUTO:
System.out.println(">> You have selected the manual mode");
break;
case EXIT:
System.out.println(">> Good-bye");
break;
< /code>

Das Programm findet jedoch weder manuell noch automatisch oder beendet. Irgendeine Idee? erschwert schwierig dem Code) und ich möchte nicht, dass die Konstanten nacheinander deklarieren wie: < /p>

public static final int MANUAL = 1;
public static final int AUTO = 2;
public static final int EXIT = 3;
< /code>

Da ich möchte, dass die Konstanten in der Konstantenklasse strukturiert werden. Danke!

Top