C#中的百分比问题 [英] Percentage Problem In C#
本文介绍了C#中的百分比问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好!
我正在C#中创建标记百分比程序,但我仍然有百分比(0,0,0,0,0,0). 请告诉我其中的错误.
Hello Everyone!
I am Creating Marks Percentage Program In C#,but i still Got Percentage(0,0,0,0,0,0). Please Tell Me Error In It.
static void Main(string[] args)
{
int[] number = new int[6];
float[] percentage = new float[6];
int x;
Console.WriteLine("Enter Number.");
for (x = 0; x < 6; x++)
{
Console.Write("Enter Subject {0} Marks.", x + 1);
number[x] = Convert.ToInt32(Console.ReadLine());
}
Console.WriteLine("Percentaeg of Marks Is.");
for (x = 0; x < 6; x++)
{
percentage[x] =(float)((number[x] / 100) * 100);
// Console.WriteLine(Math.Round(percentage[x]));
}
foreach (int per in percentage)
{
Console.WriteLine(per);
}
Console.ReadLine();
}
}
推荐答案
整数算术!
您的用户输入25个字.
Integer arithmetic!
Your user enters 25 say.
25 / 100 = 0
不是吗-这是整数运算,因此它不能使用分数.尝试更早地浮动:
No really - this is integer arithmetic, so it can''t work with fractions. Try changing to float earlier:
percentage[x] =(((float)number[x] / 100) * 100);
或先乘以:
Or multiply first:
percentage[x] =(float)((number[x] * 100) / 100);
或扔掉乘法和除法:
Or throw away the mutiplication and division:
percentage[x] =(float)number[x];
或者更好,在整个过程中使用浮点数:
Or better, use floats throughout:
float[] number = new float[6];
float[] percentage = new float[6];
int x;
Console.WriteLine("Enter Number.");
for (x = 0; x < 6; x++)
{
Console.Write("Enter Subject {0} Marks.", x + 1);
number[x] = Convert.ToFloat(Console.ReadLine());
}
Console.WriteLine("Percentaeg of Marks Is.");
for (x = 0; x < 6; x++)
{
percentage[x] =(number[x] / 100) * 100;
,则乘法和除法是多余的!
But then, the multiplication and division are redundant!
.Net的数字处理让您着迷.将int
除以另一个int
时,结果为int
-它已经失去了所有小数位.现在,将其强制转换为float
时,这些小数位将不再出现.
因此,您必须欺骗.Net进行所需的转换:
当您将int
除以float
时,.Net会将int
转换为浮点数,结果是一个浮点数.好吧,double
可能是结果的类型,我不确定它的首选项.无论如何,double
是浮点数的常规使用"类型.
另外,将数字除以100并乘以100并不会改变它-您在这里感到困惑.汇总所有数字.
double[] percentage = new double[6];
percentage[x] = 100.0 * number[x] / sum;
100.0
被解释为double
,并且可以解决问题.
You were tricked by .Net''s number handling. When you divide anint
by anotherint
, the result is anint
- it has already lost all its decimal places. When you now cast it to afloat
, those decimal places cannot appear again.
Hence you must trick .Net to do the conversions you need:
When you divide anint
by afloat
, .Net converts theint
to a floating point number, and the result is a floating point number. Well,double
might be the type of the result, I am not fully sure about its preferences. Anyway,double
is the "normally used" type of floating point numbers.
Also, dividing a number by 100 and multiplicating it by 100 doesn''t change it - you got confused here. Sum up all the numbers.
double[] percentage = new double[6];
percentage[x] = 100.0 * number[x] / sum;
100.0
is interpreted as adouble
, and that will do the trick.
除了OriginalGriff的回答,我建议阅读以下内容: float(C#参考) [
In addition of OriginalGriff''s answer, i''d suggest to read this: float (C# Reference)[^]. Follow the links provided on the bottom of relevant page.
这篇关于C#中的百分比问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文