我想從文件中讀取數組中的值。我從文件部分讀取數據,但無法將其存儲到數組中,因爲它給了我一個錯誤「Value can not be null」,因爲在循環之後,我的變量的值變爲null並且數組不能爲null。這是我的。我意識到,for循環可能不在正確的位置,所以任何幫助將它放在哪裏會很好。c#如何從文件中存儲數組的值
Program p = new Program();
int MAX = 50;
int[] grades = new int[MAX];
string environment = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal) + "\\";
string path = environment + "grades.txt";
StreamReader myFile = new StreamReader(path);
string input;
int count = 0;
do
{
input = myFile.ReadLine();
if (input != null)
{
WriteLine(input);
count++;
}
} while (input != null);
for (int i = 0; i < count; i++)
{
grades[i] = int.Parse(input);
}
什麼是'成績' – pm100
成績是我在其中存儲grades.txt文件中的值的數組 –
您剛剛從while循環退出後啓動for循環。當輸入爲空時退出while循環的條件是真的 – Steve