Results 1 to 5 of 5
  1. #1
    cachi is offline Member
    Join Date
    Jul 2007
    Posts
    40
    Rep Power
    0

    Default Generates random maths questions

    Hi, I am writing a program that generates random maths questions for primary schools, but I'm having a problem finding out how to select one of the following characters at random (+ - * /).
    Please can you help?

    Thanks.

  2. #2
    nitinborge5 is offline Member
    Join Date
    Jul 2007
    Posts
    10
    Rep Power
    0

    Default

    hi cachi
    do one thing convert characters in string[]

    and from that u can get your charactor randomly

    with help of equals method u can get apropriate operator

    ok

  3. #3
    brianhks is offline Senior Member
    Join Date
    Jul 2007
    Posts
    135
    Rep Power
    0

    Default

    Do what was previously stated and use Random.nextInt(4). It will return you either 0,1,2 or 3. You can use that to get one of the symbols from the array.

  4. #4
    yiweiang is offline Member
    Join Date
    Aug 2007
    Posts
    15
    Rep Power
    0

    Default

    yes, so to further explain that, it means that
    if (x=0)
    {
    selectedSymbol=plus;
    }

    ...etc

  5. #5
    brianhks is offline Senior Member
    Join Date
    Jul 2007
    Posts
    135
    Rep Power
    0

    Default

    Don't use an if statement at all.
    Java Code:
    String[] operators = {"+", "-", "*", "/"};
    
    Random rand = new Random();
    
    String op = operators[rand.nextInt(4)];
    Just loop that last line as you create your math problems.

Similar Threads

  1. random numbers without random class`
    By carlos123 in forum New To Java
    Replies: 4
    Last Post: 01-17-2008, 10:44 PM
  2. Just a Few Questions
    By pringle in forum New To Java
    Replies: 21
    Last Post: 01-09-2008, 06:21 PM
  3. Replies: 15
    Last Post: 11-30-2007, 05:19 PM
  4. questions
    By Gilgamesh in forum New To Java
    Replies: 3
    Last Post: 11-27-2007, 11:18 PM
  5. 3 Questions
    By hiranya in forum AWT / Swing
    Replies: 4
    Last Post: 11-14-2007, 04:57 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •