Today my teacher asked me to program a random presentation topic picker for him.
The idea was, that the student goes to the pc and clicks on the message Dialog which then randomly generates a number between 1 and the max index of the topics and then prints the according topic.
I tried it with HashMaps. to put in the key that stays with the String together so that I can then (after the output) remove that entry so that no other student can get the same topic.
But it always returns at least 1 empty reference -> null.
Can anyone help me? Please :)
thats the code:
static HashMap<Integer, String> map = new HashMap<>();
public static void main(String[] args){
int anzahlEintraege = Integer.parseInt(JOptionPane.showInputDialog("Wie viele Themen gibt es?"));
for(int i = 0; i < anzahlEintraege; i++){
map.put((i+1),JOptionPane.showInputDialog("Geben Sie das Thema Nummer " + (i+1) + " ein!"));
}
JOptionPane.showMessageDialog(null, "Jetzt geht's Los!");
int max = map.size();
int removed = 0;
for(int i = 0; i < max; i++){
Random r = new Random();
int random = r.nextInt(max-1)+1;
JOptionPane.showMessageDialog(null, "Sie haben das Thema "+ map.get(random) + " gezogen!");
map.remove(random);
removed++;
}
}
Aucun commentaire:
Enregistrer un commentaire