如果 else 语句和 while 循环
本文关键字:while 循环 语句 else 如果 | 更新日期: 2023-09-27 18:37:22
请看下面的代码
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() 方法中,我正在尝试生成一些数学问题。
如果运算符在"-"中,并且如果第一个数字<第二个数字,则应重新生成数字(第一个数字和第二个数字)以确保答案不是减号值。>
而且,如果运算符为"/"并且布尔值为 false,则应再次重新生成数字以确保答案(这意味着计算,例如 2/1 = 2)不包含任何浮点(这意味着它不应该生成像 2/3 这样的问题,因为答案包含浮点)。
在我的尝试中,以上两者都没有发生。它仍然会产生意想不到的答案,或者有时什么都没有。为什么?请帮忙
请注意,我是一名 Java 开发人员,这是我的第一个主要 C# 项目。
好吧,它有时什么也不返回,因为除了您捕获的边缘情况之外,您从未将result
设置为""
以外的任何内容。
而且你的while
陈述都是错误的。它应该是:
while ((firstNumber % secondNumber) != 0)
:
while (firstNumber < secondNumber)
但是,为了提高效率,我不会做第二while
因为您可能会在该属性所在的位置获得长时间的对。如果数字是错误的,只需交换它们。
而且在极少数情况下,你的数字选择中的余数将为零(9/3
、8/4
、8/2
、6/3
、6/2
、4/2
、M/M
和N/1
(我认为)是详尽的列表)。如果你想要一组更扩展的方程,我会走另一条路,选择两个数字来多,然后将第一个与结果交换。
例如,给定两个数字 a = 3
和 b = 7
,只需执行以下操作:
a = a * b;
并且您有a = 21, b = 7
保证给出积分乘数并"21 / 7"
提供方程式。
所以这个(伪代码)是我要开始的:
# Get the random values.
op = random_from ("+-/*")
n1 = random_from ("123456789")
n2 = random_from ("123456789")
# For subtraction, make sure n1 >= n2.
if op = "-" and n1 < n2:
tmp = n1;
n1 = n2;
n2 = tmp;
# For division, make sure n1 is integral multiplier of n2.
if op = "/":
n1 = n1 * n2
# Return expression in ALL cases.
return n1 + op + n2
while (firstNumber % secondNumber == 0)
应该是
while (firstNumber % secondNumber != 0)
只要您提供的条件true
,while
循环就会继续执行。 由于您希望第一个数字 % 第二个数字等于 0;您应该循环并生成新值,只要firstNumber % secondNumber != 0
.
我相信你在减法循环方面也有同样的问题。
最后(与您的问题无关),您可以使用==
来比较 C# 中的字符串。
if ((operators == "/") && (enableDouble == false))
你在while
语句中有错误的条件,你应该有恰恰相反的条件。此外,如果没有任何条件匹配,则不会为结果赋值,因此应将行result = firstNumber + operators + secondNumber;
移出条件语句(或仅替换为简单返回):
...
if ((operators.Equals("/")) && (enableDouble == false))
{
while (firstNumber % secondNumber != 0) // <-- change here
{
firstNumber = listOfNumbers[randomizer.Next(listOfNumbers.Length)];
}
}
// second condition was repearing while condition and that was redundant.
else if (operators.Equals("-"))
{
while (firstNumber < secondNumber) // <-- change here
{
firstNumber = listOfNumbers[randomizer.Next(listOfNumbers.Length)];
secondNumber = listOfNumbers[randomizer.Next(listOfNumbers.Length)];
}
}
return firstNumber + operators + secondNumber;
在这两种情况下,您的while条件都是错误的,您需要将其更改为
while (firstNumber % secondNumber != 0)
和
while (firstNumber < secondNumber)
这是我从您的问题中理解的解决方案。
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;
}
- 通过创建两个不同的测试项目来实现测试用例
- 使用包含这些测试用例的 MSVS 单元测试项目进行报告 报告测试覆盖率,在第
- 一种情况下考虑MCDC,在第二种情况下考虑总体覆盖率,并评论发现不同覆盖率指标的原因
对于下面的代码
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;
}
}
}