guest_20 guest_20 - 1 month ago 10
Java Question

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();

}
}
}
}

Answer

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!");
        }
    }

}