我有一个简单的循环练习, 在该练习中,我需要在while循环中输入5个数字,然后检查数字是否大于数字 之前,如果是,则将其计数,否则,则从新开始,如果是,则计数,如果数字小于之前的数字。
input: example: 1,2,3,4,5
1. number
2. number
3. number
4. number
5. number
因此,如果我的数字大于5,则我先将其计数。 (先计算多少个数字,然后计算之前的数字) 如果我的编号器最小,然后在编号之前将其计数。(相同) 如果我的数字相等,则算一下..(相同)
我知道该怎么做,但我不知道将新输入放入列表中的数字。
感谢帮助 我当前的代码:
while (totalcount < 5) {
number = int.Parse(Console.ReadLine());
if (number == 0 && lastnumber > number) {
lastnumber = number;
bigcount++;
}
if (number == 0 && number < lastnumber) {
smallcount++;
}
if (number == lastnumber) {
equalcount++;
}
totalcount++;
}
Console.WriteLine("there are" + " " + bigcount + " " + "biggers numbers.");
Console.WriteLine("there are" + " " + smallcount + " " + "smallest numbers.");
Console.WriteLine("there are" + " " + equalcount + " " + "equales numbers.");
答案 0 :(得分:0)
我试图忠实于您的样品。基本上,我删除了检查number == 0
是否毫无意义的方法,并添加了if子句totalcount > 0
来跳过对第一个数字的操作,因为在这种情况下lastnumber
将不存在。 / p>
int number, totalcount = 0, lastnumber = 0, bigcount = 0, smallcount = 0, equalcount = 0;
while (totalcount < 5)
{
number = int.Parse(Console.ReadLine());
if (totalcount > 0)
{
if (lastnumber < number)
{
bigcount++;
}
else if (lastnumber > number)
{
smallcount++;
}
else
{
equalcount++;
}
}
totalcount++;
lastnumber = number;
}
Console.WriteLine("there are" + " " + bigcount + " " + "biggers numbers.");
Console.WriteLine("there are" + " " + smallcount + " " + "smallest numbers.");
Console.WriteLine("there are" + " " + equalcount + " " + "equales numbers.");