Hallo,
ich möchte einen 4stelligen Zufallscode zwischen 1 und 8 ausgeben, das klappt auch, allerdings tritt der Code teilweise mit doppelten Ziffern auf, was nicht passieren soll. Was kann ich da eingeben?
import java.util.Random;
public class Zufallscode {
/**
* Prints random numbers between 1 and 8 (inklusiv)
*/
public static void main(String[] args) {
Random random = new Random();
int zufallsZahl;
for(int i=0; i<4; i++){
zufallsZahl = 1+ random.nextInt(8);
System.out.println(zufallsZahl);
}
}
}
ich möchte einen 4stelligen Zufallscode zwischen 1 und 8 ausgeben, das klappt auch, allerdings tritt der Code teilweise mit doppelten Ziffern auf, was nicht passieren soll. Was kann ich da eingeben?
import java.util.Random;
public class Zufallscode {
/**
* Prints random numbers between 1 and 8 (inklusiv)
*/
public static void main(String[] args) {
Random random = new Random();
int zufallsZahl;
for(int i=0; i<4; i++){
zufallsZahl = 1+ random.nextInt(8);
System.out.println(zufallsZahl);
}
}
}