- 1
why didn't the output come as good??
static void Main(string[] args) { int mark = 85; if (mark >= 50) { Console.WriteLine("You passed."); if (mark == 100) { Console.WriteLine("Perfect!"); if (mark == 85){Console.WriteLine("good");} } } else { Console.WriteLine("You failed.");
3 ответов
+ 7
Checking of mark if it's 85 is in the block of checking if it's 100. A mark can't be both 100 and 85.
You must have close the block bedore checking if it's 85. Like this:
static void Main(string[] args)
{
int mark = 85;
if (mark >= 50) {
Console.WriteLine("You passed.");
if (mark == 100) {
Console.WriteLine("Perfect!");
}
else if (mark == 85) {
Console.WriteLine("good");}
}
else {
Console.WriteLine("You failed.");
+ 3
Because, your code will never print "good".
Follow Albert's Code, that will fix it 😃
or maybe, this is what you need, so that grades greater than 85 but less than 100 will be handled.
static void Main(string[] args)
{
int mark = 85;
if (mark >= 50)
{
Console.WriteLine("You passed.");
if (mark == 100)
{
Console.WriteLine("Perfect!");
}
else if (mark >= 85)
{
Console.WriteLine("good");
}
}
else
{
Console.WriteLine("You failed.");
}
+ 1
Building on what both Albert & Erwin (the latter being more clear & fixes the missing end tag from the final else statement) have already shared, I'd point out one other thing: if the grade ≥ 50, then your code, as written, prints BOTH a line that states You Passed as well as a line that states either Perfect or Good, which seems redundant. I'd recommend removing the You Passed message from it's current position and placing it in an else-if mark < 85 statement (still nested within the if mark ≥ 50 statement) so that it prints only if the mark is ≥ 50 AND < 85.