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: Java program to generate random questions

  1. #1
    Junior Member
    Join Date
    Jun 2009
    Posts
    6
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Question Java program to generate random questions

    Little intro:
    hi i m new to java and learning it very slowly. Now i have to code for a program.

    Problem:
    firstly the program is to generate random questions like:
    7 * 8 = ?
    17 * 9 = ?

    i will explain the program. the first integer is a range between 1-20 that the user can choose any range within this like "1-5" or "12-19" any range but with in 1-20 and we must choose the first integer within the choosen range. and the second integer in the output is a random number between "1-10". and with this two variables are set and then we have to generate any five random questions.

    example: if the user selects a range like "15-19"

    then the questions may be:

    16 * 7 = ?
    15 * 2 = ?
    18 * 10 = ?
    16 * 9 = ?
    17 * 5 = ?


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: A program i am conquering with

    Hello java. Welcome to the forums

    I have wrote this example for you to look at.
    There is no error handling to check what the user inputs but the functionality is there.

    import java.util.Scanner;
     
    public class JavaQuestions {
     
        /**
         * JavaProgrammingForums.com
         */
     
        // Global variables
        static String[] rangeArray = null;
        static int startRange, endRange;
     
        public void userInput(){
     
            // Scanner class to take user input
            Scanner sc = new Scanner(System.in);
            System.out.println("Enter a range between 1 and 20. eg 5-10: ");
            String firstRange = sc.nextLine();
     
            // Split the input String
            rangeArray = firstRange.split("-");
     
            // Assign range values
            startRange = Integer.parseInt(rangeArray[0]);
            endRange = Integer.parseInt(rangeArray[1]);
     
            // doMaths method
            doMaths();        
        }
     
        public void doMaths(){
     
            System.out.println("Questions:");
     
            // Loop 5 times and print
            for(int i = 0; i < 5; i++){
     
                // Generate random number between startRange and endRange
                int randomPt1 = (int)(Math.random() * (endRange - startRange + 1) ) + startRange;
     
                // Generate random number between 1 and 10
                int randomPt2 = (int)(Math.random() * (10 - 1 + 1) ) + 1;
     
                // Print math questions
                System.out.println(randomPt1 + " * " + randomPt2 + " = ?");        
            }                
        }
     
     
        public static void main(String[] args) {
     
            JavaQuestions j = new JavaQuestions();
            j.userInput();
     
        }
    }

    Example output:

    Enter a range between 1 and 20. eg 5-10:
    1-20
    Questions:
    11 * 5 = ?
    13 * 5 = ?
    8 * 5 = ?
    11 * 8 = ?
    20 * 1 = ?
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

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

    java (June 23rd, 2009)