数学游戏 - 方法中的返回点

时间:2015-04-20 19:40:02

标签: java random methods

所以我的目标是创建一个数学游戏,用户选择是否需要来自文件的数学问题,或者随机生成一个由3个困难中的4个数学元素组成的数学游戏。我创建了很多方法。 ..我有一个想法,即时通讯,但现在我卡住了。我需要它才能正确回答问题。如何将点返回到main方法并让游戏继续,直到用户在gamePlay()方法上按3     公共课MathsGameProject2 {

public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);

    int score;
    int points = 0;
    int questionType;

    System.out.print("Please enter the what type of question you want" + "\n 1 Question from a file" + "\n 2 Random question" + "\n 3 Quit game\n");
    questionType = keyboard.nextInt();

    while (questionType != 3) {
        if (questionType == 1) {
            questionFromFile();
        } else if (questionType == 2) {
            randomQuestion();
        } else {
            System.out.println("Please enter the what type of question you want" + "\n 1 Question from a file" + "\n 2 Random question" + "\n 3 Quit game\n");
        }
    }

}
public static questionFromFile() {

}

public static randomQuestion() {
    Scanner keyboard = new Scanner(System.in);

    int difficulty;
    System.out.println("Please enter the difficulty you want to play." + "\n 1. Easy" + "\n 2. Medium" + "\n 3. Hard\n");
    difficulty = keyboard.nextInt();

    if (difficulty == 1) {
        easy();
    } else if (difficulty == 2) {
        medium();
    } else if (difficulty == 3) {
        hard();
    } else {
        System.out.println("Please enter a number between 1-3\n");
    }
}

public static easy() {
    Scanner keyboard = new Scanner(System.in);

    int mathElement;
    System.out.print("What element of maths do you want?" + "\n1 Additon" + "\n2 Subtraction" + "\n3 Multiplication" + "\n4 Division\n");
    mathElement = keyboard.nextInt();

    if (mathElement == 1) {
        easyAdd();
    } else if (mathElement == 2) {
        easySub();
    } else if (mathElement == 3) {
        easyMulti();
    } else if (mathElement == 4) {
        easyDiv();
    } else {
        System.out.println("Please enter a number between 1-4\n");
    }
}

public static easyAdd() {
    Scanner keyboard = new Scanner(System.in);

    Random rand = new Random();

    int num = rand.nextInt(10) + 1;
    int num2 = rand.nextInt(10) + 1;

    int correct = num + num2;
    int answer;

    System.out.print("What is the answer of " + num + " + " + num2 + " ?");
    answer = keyboard.nextInt();

    if (answer == correct) {
    } 
}

1 个答案:

答案 0 :(得分:0)

为了跟踪用户成功回答的问题数量,您需要:

  1. 对于每个问题,请返回用户是否正确回答
  2. 有一个计数器,只要用户正确回答问题,该计数器就会递增
  3. 可选地,有一个计数器,只要问题回答错误就会递增
  4. 对于#1,您可以使用布尔返回值来指定问题是否已成功回答。

    return (answer == correct);
    

    您需要将该返回值一直传播到main()方法。

    static void main() {
        ....
        boolean isCorrect = randomQuestion();
        ....
    }
    
    static boolean randomQuestion() {
        ....
        return easy();
        ....
    }
    
    static boolean easy() {
        ....
        return easyAdd();
        ....
    }
    
    static boolean easyAdd() {
        ...
        return (answer == correct);
    }
    

    然后对于#2和#3,您可以根据main

    返回的值递增randomQuestion()中定义的计数器
    int numberCorrect = 0;
    int numberWrong = 0;
    ....
    boolean isCorrect = randomQuestion();
    if (isCorrect) {
        numberCorrect++;
    } else {
        numberIncorrect++;
    }
    

    另外(没有双关语),你可以使用while循环来连续接收用户输入,直到你得到你的退出代码,在这种情况下是3。一种方法是使用{{} 1}}当用户输入3时循环并中断。

    while(true)

    最后,在循环播放后,您只需打印出while (true) { /* Get user input */ .... if (questionType == 3) { break; } } numberCorrect计数器的值即可。

    希望这有帮助。