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!!😀😀
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!");
+ 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);
0
In what language?
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
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