I have an array list of 4 items, i need to remove one item randomly and display the updated array list. However my random keeps targeting the second and third element on the array list. as far as i understand my random would count like this. 0 1 2 3 , shouldnt that be enough to cover my 4 elements, why does it keep targeting the same indexes? I have tried increasing the random number (4) + 1, but that puts me out of bounds.
Random rand = new Random();
Scanner input = new Scanner(System.in);
int numberOfGuests = 4;
ArrayList<String> guestList = new ArrayList<>(4);
System.out.println("Enter 4 guests:");
for(int i = 1; i <=numberOfGuests; i++){
System.out.printf("guest%d: ", i);
guestList.add(input.nextLine());
}
System.out.println("Guest List: " + guestList);
String remove = guestList.remove(rand.nextInt(4));
System.out.printf("%s can't come%n" , remove);
System.out.println("Guest List: " + guestList);
Aucun commentaire:
Enregistrer un commentaire