Java不返回乘法双

时间:2018-04-23 13:08:23

标签: java

我需要计算用户得到的问题的百分比。 Double权利是很多用户纠正的问题。出于某种原因,当用户得到正确的问题时,它不会将更新的权利值返回给main方法。我有所有的数学权利,程序运行。我从终端获得的唯一回应是#34;你得到0%正确。"

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
    }
}

3 个答案:

答案 0 :(得分:1)

您在计算后忘记更新right,将其更改为:

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

另外,有时候,代码中过多的冗余注释会让其他人(或你自己)更难阅读或理解它:)只需保留重要注释,尝试使用方法名称或变量名称显示你的逻辑。 / p>

答案 1 :(得分:1)

Java是按值传递而不是按照in this answer解释的传递方式。在尝试修改right变量的方法中,例如

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

更改方法正文中的right参数不会对用于计算正确答案总数的“全局”维护right变量产生任何副作用。

您必须重新分配值,并且您已经从方法中正确地返回了该值:

right = add(right, questions, questionsasked);

答案 2 :(得分:0)

首先,检查double作为while循环的条件是一个非常糟糕的主意,因为在内存中表示十进制数(double; float)。你不应该直接比较它们,而是使用增量或整数......

while (questions < num) // bad practice

其次,用户可以得到一个半问题,或者为什么使用双倍来存储该值?

Double num;
num = sc.nextDouble();

第三,检查选项输入使用switch语句......在我看来,它更具可读性

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

而且不是写作

questions = questions + 1.0;

通过编写

来增加值
++questions;

但你真正的问题是,Java通过值传递参数,也没有通过引用传递参数已经说过将你的语句更改为类似的东西,最终赋值... ...

 right = add(right, questions, questionsasked);