gpt4 book ai didi

java - 如何保存变量供以后访问?

转载 作者:行者123 更新时间:2023-12-01 17:15:13 25 4
gpt4 key购买 nike

我正在尝试制作一个基于文本的游戏,其中您有不同级别的生活不同方面和其他变量。我正在尝试将它们保存到某种文件中,以便稍后可以将它们取回。

到目前为止,这就是我对变量的了解:

public class baseLevels {
public boolean gameOver = false;

//100 is a normal level
public int happyLevel = 100;
public int sleepLevel = 100;
public int angerLevel = 100;
public int healthLevel = 100;
public int popularityLevel = 100;
public int smartLevel = 100;
public int weight = 100;

//0 = Single 1 = Not very close, but dating 2 = close and dating 3 = engaged 4 = married
public int relationshipStatus = 0;
public boolean children = false;
public int numberOfChildren = 0;

//Tests if the player is injured, 1-5 on severeness
public boolean injured = false;
public int severity = 1;
public boolean healthCare = false;

//Money status: 1 is poor, 5 is rich
public int money = 5000;
public int wealthStatus = 1;

//Family is alive or dead
public boolean momDead = false;
public boolean dadDead = false;
public boolean sisterDead = false;
public boolean brotherDead = false;
public boolean grandmaDead = false;
public boolean grandpaDead = false;

//Misc. variables
public boolean car = false;
public boolean house = false;
public boolean dead = false;
public boolean likeMusic = false;


}

看了某人发布的属性方法后,我喜欢它,但我需要一种将属性值设置为数字的方法。当我尝试这样做时,我收到一条错误消息,指出我不能拥有字符串和整数。

最佳答案

java.util.Properties对于这样简单的事情来说可能是最好的类(class)。它具有从文件加载和保存到文件的内置方法。

Properties props = new Properties();
props.load(new FileInputStream("data.props"));
String someProp = props.getProperty("myName");
props.setProperty("myName", "John Smith");
props.store(new PrintWriter("data.props"));

关于java - 如何保存变量供以后访问?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22582027/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com