C#如何纠正数组中的错误输入

use*_*956 2 c# arrays loops input

所以我需要和学生一起上课.输入必须在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();
          }
      }
Run Code Online (Sandbox Code Playgroud)

Tim*_*ter 5

我想你只需要&&改为||:

while (wrong < 0 || wrong >= 100) ...
Run Code Online (Sandbox Code Playgroud)

数字不能同时低于零大于99.