0

Can anyone show me how i can get system to print a random string plz?

so basicly if input "hello" system output will be random between (hey, hi, hello)? thanks!!😀😀

11th Jun 2017, 10:26 PM
D_Stark
D_Stark - avatar
5 Réponses
+ 6
(I'm assuming Java bc Scanner is in the tags, so correct me if I'm wrong) You can use Math.Random() like this: int rand = (int) (Math.Random * 3) + 1; //Generates between 1 and 3 if (rand == 1) System.out.println("Hey!"); else if (rand == 2) System.out.println("Hi!"); else System.out.println("Hello!");
11th Jun 2017, 10:42 PM
Tamra
Tamra - avatar
+ 1
String str[] = {"hi", "hey", "hello"}; Random random = new Random(); int r = random.nextInt(str.length); String random_str = str[r]; System.out.prntln(random_str);
11th Jun 2017, 10:55 PM
Da' BO$
Da' BO$ - avatar
0
In what language?
11th Jun 2017, 10:37 PM
Igor B
Igor B - avatar
0
in basic cmd (batch file) @echo off SET /a _rand=(%RANDOM%*3/32768)+1 CALL :CASE_%_rand% :case_0 GOTO END_CASE :case_1 echo hi GOTO END_CASE :case_2 echo hey GOTO END_CASE :case_3 echo hello GOTO END_CASE :END_CASE GOTO :EOF you can type cls before @echo in a new line to clear the cmd and make it clean
11th Jun 2017, 11:14 PM
soulbog
soulbog - avatar
0
thanks guys! yes it is java im using ill make sure i tag it propley next time 😀. do i need to import anything to use random? thanks
12th Jun 2017, 7:52 AM
D_Stark
D_Stark - avatar