首页 > 解决方案 > 如何计算控制台的输出

问题描述

这是一个评分标记表代码,我正在寻找应用程序如何计算一个字符串(例如:等级为 A)重复的次数。谢谢!此代码示例是我需要从中计算字符串的部分

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

标签: c#countoutput

解决方案


这个想法是每次打印 A 时简单地增加一个整数变量。

以您的代码为基础的示例:

// 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();

推荐阅读