jeudi 29 octobre 2020

How to randomise the characters '+' and '-' in java

I'm coding an arithmetic game where the user is asked a series of addition questions. I want to however randomly assign an operator for each question so that the question could be either:

Question 1: num1 + num2 =

or

Question 2: num1 - num2 = 

I have been using the Math.random() method to randomise num1 and num2 the last thing I am struggling on is randomly generating '+' and '-'.

Is it something to do with the ASCII values of these two characters and then I can randomly pick between them? Thanks for the help!

As a side note, I want to ask the user to 'press enter' to start the game, but i'm not sure how to do it. Currently I've got the user to enter 'y' to start. Any ideas? Thanks so much.

//prompt user to start the game
            System.out.println();
            System.out.print("Press y to Start the Game: ");
            String start_program = keyboard.next();
    
            if (start_program.equals("y")) {

heres my code so far:

public static void main(String[] args) {
        //mental arithmetic game
        System.out.println("You will be presented with 8 addition questions.");
        System.out.println("After the first question, your answer to the previous question will be used\nas the first number in the next addition question.");

        //set up input scanner
        Scanner keyboard = new Scanner(System.in);


        //declare constant variables
        final int min_range = 1, max_range = 10, Max_Number_of_Questions = 8;

        long start_time, end_time;

        //generate 2 random numbers
        int random_number1 = (int) ((Math.random() * max_range) + min_range);
        int random_number2 = (int) ((Math.random() * max_range) + min_range);

        //declare variables
        int question_number = 1;
        int guess;


        //prompt user to start the game
        System.out.println();
        System.out.print("Press y to Start the Game: ");
        String start_program = keyboard.next();

        if (start_program.equals("y")) {


            //start timer
            start_time = System.currentTimeMillis();

            //ask the question
            System.out.print("Question " + question_number + ": What is " + random_number1 + " + " + random_number2 + "? ");
            //take in user input
            guess = keyboard.nextInt();


            while (guess == (random_number1 + random_number2) && question_number < Max_Number_of_Questions) {
                System.out.println("Correct");
                ++question_number;
                //generate a new question
                //generate 2 random numbers
                random_number1 = guess;
                random_number2 = (int) ((Math.random() * max_range) + min_range);
                //ask the question again
                System.out.print("Question " + question_number + ": What is " + random_number1 + " + " + random_number2 + "? ");
                //take in user input
                guess = keyboard.nextInt();
            }
            end_time = System.currentTimeMillis();
            int time_taken = (int) (end_time - start_time);

            if (guess != (random_number1 + random_number2))
                System.out.println("Wrong");
            else {
                System.out.println();
                System.out.println("Well Done! You answered all questions successfully in " + (time_taken / 1000) + " seconds.");
            }

        }
    }



Aucun commentaire:

Enregistrer un commentaire