gpt4 book ai didi

c# - "Division by constant zero"?微软视觉 C#

转载 作者:太空宇宙 更新时间:2023-11-03 18:15:37 26 4
gpt4 key购买 nike

为什么我收到“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();
}
}
}

最佳答案

你正在进行整数除法:

int H = 1/2;

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

decimal H = 0.5M;

然后将您的 computerGuess 转换回 int:

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

关于c# - "Division by constant zero"?微软视觉 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6850493/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com