Thursday, August 18, 2022

[FIXED] How to count the output of the console

Issue

This is a Grading Marksheet code and I am looking for how the app may count how many times a string (e.g: Grade is A) has been repeated. Thank you! This code sample is the part where I need to count the string from

if (p > 60 && p <= 80)
{
     Console.WriteLine("Grade is A");
}
if (p > 80 && p <= 100)
{
     Console.WriteLine("Grade is A++");
}
Console.ReadLine();

Solution

The idea would be to simply increase a integer variable for each time you print an A.

Example with your Code as Base:

// Use a Variable that you increase in value
// To see how many A were printed
int acount = 0;

// Is number bigger than 60 and smaller or equal to 80
if (p > 60 && p <= 80)
{
    Console.WriteLine("Grade is A");
    acount++;
}
// If the first statement is true the second can't be true anyway
// so use else if so it doesn't have to make usless checks

// Is number bigger than 80 and smaller or equal to 100
else if (p > 80 && p <= 100)
{
    Console.WriteLine("Grade is A++");
    acount++;
}
// Print out result
Console.WriteLine("A was printed: " + acount + " times");
Console.ReadLine();


Answered By - IndieGameDev
Answer Checked By - Candace Johnson (PHPFixing Volunteer)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.