【发布时间】:2021-10-10 18:39:32
【问题描述】:
我要创建的程序是我需要随机生成 5 道数学题,最后输出分数。我知道这可能看起来很简单,但我尝试了许多不同的方法来做到这一点,但它给了我相同的问题和重复的答案,但它确实告诉我它是否正确,所以循环几乎可以工作。
using System;
namespace Homework
{
class Program
{
static void Main(string[] args)
{
// Maths test problem
string name;
int score, ans, question1;
Console.Write("Enter your name: ");
name = Console.ReadLine();
score = 0;
Random rnd = new Random();
int num1 = rnd.Next(10, 51);
int num2 = rnd.Next(10, 51);
int[] numbers = new int[2];
numbers[0] = num1;
numbers[1] = num2;
Console.Write(num1 + "+" + num2 + "= ");
question1 = Convert.ToInt32(Console.ReadLine());
ans = num1 + num2;
for (int i = 0; i < 5; i++)
{
Console.Write(question1);
while (question1 == ans)
{
Console.WriteLine("Correct");
score = score + 1;
Console.Write(question1);
break;
}
while (question1 != ans)
{
Console.WriteLine("Not quite. The answer was " + ans);
Console.Write(question1);
break;
}
}
Console.WriteLine("That's the end of the test. You scored " + score);
}
}
}
【问题讨论】:
-
如果您需要在循环的每次迭代中以不同方式执行某些操作,您自然需要在循环内而不是循环之前执行这些操作。
-
感谢您的实际帮助,代码现在可以正常工作,但现在可以这样做了:35+21= 78 78 不完全。答案是 56。它重复了用户的答案。
-
因为您的代码中有多个 console.write。检查Write和WriteLine的区别