为什么这不起作用?(Why does this not work?)
以下是我正在使用的代码。
我希望输出为1.65,但我得到0。
这似乎是一个范围问题。 但是我已经将变量t声明为静态,那么为什么输出仍为0?
namespace WindowsFormsApplication1 { public partial class Form1 : Form { public static double t; private void Form1_Load(object sender, EventArgs e) { for (int i = 0; i < 100; i = i + 1) { t = (i * (1 / 60)); } MessageBox.Show(Convert.ToString(t)); } } }Below is the code that I am using.
I would like the output be 1.65, but I get 0.
This seems like a problem of scope. However I have declared the variable t as static, so why is the output still 0?
namespace WindowsFormsApplication1 { public partial class Form1 : Form { public static double t; private void Form1_Load(object sender, EventArgs e) { for (int i = 0; i < 100; i = i + 1) { t = (i * (1 / 60)); } MessageBox.Show(Convert.ToString(t)); } } }最满意答案
1 / 60将始终为0。
你正在进行整数除法 。
在除法运算中,如果需要double结果,则至少需要一个操作数为double 。
更改:
t = (i * (1 / 60));至:
t = (i * (1 / 60D));要么:
t = (i * (1D / 60));将解决问题。
1 / 60 will always be 0.
You are doing integer division.
In a division operation, you need at least one of the operands to be double if you want a double result.
Changing:
t = (i * (1 / 60));To:
t = (i * (1 / 60D));Or:
t = (i * (1D / 60));Will solve the issue.
更多推荐
发布评论