Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

Random java operators


ghost's Avatar
0 0

I need to know how to make a random java operator(+,-,*,/) does anyone know how i should go about doing this, would much appricaite it.


ghost's Avatar
0 0

make random operators or produce them for displaying? in the 2nd case…try this code

public class ops { import java.util.*; public static char returnOp() { Random r = new Random(); char ops = {'!','@','#','$'}; //etc….. return ops[r.nextInt(ops.length)]; } }

i didnt compile that or anything, so you may have mod it a bit…but its a PoC


ghost's Avatar
0 0

I always thought you'd keep the import statements at the top of the file…


ghost's Avatar
0 0

shit….yeah. my bad. xD


ghost's Avatar
0 0

Pretty much the same shit samurai has, thisis the way I do it..

  public static String generateOp()
     {
          Random random = new Random();
           char[] letters = "*/+-".toCharArray();
            return letters[random.nextInt(3)].toString();
      }

ghost's Avatar
0 0

Dunno why I have it returning a string, should of been a char, stole that from an old string generator method I have :-\