I'm trying to get a pin number with only 4 digits. But the numbers that are less than 1000 are also getting printed. What is happening in this code?
import java.util.Random;
public class Util {
public static int getRandomPINNumber(){
Random randomGenerator = new Random();
int randomNo = randomGenerator.nextInt(10000);
if(randomNo < 1000)
getRandomPINNumber(); //repeat if it is less than 1000,some bug here
return randomNo;
}
public static void main(String[] args){
for(int i = 0;i<100; i++)
System.out.println(getRandomPINNumber());
}
}
Output
6413
1692
5734
105 <--- why 105 is getting printed, it should reevaluate the pin right?
4857
6348
1355
Aucun commentaire:
Enregistrer un commentaire