0

I need to calculate the % of questions that the user got correct. The Double right is ho many questions that the user got correct. For some reason when the user gets the question correct it will not return the updated value of right to the main method. I have all of the math right and the program runs. The only response that I get out of the terminal is "you got 0% correct."

import java.util.Scanner;
import java.util.Random;

public class program {
    public static void main(String[] args) {

        System.out.println("How Many Questions Would You Like?");

        Scanner sc = new Scanner(System.in);
        Double num;
        num = sc.nextDouble();


        Double questions;

        Double right;

        right = 0.0;
        questions = 0.0;

        Double questionsasked;
        questionsasked = questions;

        while (questions < num) { // Compares Initial value of questions to num,is how many questions that the
            // user wants.
            // Ask User What Type Of Problem
            System.out.println("What Kind Of Problem Would You Like?");
            // 1 for Addition, 2 for Subtraction, 3 For Multiplication, 4 for Division
            System.out.println("1 for \"+\", 2 for \"-\", 3 for \"*\", 4 for \"/\"");
            // Takes Input Values For Problem Type
            Scanner sd = new Scanner(System.in);
            Double input;
            input = sc.nextDouble();
            // Register What Problem They Would Like
            // Input is what type of problem user wants
            if (input == 1) {
                // Addition Problem, Passes Number Of Questions Right, and other necessary
                // things
                // Adds Value To Number Of Questions Asked, Used To Find Percentage Correct
                questions = questions + 1.0;
                // Calls Method
                add(right, questions, questionsasked);
            } else if (input == 2) {
                // Subtraction Problem, Passes Number Of Questions Right, and other necessary
                // things
                // Adds Value To Number Of Questions Asked, Used To Find Percentage Correct
                questions = questions + 1.0;
                // Calls Method
                subtract(right, questions, questionsasked);

            } else if (input == 3) {
                // Multiply Problem, Passes Number Of Questions Right, and other necessary
                // things
                // Adds Value To Number Of Questions Asked, Used To Find Percentage Correct
                questions = questions + 1.0;
                // Calls Method
                multiply(right, questions, questionsasked);

            } else if (input == 4) {
                // Division Problem, Passes Number Of Questions Right, and other necessary
                // things
                // Adds Value To Number Of Questions Asked, Used To Find Percentage Correct
                questions = questions + 1.0;
                // Calls Method
                divide(right, questions, questionsasked);

            } else {
                // If Answer to what type of problem they want is not 1-4 it will print this.
                System.out.println("Not A Valid Problem.");
            }
        }
        Double calcper;
        calcper = right / questions;
        Double calcperfinal;
        calcperfinal = calcper * 100;
        System.out.println("You Got " + calcperfinal + "% Correct");

        System.out.println("Program Is Done Running!"); // This Is The Very Last Thing That Happens.
    }

    // Addition Method
    public static Double add(Double right, Double questions, Double questionsasked) {

        System.out.println("OK, Let's Add!");

        Random rand = new Random(); // This Declares The Random Number
        Double rnumber = rand.nextInt((int) 100.0) + 0.0; // 100 Is The Max, 0 Is The Min.

        Random rand2 = new Random(); // This Declares The Random Number
        Double rnumber2 = rand2.nextInt((int) 100.0) + 0.0; // 100 Is The Max, 0 Is The Min.

        System.out.println("What Is, " + rnumber + " + " + rnumber2 + "?"); // This Will Ask Question.

        Scanner sc = new Scanner(System.in);
        Double add; // This Is The User Answer
        add = sc.nextDouble(); // This Sets Current Scanner To The User Answer

        int answer = (int) (rnumber + rnumber2); // Creates INT To Determine The Right Answer

        if (add == answer) { // Compares User Answer To The Correct Computer Answer
            System.out.println("Correct!"); // Prints Out Correct
            right = right + 1.0;
            return right;
            // Used If user Got Question Incorrect
        } else {
            System.out.println("No, the correct answer is " + answer); // Prints The Correct Answer.
            return right;
        }
    }

    // Subtraction Method
    public static Double subtract(Double right, Double questions, Double questionsasked) {

        System.out.println("OK, Let's Subtract!");

        Random rand = new Random(); // This Declares The Random Number
        Double rnumber = (double) (rand.nextInt(100) + 0.0); // 100 Is The Max, 0 Is The Min.

        Random rand2 = new Random(); // This Declares The Random Number
        Double rnumber2 = (double) (rand2.nextInt(100) + 0.0); // 100 Is The Max, 0 Is The Min.

        System.out.println("What Is, " + rnumber + " - " + rnumber2 + "?"); // This Will Ask Question.

        Scanner sc = new Scanner(System.in);
        Double sub; // This Is The User Answer
        sub = sc.nextDouble(); // This Sets Current Scanner To The User Answer

        int answer = (int) (rnumber - rnumber2); // Creates INT To Determine The Right Answer

        if (sub == answer) { // Compares User Answer To The Correct Computer Answer
            System.out.println("Correct!"); // Prints Out Correct
            right = right + 1;
            return right;
            // Used If User Gets Question Incorrect
        } else {
            System.out.println("No, the correct answer is " + answer); // Prints The Correct Answer.
            if (questionsasked == 1.0 || questions == 1.0 && right == 0.0) {
                System.out.println("You Got %0 Right.");
            } else {
                return right;
            }
        }
        return questions; // Updates Value Of Questions Asked
    }

