C# 使用标记值阻止特定类型的答案

C# Blocking specific types of answers using a sentinel value

我正在编写代码以获得最高分和最低分。它还收集平均值。它使用 999 退出 do 循环。 我需要让它停止添加无效答案以及 999,但我无法让它工作。 (mark != 999) 停止将退出命令 999 添加到我们要跟踪的成绩中,但我需要做到这一点,以便它阻止高于 100 或低于 0 的标记,但我似乎无法得到它上班。

我正在使用的代码块是这样的:

    if (mark != 999 && (mark < 0 || mark > 100))
                {
                    sum += mark;
                    count++;
                }

这是主程序:

    int mark,
            sum = 0,
            lowMark = 100,
            highMark = 0,
            average,
            count = 0;
        char playagain = 'N';
        // In a do loop, ask the user to enter a grade for a student or 999 to quit
        do
        {
            do
            {
                Console.Write("Please enter a mark for the student or enter 999 to quit: ");
                mark = int.Parse(Console.ReadLine());
                while (mark != 999)
                {
                    if (mark != 999 && (mark < 0 || mark > 100))
                    {
                        sum += mark;
                        count++;
                    }
                    if (mark < lowMark)
                    {
                        lowMark = mark;
                    }
                    if (mark > highMark)
                    {
                        highMark = mark;
                    }
                    if (mark < 0 || mark > 100)
                    {
                        Console.WriteLine("Invalid input value.");
                        Console.Write("Please enter a mark for the student or enter 999 to quit: ");
                        mark = int.Parse(Console.ReadLine());
                    }
                    else break;
                }
            } while (mark != 999);

            average = sum / count;

            Console.WriteLine($"\nThe class average was {average}%");
            Console.WriteLine($"The highest mark was {highMark}% and the lowest mark was {lowMark}%");

            Console.WriteLine("\nWould you like to start again?: Y/N");
            playagain = char.Parse(Console.ReadLine());
            playagain = char.ToUpper(playagain);
        } while (playagain == 'Y');
        // In a while loop, the program will:
        // - Determine if the entered mark is the highest or lowest grade
        // - Validate the entered grade before entering another grade; display some error
        //   message if the grade is invalid (i.e. < 0 or > 100)
        // - Prompt the user to enter another grade or 999 to quit

        // If there are valid marks:
        // - Calculate the average grade for the class
        // - Display the average, highest, and lowest grades
        // Else, display some error message
        // - Prompt the user to redo the steps above or quit.

正如评论中指出的那样,这一行

if (mark != 999 && (mark < 0 || mark > 100)) 

仅当您输入小于 0 或大于 100 的标记时才评估为真,这与您的要求相矛盾。所以条件应该写成

if (mark >= 0 && mark <= 100)

对于 while 循环,重点是决定何时中断或继续。一个好的做法是在开始时检查条件 ,如果满足某些条件,例如用户输入 999 或无效标记,则立即中断或继续,而不会继续。

所以对于内层while循环,可以这样写

while (true)
{
    Console.Write("Please enter a mark for the student or enter 999 to quit: ");
    mark = int.Parse(Console.ReadLine());
    if (mark == 999) //get signal for exit
    {
        break;  //go no further, stop the loop
    }

    if (mark < 0 || mark > 100) //invalid inputs
    {
        Console.WriteLine("Invalid input value.");
        Console.Write("Please enter a mark for the student or enter 999 to quit: ");
        continue; //go no further, continue the loop, let user input again
    }

    //handle valid inputs
    sum += mark;
    count++;

    if (mark < lowMark)
    {
        lowMark = mark;
    }
    if (mark > highMark)
    {
        highMark = mark;
    }
}   

试试这个。

class Program
{
    static void Main(string[] args)
    {
        bool playAgain = true;

        while (playAgain)
        {
            decimal mark = 0, lowMark = -1, highMark = -1, sum = 0, average = 0; 
            int count = 0;                

            do
            {
                Console.Write("Please enter a mark for the student or enter 999 to quit: ");

                if (!decimal.TryParse(Console.ReadLine(), out mark) || 
                    (mark < 0 || mark > 100 || (mark > 0 && mark < 1)))
                {
                    Console.WriteLine("Invalid input value.");
                    continue;
                }
                else
                {
                    mark = mark / (decimal)100;
                    sum += mark;
                    count++;
                    if (lowMark < 0 || mark < lowMark)
                    {
                        lowMark = mark;
                    }
                    if (mark > highMark)
                    {
                        highMark = mark;
                    }
                }
            } while (mark != 999);

            average = count == 0 ? 0 : sum / count;
            lowMark = lowMark < 0 ? 0 : lowMark;
            highMark = highMark < 0 ? 0 : highMark;

            Console.WriteLine("\nThe class average was {0}", 
                average.ToString("P1", CultureInfo.InvariantCulture));
            Console.WriteLine("The highest mark was {0} and the lowest mark was {1}", 
                highMark.ToString("P1", CultureInfo.InvariantCulture), 
                lowMark.ToString("P1", CultureInfo.InvariantCulture));
            Console.WriteLine("\nWould you like to start again?: Y/N");
            playAgain = Console.ReadKey().Key == ConsoleKey.Y;
            Console.WriteLine();
        }
    }
}