我不理解'新字符串'和这里的[i]

问题描述 投票:0回答:2

所以我在这里不理解'新字符串'。我尝试阅读它,但找不到任何易于理解的具体答案。字符串和新字符串有什么区别?

 public class MainClass {
      public static void Main (string[] args) {

      Console.Write("\nInput number of students: ");
      var totalstudents = int.Parse(Console.ReadLine());

        var name = new string [totalstudents];
        var grade = new int [totalstudents]; 

我的程序无法编译,出现了我认为可能与[i]相关的意外符号“名称”和“等级”。

  for (int i =0 ; i<totalstudents ; i++)
        {
         Console.WriteLine("\nInput student name: ")
          name[i] = Console.ReadLine(); 
         Console.WriteLine("\nInput student grade: ")
          grade[i] = int.parse(Console.ReadLine());
        }

  foreach(var gradesof in grade)
   { 
    Console.WriteLine(gradesof);
   }

  }
}

}
c#
2个回答
0
投票

如注释中所述,new string[...]正在创建数组。

您的编译问题包括...

行:

Console.WriteLine("\nInput student name: ")
Console.WriteLine("\nInput student grade: ")

…都末尾缺少分号;

也:

grade[i] = int.parse(Console.ReadLine());

…parse应该为Parse


0
投票

string是字符串。 string[]是一个字符串数组。

string s = "hello"; // Declares and initializes a string.

string[] a = new string[3]; // Declares and initializes a string array of length 3.
                            // Every element of the array is `null` so far.

// Fill the array with meaningful values.
a[0] = "hello";
a[1] = "world";
a[2] = "!";
© www.soinside.com 2019 - 2024. All rights reserved.