【发布时间】:2026-02-06 12:30:01
【问题描述】:
我在 c# 控制台中的基于文本的 RPG 有一个小问题。
我已经做了一个保存的方法,但是当我想加载它时,它给了我一个错误..
这是我的加载代码:
(字符串没有给出错误,但问题从Level到Agility开始)
代码:
public static void LoadData ()
{
// create reader & open file
TextReader tr = new StreamReader("SavedGame.txt");
// read lines of text
string xCoordString = tr.ReadLine();
string yCoordInt= tr.ReadLine();
//Convert the strings to int
Name = Convert.ToString(xCoordString);
PlayerType = Convert.ToString (xCoordString);
Level = Convert.ToString(xCoordString);
HP = Convert.ToInt32(yCoordInt);
Strenght = Convert.ToInt32(yCoordInt);
Intelligence = Convert.ToInt32(yCoordInt);
Agility = Convert.ToInt32(yCoordInt);
// close the stream
tr.Close();
}
【问题讨论】:
-
错误信息是什么?您可能需要单步执行您的代码并查看您尝试转换的每个变量的值
-
Level = Convert.ToInt32 btw 而不是 ToString
-
并且错误是:System.FormatException:输入字符串在 System.Int32.Parse (System.String s) [0x00000] in
:0 在 System.Convert .ToInt32 (System.String value) [0x00000] in :0 at practiceCSHARP.MainClass.LoadData () [0x0002f] in /home/stefano/Projects/practiceCSHARP/practiceCSHARP/Main.cs:396 at practiceCSHARP.MainClass .Main (System.String[] args) [0x002eb] 在 /home/stefano/Projects/practiceCSHARP/practiceCSHARP/Main.cs:109 -
你能展示一下你的文本文件是什么样子的吗?
-
level里面放的是int数3