“除以常数零”?微软视觉C#
为什么我收到错误消息“错误 1 除以常量零 25 17 ConsoleApplication3”
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Higher, Lower... What is the highest possible number that you're thinking of?");
string b = Console.ReadLine();
int f = int.Parse(b);// F = THE MAX #
Random computerguess = new Random();
int computerGuess = computerguess.Next(f);
Console.WriteLine(computerGuess);
Console.WriteLine("if your number is higher than the number displaid above, then press the '1' key so I guess higher. if your number is lower press the '0' (down) as in telling the comp to go down/lower");
string I = Console.ReadLine();
int G = int.Parse(I);
int H = 1/2;
if (G == 0)
{
computerGuess = computerGuess * H;
Console.WriteLine(computerGuess);
}
Console.Read();
}
}
}
Why am I getting an error message of "Error 1 Division by constant zero 25 17 ConsoleApplication3"
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Higher, Lower... What is the highest possible number that you're thinking of?");
string b = Console.ReadLine();
int f = int.Parse(b);// F = THE MAX #
Random computerguess = new Random();
int computerGuess = computerguess.Next(f);
Console.WriteLine(computerGuess);
Console.WriteLine("if your number is higher than the number displaid above, then press the '1' key so I guess higher. if your number is lower press the '0' (down) as in telling the comp to go down/lower");
string I = Console.ReadLine();
int G = int.Parse(I);
int H = 1/2;
if (G == 0)
{
computerGuess = computerGuess * H;
Console.WriteLine(computerGuess);
}
Console.Read();
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您正在进行整数除法:
这将始终为零。而是使用小数:
然后将
computerGuess
转换回 int:You are doing an integer division:
This will be zero always. Instead use a decimal:
and then cast your
computerGuess
back to int:因为
将 H 设置为 0:0.5 向下舍入到最接近的整数。
Because
sets H to 0: 0.5 rounded down to the nearest integer.
当我尝试编译您的代码时,我没有收到该错误消息。
该错误消息可能有点误导。这是第 25 行,实际上并没有除以零:
您正在除以整数,因此结果将为零。此计算由编译器完成,因此生成的代码相当于:
您可能会收到警告,因为结果很可能不是预期的。
I don't get that error message when I try to compile your code.
The error message might be a bit misleading. This is line 25, that doesn't actally divide by zero:
You are dividing integers, so the result will be zero. This calculation is done by the compiler, so the code generated is equivalent to:
You probably get a warning because the result is most likely not the intended.
关于这一行...
您正在尝试将浮点值 0.5 存储在整数变量中。
About the line...
You are triying to store the float value 0.5 inside a Integer var.