小编use*_*956的帖子

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

所以我需要和学生一起上课.输入必须在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)

c# arrays loops input

2
推荐指数
1
解决办法
132
查看次数

标签 统计

arrays ×1

c# ×1

input ×1

loops ×1