gpt4 book ai didi

c# - 当我使用 Math.Round() 时,它总是返回下限

转载 作者:行者123 更新时间:2023-12-03 19:43:52 25 4
gpt4 key购买 nike

我正在制作一个数学项目,用户根据他们选择的难度级别回答问题!

但每次问题是除法问题时,它都会返回下界。

例如,如果代码询问“5/3”是什么,答案应该是 2,但代码认为正确答案是 1。

这是我的代码:

using System;

namespace mathstester
{
class Program
{
enum UserDifficulty
{
Easy,
Normal,
Hard
}
public static void Main(string[] args)
{
string userDifficulty = "";
do
{
Console.WriteLine("What difficulty level would you like to do! Please type E for Easy, N for Normal and H for hard");
userDifficulty = Console.ReadLine().ToUpper();
} while (userDifficulty != "E" && userDifficulty != "N" && userDifficulty != "H");

int numberOfQuestions = 0;
do
{
Console.Write("How many questions would you like to answer? Please type a number divisible by 10!");
int.TryParse(Console.ReadLine(), out numberOfQuestions);
} while (numberOfQuestions % 10 != 0);

int numberOfQuestionsLeft = numberOfQuestions;
Random random = new Random();
int score = 0;

while (numberOfQuestionsLeft > 0)
{
var operation = random.Next(1, 7);
int number1 = 0;
int number2 = 0;
switch (userDifficulty)
{
case "E":
switch (operation)
{
case 1:
number1 = random.Next(1000);
number2 = random.Next(1000);
break;
case 2:
number1 = random.Next(1000);
number2 = random.Next(1000);
break;
case 3:
number1 = random.Next(13);
number2 = random.Next(13);
break;

}
break;
case "N":
switch (operation)
{
case 1:
number1 = random.Next(1000);
number2 = random.Next(1000);
break;
case 2:
number1 = random.Next(1000);
number2 = random.Next(1000);
break;
case 3:
number1 = random.Next(1000);
number2 = random.Next(1000);
break;
case 4:
number1 = random.Next(1, 10000);
number2 = random.Next(1, 1000);
break;

}
break;
case "H":
switch (operation)
{
case 3:
number1 = random.Next(1000);
number2 = random.Next(1000);
break;
case 4:
number1 = random.Next(1, 10000);
number2 = random.Next(1, 1000);
break;
case 5:
number1 = random.Next(13);
number2 = random.Next(5);
break;
case 6:
number1 = random.Next(1000);
break;

}
break;
}

if(operation == 1 && (userDifficulty == "E" || userDifficulty == "N"))
{
Console.Write($"What is {number1} + {number2} =");
int correctAnswer = number1 + number2;
int userAnswer = Convert.ToInt32(Console.ReadLine());
if (correctAnswer == userAnswer)
{
Console.WriteLine("Well Done!");
score++;
}
else
{
Console.WriteLine("Your answer is incorrect!");
}
numberOfQuestionsLeft--;
}
else if (operation == 2 && (userDifficulty == "E" || userDifficulty == "N"))
{
Console.Write($"What is {number1} - {number2} =");
int correctAnswer = number1 - number2;
int userAnswer = Convert.ToInt32(Console.ReadLine());
if (correctAnswer == userAnswer)
{
Console.WriteLine("Well Done!");
score++;
}
else
{
Console.WriteLine("Your answer is incorrect!");
}
numberOfQuestionsLeft--;
}
else if (operation == 3 && (userDifficulty == "E" || userDifficulty == "N" || userDifficulty == "H"))
{
Console.Write($"What is {number1} * {number2} =");
int correctAnswer = number1 * number2;
int userAnswer = Convert.ToInt32(Console.ReadLine());
if (correctAnswer == userAnswer)
{
Console.WriteLine("Well Done!");
score++;
}
else
{
Console.WriteLine("Your answer is incorrect!");
}
numberOfQuestionsLeft--;
}
else if (operation == 4 && (userDifficulty == "N" || userDifficulty == "H") && (number1 > number2))
{
Console.Write($"To the nearest integer, What is {number1} / {number2} =");
double correctAnswer = (number1 / number2);
double roundedCorrectAnswer = Math.Round(correctAnswer);
int userAnswer = Convert.ToInt32(Console.ReadLine());
if (roundedCorrectAnswer == userAnswer)
{
Console.WriteLine("Well Done!");
score++;
}
else
{
Console.WriteLine("Your answer is incorrect!");
}
numberOfQuestionsLeft--;
}
else if (operation == 5 && userDifficulty == "H")
{
Console.Write($"What is {number1} ^ {number2} =");
double correctAnswer = Math.Pow(number1, number2);
int userAnswer = Convert.ToInt32(Console.ReadLine());
if (correctAnswer == userAnswer)
{
Console.WriteLine("Well Done!");
score++;
}
else
{
Console.WriteLine("Your answer is incorrect!");
}
numberOfQuestionsLeft--;
}
else if (operation == 6 && userDifficulty == "H")
{
Console.Write($"To the nearest integer, What is √{number1} =");
double correctAnswer = Math.Sqrt(number1);
double roundedCorrectAnswer = Math.Round(correctAnswer);
int userAnswer = Convert.ToInt32(Console.ReadLine());
if (roundedCorrectAnswer == userAnswer)
{
Console.WriteLine("Well Done!");
score++;
}
else
{
Console.WriteLine("Your answer is incorrect!");
}
numberOfQuestionsLeft--;
}
}
Console.WriteLine($"You got a score of {score} out of {numberOfQuestions}");
}
}
}

发生的情况是这样的:

enter image description here

最佳答案

由于 number1number2 是整数,因此可以使用 integer division 将它们相除。 ,并且只保留结果的“整个”部分(即小数点左边的部分)。

在执行除法之前,您可以将它们(或至少其中一个)转换为 double 以获得“正确”的结果:

double correctAnswer = ((double) number1) / number2;

关于c# - 当我使用 Math.Round() 时,它总是返回下限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60678464/

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