    // Multiplication Method
    public static Double multiply(Double right, Double questions, Double questionsasked) {

        System.out.println("OK, Let's Multiply!");

        Random rand = new Random(); // This Declares The Random Number
        Double rnumber = (double) (rand.nextInt(100) + 0); // 100 Is The Max, 0 Is The Min.

        Random rand2 = new Random(); // This Declares The Random Number
        Double rnumber2 = (double) (rand2.nextInt(100) + 0); // 100 Is The Max, 0 Is The Min.

        System.out.println("What Is, " + rnumber + " * " + rnumber2 + "?"); // This Will Ask Question.

        Scanner sc = new Scanner(System.in);
        Double mul; // This Is The User Answer
        mul = sc.nextDouble(); // This Sets Current Scanner To The User Answer

        int answer = (int) (rnumber * rnumber2); // Creates INT To Determine The Right Answer

        if (mul == answer) { // Compares User Answer To The Correct Computer Answer
            System.out.println("Correct!"); // Prints Out Correct
            right = right + 1.0;
            // Used If User Gets Question Incorrect
        } else {
            System.out.println("No, the correct answer is " + answer); // Prints The Correct Answer.
            return right;
        }
        return questions; // Updates Value Of Questions Asked
    }

    // Division Method
    public static Double divide(Double right, Double questions, Double questionsasked) {

        System.out.println("OK, Let's Divide!");

        Random rand = new Random(); // This Declares The Random Number
        Double rnumber = (double) (rand.nextInt(100) + 0.0); // 100 Is The Max, 0 Is The Min.

        Random rand2 = new Random(); // This Declares The Random Number
        Double rnumber2 = (double) rand2.nextInt(100) + 0.0; // 100 Is The Max, 0 Is The Min.

        System.out.println("What Is, " + rnumber + " / " + rnumber2 + "?" + ", Round To The Nearest 100th Place."); // This

        // Will

        // Ask



        Scanner sc = new Scanner(System.in);
        Double div; // This Is The User Answer
        div = sc.nextDouble(); // This Sets Current Scanner To The User

        double answer = (rnumber / rnumber2); // Creates Double To Determine
        double answerrounded = Math.round(answer * 100.0) / 100.0; // Creates

        if (div == answerrounded) { // Compares User Answer To The Correct
            System.out.println("Correct!"); // Prints Out Correct
            right = right + 1.0;
            return right;
            // used If User Gets Question Incorrect
        } else {
            System.out.println("No, the correct answer is " + answerrounded);

        }
        return questions; // Updates Value Of Questions Asked
    }
}
xingbin
  • 27,410
  • 9
  • 53
  • 103
Larson Carter
  • 153
  • 2
  • 11
  • 2
    Your code is very bad indented, so unreadable,also theris a lot a useledd comment, the only thing I see is that you declare differents Random instance in same method, you can use once, also use double instead of Double if not required – azro Apr 23 '18 at 13:09
  • 2
    Also, please [edit] your question and inclue a [MCVE] (with emphasis on **minimal**). – Turing85 Apr 23 '18 at 13:10
  • 2
    A small logic question. First you ask how many questions the user wants and you save it as a `Double`. Can you ask me 4.583 questions?? Same for `input`. A little hint, don't save it as `Double` when you don't need a floating number – Morchul Apr 23 '18 at 13:21

3 Answers3

1

You forgot update right after calculating, change it to this:

right = add(right, questions, questionsasked);
...
right = subtract(right, questions, questionsasked);
...
right = multiply(right, questions, questionsasked);
...
right = divide(right, questions, questionsasked);

Also, sometimes, too much redundant comments in the code will make it harder for others(or yourself) to read or understand it :) Just keep the important comment, try use the method name or variable name show your logic.

xingbin
  • 27,410
  • 9
  • 53
  • 103
  • @LarsonCarter You already got `add(right, questions, questionsasked);` in your code, what you need to do is add `right = ` on the left. Otherwise the `right` will not be updated and it will always be zero. – xingbin Apr 23 '18 at 13:20
1

Java is pass-by-value and not pass-by-reference as explained in this answer. In your methods that attempt to modify right variable e.g.

public static Double add(Double right, Double questions, Double questionsasked)

changing right argument within the method body will have no side effect on the "globally" maintained right variable used to calculate the total number of correct answers.

You must assign the value back and you are already correctly returning it from the method:

right = add(right, questions, questionsasked);
Morchul
  • 1,987
  • 1
  • 7
  • 21
Karol Dowbecki
  • 43,645
  • 9
  • 78
  • 111
0

First of all it's a very bad idea to check a double as condition of a while loop, because of representation of decimal numbers in the memory (double; float). You shouldn't compare them directly, instead use deltas or integer...

while (questions < num) // bad practice

Second, can a user get one and a half question or why do you use a double to store that value?

Double num;
num = sc.nextDouble();

Third, to check for option input use a switch statement... In my opinion it's a lot more readable

switch (input)
{
    case 1:
    {
        ...
        break;
    }
    ...
}

Moreover instead of writing

questions = questions + 1.0;

increment the value by writing

++questions;

But your real problem is, that Java pass arguments by value and nor by reference as already said change your statement to something like that to finally assign the value...

 right = add(right, questions, questionsasked);
0x1C1B
  • 1,204
  • 11
  • 40