mardi 14 janvier 2020

changing String value based upon String[] index

i know how to do this

public class AccountPicker {

public static void main(String[] args) throws InterruptedException {

    String password = "";
    String[] accounts = {"a", "b", "c"};

    Random r = new Random();
    int assigned = r.nextInt(3);

    if (assigned == 2) {
        password = "password";
        System.out.println("account: " + accounts[assigned] + " " + "password: " + password);
    }

    if (assigned == 1) {
        password = "Password";
        System.out.println("account: " + accounts[assigned] + " " + "password: " + password);
    }

    if (assigned == 0) {
        password = "PASSWORD";
        System.out.println("account: " + accounts[assigned] + " " + "password: " + password);
    }


}

}

basically what this is doing is choosing a account a, b , or c and changing password based upon which index was picked out of accounts

but what if i have 100 values in accounts?

making 100 if statements would take up too much code..

is there a more efficient way of doing this?




Aucun commentaire:

Enregistrer un commentaire