I have run into a little problem. I have a homework exercise where i need to 'randomly' select a string from a string array. The goal of the exercise is to make a code which choses a random (inserted) name. My code:
public void run() {
int userSelection = -1;
int userAmount = 0;
String[] users = new String[userAmount];
int[] amountChosen = new int[userAmount];
while (userSelection != 0) {
drawMenu();
System.out.println();
//user selecting the menu choice
System.out.print("Make a selection from the menu: ");
userSelection = userInput();
System.out.println();
//forcing the user to give one of the allowed values
while (userSelection < 0 || userSelection > 4) {
System.out.print("That is invalid input. try again: ");
userSelection = userInput();
}
//adding users
if (userSelection == 1) {
System.out.print("How many users do we have?");
userAmount = userInput();
users = new String[userAmount];
amountChosen = new int[userAmount];
addUsers(users, userAmount); //returns user array with names
System.out.println();
}
//selecting random user
else if (userSelection == 2) {
int playerSelect = (int) (Math.random()*userAmount);
amountChosen[playerSelect]++;
System.out.println(users[playerSelect] + " was chosen!");
System.out.println();
}
//display the amount the users were chosen
else{
System.out.println("******** Turns ********");
for (int i = 0; i < userAmount; i++){
System.out.println("* " + "[" + amountChosen[i] + "] " + users[i]);
}
System.out.println("***********************");
System.out.println();
}
}
}
As you can see i now have a totally random userselection. For keeping tabs on how often a player is chosen i already made the 'int[] amountChosen' array. The goal is to "select a random player, also make it chose the player that is chosen the fewest times" so basicly it needs to select the string with the lowest amountChosen. (Also: I am aware my code may be a little bit messy and weird in some places. I just started learning java) Thank you for response!