创建将产生相同结果的方法

时间:2016-10-25 18:41:31

标签: java methods

我在初学者Java课程中,我对使用其他方法并在另一个方法中使用它们感到困惑。我认为我完成了大部分工作,但我只需要帮助来创建方法。一个是生成问题,另一个是显示消息。

我知道为了调用方法

public static test(num1,num2,num3)

但是在我的代码中,我如何制作它以便我调用该方法并仍然使其正确循环?

在给我的赋值说明中,为了做到这一点,我必须编写一个名为

的方法
public static in generateQuestion() 

public static void displayMessage(boolean isCorrect)

这是我的代码:

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

            }
        }
    }   
}

3 个答案:

答案 0 :(得分:1)

你将有两种方法

public static void generateQuestion() 

将保留代码以生成随机值并输出它。它将返回void,因为它所做的一切都在打印出来。

接下来你将有

public static void displayMessage(boolean isCorrect)
如果if(answer == (number1*number2))为真,则将调用

,其中true为参数。否则它仍将被调用,但传入的参数将为false。此方法将确定isCorrect是true还是false,并输出相应的消息。

答案 1 :(得分:1)

如果我做对了,我的解决方案可能有点愚蠢,但适用于你的任务 如果你使generateQuestion产生两个随机的整数,则打印问题并返回它们的多个(答案) 而displayMessgae打印"干得好!你做对了!"如果isCorrecttrue和"你弄错了,请再试一次!"否则,

你可以调用generateQuestion,然后得到答案(在main中),然后循环直到答案正确(根据generateQuestion的返回值)。

每次获得新答案(循环播放)时,请致电displayMessgae(false) 循环结束后调用displayMessgae(true)

这是我的工作代码:

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

    }   
}

答案 2 :(得分:0)

如果我正确理解您的问题,只需将打印问题的功能分开并将答案显示在不同的方法中即可。请参阅我在下面编辑的代码版本

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

}