所以我需要和学生一起上课。输入必须在0-100之间,否则它不接受用户的输入。
Console.WriteLine("How many students?");
int num1 = Keyboard.ReadInt();
int[] array = new int[num1];
Console.WriteLine("Give the student grades: ");
for (int i = 0; i < array.Length; i++)
{
int wrong;
wrong = Keyboard.ReadInt();
if (wrong > 0 && wrong <= 100)
{
array[i] = wrong;
}
else
{
while (wrong < 0 && wrong >= 100)
{
Console.WriteLine("Wrong input:");
wrong = Keyboard.ReadInt();
}
}
答案 0 :(得分:5)
答案 1 :(得分:1)
答案 2 :(得分:0)
更改您的代码:
while (wrong < 0 || wrong >= 100)
{
Console.WriteLine("Wrong input:");
wrong = Keyboard.ReadInt();
}
或者您可以在此处将while
更改为if
:
if (wrong < 0 || wrong >= 100)
{
Console.WriteLine("Wrong input:");
wrong = Keyboard.ReadInt();
}
或者你可以删除整个块并在else
语句中以这两种方式使用它:
if (wrong > 0 && wrong <= 100)
{
array[i] = wrong;
}
else if(wrong < 0 || wrong >= 100)
{
Console.WriteLine("Wrong input:");
wrong = Keyboard.ReadInt();
}
或
if (wrong > 0 && wrong <= 100)
{
array[i] = wrong;
}
else
{
Console.WriteLine("Wrong input:");
wrong = Keyboard.ReadInt();
}