C# 如何设置书写单词的最低要求编号
有人可以编辑代码,并将结果“2”的最低要求为他们显示吗C# 如何设置书写单词的最低要求编号,c#,C#,有人可以编辑代码,并将结果“2”的最低要求为他们显示吗 if (!textBox2.Text.EndsWith(" Animals")) textBox2.Text += " Animals"; if (this.textBox2.Text != "") { listBox1.Items.Add(this.textBox2.Text); } if (!textBox3.
if (!textBox2.Text.EndsWith(" Animals"))
textBox2.Text += " Animals";
if (this.textBox2.Text != "")
{
listBox1.Items.Add(this.textBox2.Text);
}
if (!textBox3.Text.EndsWith(" People"))
textBox3.Text += " People";
if (this.textBox3.Text != "")
{
listBox1.Items.Add(this.textBox3.Text);
}
例如(这是我代码的一部分,我成功地完成了,但无法完成上面的代码)我之所以询问,是因为上面的代码没有限制,所以它显示为“1动物”:
if (textBox2.Text == "1" && textBox3.Text == "1")
{
textBox2.Text += " Animal";
textBox3.Text += " Person";
}
我需要它来显示动物,人,当结果是2或更多。不仅仅是2,据我所知,你想在一个框中写一个数字,然后检查字符串是否以“Animal”或“Animals”等结尾。我个人只使用整数,然后检查数字,然后添加字符串值(Animals、Person等) 但是,使用您的方法并根据需要对其进行修改,我将两个代码快照合并在一起,并尝试解决您描述的问题:
if (textBox2.Text == "1")
{
textBox2.Text += " Animal";
}
if (textBox3.Text == "1"
{
textBox3.text += " Person";
}
if (!textBox2.Text.EndsWith(" Animals") && !textBox2.Text.EndsWith(" Animal"))
textBox2.Text += " Animals";
if (this.textBox2.Text != "")
{
listBox1.Items.Add(this.textBox2.Text);
}
if (!textBox3.Text.EndsWith(" People") && !textBox3.Text.EndsWith(" Person"))
textBox3.Text += " People";
if (this.textBox3.Text != "")
{
listBox1.Items.Add(this.textBox3.Text);
}
这样,如果开始时为1,则添加“动物”/“人”。如果文本框中有“动物”或“动物”,则不会进一步编辑。如果没有,它将添加“动物”。人也是如此
再次-我强烈建议在这种任务中使用ints而不是字符串格式
这有用吗
int number= 0;
if (Int32.TryParse(textBox2.Text, out number)) { // Convert your text to int if it's possible
if (number >= 2) {
textBox2.Text += " Animals";
}
if (number == 1) {
textBox2.Text += " Animal";
}
}
你不能把这两段代码合并在一起吗?确保只调用两个文本编辑中的一个?我不能,但我不知道如何说,当结果为2或更多时,我希望它写入单词,如2,3,4,5,6,7,8,9,直到9999。