+ 1

Is there a way to create a code that can choose a random case?

1st Dec 2017, 7:10 AM
jalen
jalen - avatar
6 odpowiedzi
+ 1
give more information. of course you can generate random numbers and randomly choose one for example.
1st Dec 2017, 8:10 AM
Lucien
Lucien - avatar
+ 1
like in this code I have numerous cases where the user has to input something such as '2' or '3' or '4'. I was wondering if it was possible to create a code that can chooses randomly which case to go through and follow what ever is in that case? https://code.sololearn.com/c5BL4rb1UACd/?ref=app
1st Dec 2017, 8:17 AM
jalen
jalen - avatar
0
i gave him one example. now it's his turn to explain what this program should be about. there are not only ways to generate random integers, also strings and others as you hopefully know.
1st Dec 2017, 8:21 AM
Lucien
Lucien - avatar
0
I was thinking of doing something along the lines of this (obviously not finished) I just dont know how to get the code to understand that 2, 3, and 4 are cases not a word or string. case "random case": Random random = new Random(); String [] cases = {"2", "3", "4"}; int gg = random.nextInt(cases.length); String randomcase = cases[gg]; System.out.println(randomcase);
1st Dec 2017, 8:39 AM
jalen
jalen - avatar
0
Use switch, break and default
1st Dec 2017, 11:17 AM
Lucien
Lucien - avatar
0
instead of this or do I add on to it?
2nd Dec 2017, 11:24 PM
jalen
jalen - avatar