java - Random integer from an array -


i'm new java , i'm practicing skills using multiple classes , using system.out.println.

i'm trying make program in have 'conversation' computer. i'd try , instead of computer having same age every time console run, use array list load of random ages, randomly selected. in computer class i've got:

    static int [] compage = {19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31}; 

and in main conversation class, i've got:

int uage = input.nextint(); // (uage user age) system.out.println("that's cool! you're " +uage+ ". i'm " + (computers age here) + " myself. live?"); 

i've read around bit , found code such as

compage[new random().nextint(compage.length)] 

but in knowledge of arrays , using random function (i have imported it) limited , i'm not sure go.

any massively appreciated. all.

compage[new random().nextint(compage.length() )] 

the new random().nextint() generates random positive number. if use compage.length(), set max value (exclusively, not picked ever).

that way, random age everytime start program.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -