【问题标题】:I have been stuck on this code for a few hours now. I am new to C# and am still in the process of learning so i'd appreciate it if you could solv我已经被困在这段代码上几个小时了。我是 C# 新手,仍在学习过程中,所以如果你能解决我将不胜感激
【发布时间】: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的区别

标签: c# console-application


【解决方案1】:

您可能需要考虑重构事物,以便由单独的函数提出一个问题。

这样的事情会让用户有机会回答每个问题;您的原始代码不清楚您是否希望他们进行多次尝试。 (不过,很容易在TestQuestion...中添加尝试循环)

using System;

namespace Homework
{
    class Program
    {
        static bool TestQuestion()
        {
            Random rnd = new Random();
            int num1 = rnd.Next(10, 51);
            int num2 = rnd.Next(10, 51);
            Console.Write(num1 + "+" + num2 + "= ");
            var userAnswer = Convert.ToInt32(Console.ReadLine());
            var correctAnswer = num1 + num2;
            if (userAnswer == correctAnswer)
            {
                Console.WriteLine("Correct");
                return true;
            }
            Console.WriteLine("Not quite. The answer was " + correctAnswer);
            return false;
        }
        static void Main(string[] args)
        {
            Console.Write("Enter your name: ");
            string name = Console.ReadLine();
            int score = 0;
            for (int i = 0; i < 5; i++)
            {
                if (TestQuestion())  // Returns true if user was correct
                {
                    score++;
                }
            }
            Console.WriteLine("That's the end of the test. You scored " + score);
        }
    }
}

【讨论】:

  • 最好不要在循环内移动 Random 的创建。 (尽管回答所花费的时间应该足以让随机生成器代码在每个循环中创建一组不同的数字)
  • 谢谢大家,多亏了提示,我想出了办法。
  • @Steve 好点。我想你可以静态初始化它:)
  • @Steve 这是一个合理的观点,但现代 c# 不会从时钟中随机播种
  • @CaiusJard 我不知道,谢谢。事实上,我已经使用 Linqpad 5 (NET) 对 Linqpad 6 (Core) 尝试了这段代码(删除 IO),结果确实不同
猜你喜欢
  • 2022-11-19
  • 1970-01-01
  • 2020-12-27
  • 2023-02-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-05
相关资源
最近更新 更多