本文介绍了帮助百分比输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找百分比时遇到问题.当显示数学问题的输出时,我需要:#完成,正确,错误和正确百分比.除了百分比以外,其他所有内容都正确无误.如果所有问题都正确,则表明100%的其他值均为0%.我在做什么错了.


cout<< setw(17)<< 添加:"<< setw(16)<< TotalAddProb<< setw(17)<< AddCountRight<< setw(11)<< AddCountWrong<< setw(11)<< setprecision(2)<<固定<< (((AddCountRight)/(TotalAddProb)* 100)<< "\ n";

I am having a problem with finding percentage. When displaying output from math problems i need: # complete, Right, Wrong, and percentage right. All but percentage comes out correct. If all problems are correct it shows 100% anything else is 0%. What am I doing wrong.


cout << setw(17) << "Addition: " << setw(16) << TotalAddProb << setw(17) << AddCountRight << setw(11) << AddCountWrong << setw(11) << setprecision(2) << fixed << ((AddCountRight) / (TotalAddProb) *100) << "\n";

推荐答案

((AddCountRight) / (TotalAddProb) *100)


为此:


To this:

(AddCountRight * 100.0 / TotalAddProb)


在您的版本中,您正在执行整数除法,因此结果将被截断为0,然后再乘以100(结果为0).在我的版本中,我将整数转换为浮点数,然后执行其余的数学运算(这避免了整数除法).


In your version, you are performing integer division, so the result gets truncated to 0 then multiplied by 100 (the result of which is 0). In my version, I convert the integer to a floating point number, then perform the rest of the math (which avoids the integer division).


这篇关于帮助百分比输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-23 08:50