[英]C# Blocking specific types of answers using a sentinel value
I am writing a code to get the highest mark and lowest mark. 我正在编写代码以获取最高分数和最低分数。 It also collects the average.
它还收集平均值。 It uses 999 to exit out of the do loop.
它使用999退出do循环。 I need to get it to stop adding the invalid answers as well as the 999, but I can't get it to work.
我需要获取它以停止添加无效答案以及999,但是我无法使其正常工作。 (mark != 999) stops 999, the exit command, from being added to the grades we're trying to track, but I need to make it so it blocks marks above 100 or below 0 and I can't seem to get it to work.
(mark!= 999)停止将999(退出命令)添加到我们尝试跟踪的成绩中,但是我需要使其退出,以便它阻止100或0以下的分数,我似乎无法理解上班。
The block of code I am working with is this: 我正在使用的代码块是这样的:
if (mark != 999 && (mark < 0 || mark > 100))
{
sum += mark;
count++;
}
This is the main program: 这是主程序:
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.
As it is pointed out in the comments, this line 正如评论中指出的那样,这一行
if (mark != 999 && (mark < 0 || mark > 100))
only evaluates to true when you input a mark less than 0 or greater than 100, which contradicts your requirement. 仅当您输入的标记小于0或大于100时才计算为true,这与您的要求相矛盾。 So the condition should be written like
所以条件应该写成
if (mark >= 0 && mark <= 100)
And for the while loops, the point is deciding when to break or continue. 对于while循环,重点是确定何时中断或继续。 A good practice is checking conditions at the start , if certain conditions are met like user inputting 999 or invalid mark, break or continue immediately without going further.
良好的做法是在开始时检查条件,如果满足某些条件(例如用户输入999或无效标记),则立即中断或继续操作而无需进一步操作。
So for the inner while loop , you can write it like this 因此,对于内部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;
}
}
Try this. 尝试这个。
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();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.