【问题标题】:How to count the output of the console如何计算控制台的输出
【发布时间】:2020-07-17 09:43:43
【问题描述】:

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

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

【问题讨论】:

  • 声明一个计数器(var count = 0)并在正确的if语句中递增(counter++)
  • 向我们展示输入和所需输出的示例。
  • 所以输入将是这样的。卷:(数字)分数 A:分数 B:分数 C:所需的输出将说明有多少学生获得(例如:A 级)

标签: c# count output


【解决方案1】:

这个想法是在每次打印 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();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-22
    相关资源
    最近更新 更多