Java arithmetics for calculating a percentage

2020-04-16 02:58发布

问题:

I have a little problem in my java application.

I have to calculate the score they have when they finish, I use this method:

public Float ScoreProcent(int questions, int correct){
    Float x = new Float(questions);
    Float y = new Float(correct);

    Float num = (float) (100 / questions * correct);
    return num;
}

However, when I have 38 questions and 38 are correct it displays 76.

回答1:

Firstly, you shouldn't be using Float all over the place - you want float; there's no need to be boxing here.

Secondly, you're not using x and y at all.

Thirdly, I'd say the way you're expressing the equation is at the least confusing. It's possible that just changing to x and y would be fine, but I wouldn't - I'd change the whole way you're expressing yourself.

Fourthly, you're violating Java naming conventions by writing a method name in PascalCase. You've also got a spelling mistake.

Fixing all of these, you'd end with with something like:

public static float getPercentageCorrect(int questions, int correct) {
    float proportionCorrect = ((float) correct) / ((float) questions);
    return proportionCorrect * 100;
}

I'd actually generalize this - it's not specific to "correct answers", so can be used for anything where it's some score out of a total:

/**
 * Returns a proportion (n out of a total) as a percentage, in a float.
 */
public static float getPercentage(int n, int total) {
    float proportion = ((float) n) / ((float) total);
    return proportion * 100;
}

As noted in comments, this could be written as:

float proportion = (float) n / total;

... but then you need to know the precedence rules to validate it. I've included both casts explicitly to make it clear that I want to convert each operand to float before the division.



回答2:

Try this.

float num = (correct * 100.0) / questions;


回答3:

Float num = (float) (100 / y * x);

The problem is that you weren't using the float you just built before.

Alternatively I suggest this :

public float getScorePercent(int questions, int correct){
    return 100f * correct / questions;
}

It's generally advised to avoid Float and use float, except when you really need objects (that is rarely).



回答4:

Try this:

num = (float) (100 * (float) correct / (float) questions);

or

num = (correct * 100.0) / questions;


回答5:

Use the following:

Float num = (float) ((100  * correct) / questions);


回答6:

Float num = (float) (100 / questions * correct); 

It is executed in following way:

Float num = (float) ((100 / questions) * correct); 

since type of questions and correct is int, values after decimal are not conisdered.

Now, 100 /questions = 100 / 38 = 2 and, num = (Float)(2 * 28) = 76.

That's what you are getting.



回答7:

I will try explain other way:

float hoursWorked = (float) 32.5;
final double HOURLY_RATE = 9.35;
double wages = HOURLY_RATE * hoursWorked;
final double TAX_RATE = 22; // percentage
double taxDue = TAX_RATE * wages / 100;
// to calculate using percentage need to divide by 100
System.out.print(taxDue);


回答8:

There are no unreasonable quesions.

You are calculating what fraction of correct are there among the questions. The mathematical way of saying this is:

  correct/questions

Now, since they can go onto decimals you need to use that Floats you mentioned in your code. So:

 (float)correct/(float)quesions  

That is how you cast something to float. To get the answer in percentage(in 100) it must be multiplied by 100 which is

  100 * (float)correct/(float)quesions 

And there you go :)

For proper way to get it coded listen to other answers.



标签: java math