Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Random math operators and asking ten questions with a loop?

  1. #1
    Junior Member
    Join Date
    May 2014
    Location
    Java, TX
    Posts
    18
    My Mood
    Where
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Random math operators and asking ten questions with a loop?

    So my little math game is working so far. I can only do addition though. I know how to fix it if I can find out how to generate random math operators. After that I want to use a loop (or a better technique) to ask ten different math questions before the game is over. If anyone could give me a hand as to how to do this it would be appreciated.


    package pkgnew;
     
    import java.util.Scanner;
    import java.util.Random;
     
    public class New {
     
        public static void main(String args[]) {
     
     
            //Declare and construct variables 
            Random randomnum = new Random();
            int fnum, snum, answer;
            int mathnumber = randomnum.nextInt(20);
            int newnumber = randomnum.nextInt(20);
     
            //Declare and construct variables of math problem
            fnum = mathnumber;
            snum = newnumber;
            answer = fnum + snum;
     
     
            //Output of math problem
            System.out.print(fnum);
            System.out.print("+");
            System.out.println(snum);
     
            Scanner serena = new Scanner(System.in);
            int userAnswer = serena.nextInt();
     
            //If user input = answer display "correct"
            if (userAnswer == answer) {
                System.out.println("Correct!");
     
            //If user input does not = answer display "wrong" and correct answer
            } else {
                System.out.print("Wrong!");
            }
     
        }
    }


  2. #2
    Junior Member
    Join Date
    Jan 2014
    Posts
    13
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Random math operators and asking ten questions with a loop?

    How do you want your little game to run?
    I mean, you want to ask ten random math questions before the game is over. Do you want to keep track of how many the user gets correct and then report that score to the user after the ten questions?

    You could put everything in a for/while loop and make it run 10 times (10 questions). And inside this loop keep track of how many times the user gets the math question correct with a variable, and then print out that score to the user outside your main game loop.


    Also, if you want to print out a random math operator along with random operands (numbers), you can put your operators into an array of Strings and then print out a random index of that array.

    Example:

     
    //Declare and construct variables 
            Random randomnum = new Random();
            int fnum, snum, answer;
            int mathnumber = randomnum.nextInt(20);
            int newnumber = randomnum.nextInt(20);
     
    //Declare the operator
            String[] operators = {"+", "-" , "/" , "*" };
            int randomIndex = randomnum.nextInt(3);
            String selectedOperator = operators.substring(i, i+1);
     
            //Declare and construct variables of math problem
            fnum = mathnumber;
            snum = newnumber;
     
            if(selectedOperator.equals("+")
                 answer = fnum + snum;
             else if(selectedOperator.equals("-")
                 answer = fnum - snum;
              else if(selectedOperator.equals("/")
                 answer = fnum / snum;
              else if(selectedOperator.equals("*")
                 answer = fnum * snum;
     
     
     
            //Output of math problem
            System.out.print(fnum);
            System.out.print(selectedOperator);
            System.out.println(snum);

  3. The Following User Says Thank You to Midgar77 For This Useful Post:

    kunimaro15689 (May 18th, 2014)

Similar Threads

  1. [SOLVED] Counting and Math.random
    By Elyril in forum What's Wrong With My Code?
    Replies: 30
    Last Post: March 9th, 2014, 08:12 PM
  2. analysis of math questions
    By Dingwana in forum Java Theory & Questions
    Replies: 7
    Last Post: March 25th, 2013, 12:37 PM
  3. Java Math.random() and loop
    By maple1100 in forum What's Wrong With My Code?
    Replies: 23
    Last Post: December 28th, 2012, 12:23 PM
  4. Problems with Math.Random() in a for loop
    By csharp100 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 27th, 2012, 06:18 PM
  5. Math.Random()
    By xionyus in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 26th, 2011, 10:22 PM