为什么这不起作用?(Why does this not work?)

编程入门 行业动态 更新时间:2024-10-28 08:18:08
为什么这不起作用?(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.

更多推荐

本文发布于:2023-07-15 21:50:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1119280.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:这不   work

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!