好的,这是我在游戏中遇到的第二个问题。这是一个基于文本的游戏,我也想根据用户的答案增加点数。我还有另一个课程可以保存游戏和积分,以防用户想要稍后继续游戏。游戏保存正常,但点数没有,我认为这是因为它应该返回一个整数。但我不知道如何解决这个问题。程序甚至没有启动,因为我得到的错误是线程“main”中的异常java.lang.NumberFormatException:java.lang.Integer.parseInt中的null ...提前感谢!
MainGame
public class MainGame {
public static GameSave gameSave = new GameSave();
public static String user = "";
public static Scanner scanner = new Scanner(System.in);
public static String question;
public static int relationshipPoints;
public static void main(String[] args) {
Random random = new Random();
question = gameSave.loadGame();
relationshipPoints = gameSave.loadPoints();
RelationshipPoints points = new RelationshipPoints();
System.out.println("\n\t*** TEXT_GAME: FIRSTDATE ***\n");
System.out.println("-You can exit the game at any time by typing 'exit'.-\n\n");
while (true) {
user = scanner.nextLine();
switch (question) {
case "4_Story2":
if (user.trim().equalsIgnoreCase("exit")) {
System.exit(1);
break;
}
switch (user) {
case "1":
System.out.println("\n\nIan is overjoyed!");
relationshipPoints++;
gameSave.savePoints(relationshipPoints);
gameSave.saveGame("4B");
question = "4B";
break;
case "2": [...]
GameSave
public class GameSave {
public static Properties prop = new Properties();
public void savePoints(int points) {
try {
prop.setProperty("Save_RPS", String.valueOf(points));
prop.store(new FileOutputStream("config.prop"), null);
} catch (IOException e) {
}
}
public void saveGame(String point) {
try {
prop.setProperty("Save_Point", point);
prop.store(new FileOutputStream("config.prop"), null);
} catch (IOException e) {
}
}
public int loadPoints() {
int p = 0;
String line = "";
try {
prop.load(new FileInputStream("config.prop"));
line = prop.getProperty("Save_RPS");
} catch (IOException e) {
try {
prop.setProperty("Save_RPS", String.valueOf(p));
prop.store(new FileOutputStream("config.prop"), null);
try {
prop.load(new FileInputStream("config.prop"));
line = prop.getProperty("Save_RPS");
} catch (IOException exe) {
}
} catch (IOException ex) {
}
}
p = Integer.parseInt(line);
return p;
}
public String loadGame() {
String line = "";
try {
prop.load(new FileInputStream("config.prop"));
line = prop.getProperty("Save_Point");
} catch (IOException e) {
try {
prop.setProperty("Save_Point", "0");
prop.store(new FileOutputStream("config.prop"), null);
try {
prop.load(new FileInputStream("config.prop"));
line = prop.getProperty("Save_Point");
} catch (IOException exe) {
}
} catch (IOException ex) {
}
}
return line;
}
public void newGame() {
try {
prop.setProperty("Save_Point", "0");
prop.store(new FileOutputStream("config.prop"), null);
} catch (IOException e) {
}
}
}