guest_20
guest_20

Reputation: 13

Creating Methods that will produce the same result

I'm in a Beginner Java class and I'm confused about using additional methods and using them in another. I think that I have most of my assignment done but I just need help to create the methods. One is to generate the question and the other one is to display the message.

I know that in order to call a method

public static test(num1,num2,num3)

but in my code, how do I make it so that I call the method and still make it loop correctly?

In the assignment instructions that was given to me, In order to do that, I have to write a method named

public static in generateQuestion() 

and

public static void displayMessage(boolean isCorrect)

This is my code:

//For Random Generator
import java.util.Random;
//Uses a class Scanner
import java.util.Scanner;
public class Assign6 
{
    public static void main(String[] args)
    {
        //Scanner to Obtain Input from CW
        Scanner input = new Scanner(System.in);
        //Generate Random Number for Quiz
        Random randomNumbers = new Random();
        int number1 = 0;
        int number2 = 0;
        int answer = 0;
        //Rolls number from 1-9
        number1 = randomNumbers.nextInt(9);
        number2 = randomNumbers.nextInt(9);
        //Question prompt
        System.out.println("How much is " +number1+ " times " +number2+ "? ");
        answer = input.nextInt();
        //If Else While Statements
        if(answer == (number1*number2))
        {
            System.out.println("Good job! You got it right!");
        }
        else
        {
            while (answer !=(number1*number2))
            {
                System.out.println("You got it wrong, try again!");
                answer = input.nextInt();

            }
        }
    }   
}

Upvotes: 1

Views: 116

Answers (3)

Neo
Neo

Reputation: 3786

If I got it right, I have a solution that might be a little stupid but will work for your assignment.
If you make generateQuestion that makes two random ints, prints the question and returns their multiple (answer).
And displayMessgae that prints "Good job! You got it right!" if isCorrect is true and "You got it wrong, try again!" else,

you can call generateQuestion, then get an answer (in main), and loop until answer is correct (according to return value of generateQuestion).

Every time you get a new answer (in loop), call displayMessgae(false).
After the loop ended call displayMessgae(true)

This is my working code for this:

//For Random Generator
import java.util.Random;
//Uses a class Scanner
import java.util.Scanner;
public class Assign6 
{
    public static int generateQuestion()
    {
        Random r = new Random();
        int x = r.nextInt(9), y = x = r.nextInt(9);
        System.out.println("How much is " + x + " times " + y + "? ");
        return x * y;
    }
    public static void displayMessage(boolean isCorrect)
    {
        if (isCorrect)
            System.out.println("Good job! You got it right!");
        else
            System.out.println("You got it wrong, try again!");
    }
    public static void main(String[] args)
    {
        //Scanner to Obtain Input from CW
        Scanner input = new Scanner(System.in);
        int rightAnswer = 0;
        rightAnswer = generateQuestion();
        while (input.nextInt() != rightAnswer)
            displayMessage(false);
        displayMessage(true);

    }   
}

Upvotes: 1

Iofacture
Iofacture

Reputation: 685

If I understand your question correctly, It's simply a matter of separating the functionality that prints a question and displays the answer into separate methods. See my edited version of your code below

public class Assign6
{
    public static void main(String[] args)
    {
        // Scanner to Obtain Input from CW
        Scanner input = new Scanner(System.in);
        // Generate Random Number for Quiz
        Random randomNumbers = new Random();
        int number1 = 0;
        int number2 = 0;
        int answer = 0;
        // Rolls number from 1-9
        number1 = randomNumbers.nextInt(9);
        number2 = randomNumbers.nextInt(9);

        displayQuestion("How much is " + number1 + " times " + number2 + "?");

        answer = input.nextInt();
        // If Else While Statements
        if (answer == (number1 * number2))
        {
            displayMessage(Boolean.TRUE);
        }
        else
        {
            while (answer != (number1 * number2))
            {
                displayMessage(Boolean.FALSE);

                answer = input.nextInt();

            }
        }
    }

    public static void displayQuestion(String q)
    {
        System.out.println(q);
    }

    public static void displayMessage(Boolean isCorrect)
    {
        if (isCorrect)
        {
            System.out.println("Good job! You got it right!");
        }
        else
        {
            System.out.println("You got it wrong, try again!");
        }
    }

}

Upvotes: 0

jthort
jthort

Reputation: 409

You are going to have two methods

public static void generateQuestion() 

Which is going to hold the code to generate the random values and output it. It will return void because all it's doing is printing out.

Next you will have

public static void displayMessage(boolean isCorrect)

which will be called if if(answer == (number1*number2)) is true with true as the parameter. Otherwise it will still be called, but the parameter passed in will be false. This method will determine if isCorrect is true or false, and output the appropriate message.

Upvotes: 1

Related Questions