请看下面的代码
namespace Funny
{
class QuesionsAndAnswers
{
private double firstNumber;
private double secondNumber;
private double userAnswer;
private double computerAnswer;
private string operators;
private bool answerCorrect;
private bool enableDouble;
private double[] listOfNumbers;
private string[] listOfOperators;
private Random randomizer;
private static QuesionsAndAnswers qa;
private QuesionsAndAnswers()
{
randomizer = new Random();
listOfNumbers = new double[] { 1,2,3,4,5,6,7,8,9 };
listOfOperators = new string[] { "+", "-", "*", "/" };
}
public static QuesionsAndAnswers getQuesionsandAnswersInstance()
{
if (qa == null)
qa = new QuesionsAndAnswers();
return qa;
}
public string generateQuestions()
{
string result = "";
operators = listOfOperators[randomizer.Next(listOfOperators.Length)];
firstNumber = listOfNumbers[randomizer.Next(listOfNumbers.Length)];
secondNumber = listOfNumbers[randomizer.Next(listOfNumbers.Length)];
if ((operators.Equals("/")) && (enableDouble == false))
{
while (firstNumber % secondNumber == 0)
{
firstNumber = listOfNumbers[randomizer.Next(listOfNumbers.Length)];
}
result = firstNumber + operators + secondNumber;
}
else if (operators.Equals("-") && (firstNumber<secondNumber))
{
while (firstNumber > secondNumber)
{
firstNumber = listOfNumbers[randomizer.Next(listOfNumbers.Length)];
secondNumber = listOfNumbers[randomizer.Next(listOfNumbers.Length)];
}
result = firstNumber + operators + secondNumber;
}
return result;
}
public void setDoubleAnswers(bool check)
{
enableDouble = check;
}
}
}
在这里,在 generateQuestions() 方法中,我试图生成一些数学问题。
如果运算符在“-”中并且如果 firstNumber < secondNumber,则应重新生成数字(第一个数字和第二个数字)以确保答案不是负值。
并且,如果运算符是“/”并且布尔值为 false,它应该再次重新生成数字以确保答案(这意味着计算,例如 2/1 = 2)不包含任何浮点(这意味着它不应该产生像 2/3 这样的问题,因为答案包含浮点数)。
在我的尝试中,以上都没有发生。它仍然会产生意想不到的答案,或者有时什么也不会。这是为什么?请帮忙
请注意,我是一名 Java 开发人员,这是我的第一个主要 C# 项目。