lundi 20 avril 2015

Maths Game - returning points in methods

So my objective is to create a maths game where the user selects if he/she wants a maths question from a file or a random generate one consisting of the 4 maths elements in 3 difficulties.I have created a lot of methods... I have an idea where im going but now im stuck. I need to have it so it keeps a score of questions answered correctly. How do i return the points to the main method and have the game going until the user presses 3 on the gamePlay()method

public class MathsGameProject2 {

public static void main(String[] args) {

    int score =0;
    int points = 0;
    score = gamePlay(points);
    gamePlay();
}

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

    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(int points) {
    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(int points) {
    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(int points) {
    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) {
        points = points + 3;
    } 
}




Aucun commentaire:

Enregistrer un commentaire