“除以常数零”?微软视觉C#

发布于 2024-11-26 16:25:42 字数 1204 浏览 0 评论 0原文

为什么我收到错误消息“错误 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

无声情话 2024-12-03 16:25:42

您正在进行整数除法:

int H = 1/2;

这将始终为零。而是使用小数:

decimal H = 0.5M;

然后将 computerGuess 转换回 int:

..
computerGuess = (int)(computerGuess * H);

You are doing an integer division:

int H = 1/2;

This will be zero always. Instead use a decimal:

decimal H = 0.5M;

and then cast your computerGuess back to int:

..
computerGuess = (int)(computerGuess * H);
此刻的回忆 2024-12-03 16:25:42

因为

int H = 1/2;

将 H 设置为 0:0.5 向下舍入到最接近的整数。

Because

int H = 1/2;

sets H to 0: 0.5 rounded down to the nearest integer.

女中豪杰 2024-12-03 16:25:42

当我尝试编译您的代码时,我没有收到该错误消息。

该错误消息可能有点误导。这是第 25 行,实际上并没有除以零:

int H = 1/2;

您正在除以整数,因此结果将为零。此计算由编译器完成,因此生成的代码相当于:

int H = 0;

您可能会收到警告,因为结果很可能不是预期的。

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:

int H = 1/2;

You are dividing integers, so the result will be zero. This calculation is done by the compiler, so the code generated is equivalent to:

int H = 0;

You probably get a warning because the result is most likely not the intended.

娜些时光,永不杰束 2024-12-03 16:25:42

关于这一行...

int H = 1/2;

您正在尝试将浮点值 0.5 存储在整数变量中。

About the line...

int H = 1/2;

You are triying to store the float value 0.5 inside a Integer var.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文