向 ListBox 添加 n 个字符

Adding n amount of characters to ListBox

我有一个列表框,需要用成绩和相应的 + 例如,如果一个学生有 2 个 A、1 个 B 和 2 个 C,列表框将如下所示:A ++ B + C ++

grade = ((Student)students_list[j]).AverageNumMark;
if (grade >= 70)
{ numA++; }
else if (grade >= 60)
{ numB++; }
else if (grade >= 50)
{ numC++; } 

我遇到的问题是成绩 (+) 的数量被保存为双精度,不幸的是我不能写“+”* numA。

如何让它显示 numA 个 +s?

我也知道我可以将它作为一个文本框来实现,并启用多行,但我不确定如何显示 numA +s。

您需要在循环中构建 + 号字符串。

var aPlusses = string.Empty;
for(var i = 0; i < numA; i++)
{
    aPlusses += "+";
}

对每个年级都这样做,并将回复连接到您的文本框中。

您可以使用带有 charintstring 构造函数的重载。

来自documentation

Initializes a new instance of the String class to the value indicated by a specified Unicode character repeated a specified number of times.

例如:

double numA = 2; //this would be better as an int
string s = new string('+', (int)numA);
Console.WriteLine(s); //prints ++