Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在java游戏中保存玩家姓名和分数?_Java_Android_Save_Indexoutofboundsexception - Fatal编程技术网

如何在java游戏中保存玩家姓名和分数?

如何在java游戏中保存玩家姓名和分数?,java,android,save,indexoutofboundsexception,Java,Android,Save,Indexoutofboundsexception,我正在制作我的第一个android游戏。我想将用户名和分数保存到一个文件中,稍后再读取。我正在尝试使用arraylist存储5个用户对象。我不知道如何高效地写入文件并将其读回并存储在arraylist中。如果你知道另一种更好的方法,请告诉我。 这是我用来写作的方法 public static void save(FileIO file) { preferences = file.getSharedPref(); Editor editor = preferences.edit()

我正在制作我的第一个android游戏。我想将用户名和分数保存到一个文件中,稍后再读取。我正在尝试使用arraylist存储5个用户对象。我不知道如何高效地写入文件并将其读回并存储在arraylist中。如果你知道另一种更好的方法,请告诉我。 这是我用来写作的方法

public static void save(FileIO file) {
    preferences = file.getSharedPref();
    Editor editor = preferences.edit();
    for(int i=0;i<5;i++){
        editor.putString("username"+String.valueOf(i), User.userList.get(i).userName);
        editor.putInt("userscore"+String.valueOf(i), User.userList.get(i).highScore);
        }
    editor.commit();
}
publicstaticvoidsave(FileIO文件){
首选项=file.getSharedRef();
Editor=preferences.edit();

对于(int i=0;i当您希望存储值时:

SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(Your_Activity.this);

Editor editor = sp.edit();
editor.putString("username", "<username goes here>");
editor.putInt("score", <the score goes here>);
editor.commit();   // Do not forget this to actually store the values
“用户名”和“分数”是他们所尊重的价值观的关键

SharedReferences是存储值时想到的最简单的方法。 希望能有帮助。
~干杯。

当您希望存储值时:

SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(Your_Activity.this);

Editor editor = sp.edit();
editor.putString("username", "<username goes here>");
editor.putInt("score", <the score goes here>);
editor.commit();   // Do not forget this to actually store the values
“用户名”和“分数”是他们所尊重的价值观的关键

SharedReferences是存储值时想到的最简单的方法。 希望能有帮助。
~Cheers.

请提供有关具体问题的更详细信息。还要为编程语言和平台设置正确的标记。请提供有关具体问题的更详细信息。还要为编程语言和平台设置正确的标记。我第一眼看到的一些编码错误:-在read()中方法,您已经为(int i=0;iI)纠正了我的愚蠢错误。现在我在read()方法中得到indexoutofbounds错误您是否已将显示和读取循环更改为
for(int i=0;iit起作用了,但我认为它没有正确地保存和读取值。我在屏幕上得到了空白分数。我试图将两种方法的for循环中的条件更改为您建议的条件。强制关闭已消失,但它现在没有保存和读取值。为什么要使用FileIO文件?请尝试忽略此项,并使用m中所述的SharedReferencesy答案,我相信它会起作用。我第一眼看到的一些编码错误:-在read()方法中,你有'for(int I=0;我已经纠正了我的那个愚蠢错误。现在我在read()方法中遇到indexoutofbounds错误。你有没有将显示和读取循环更改为
(int i=0;iit起作用了,但我认为它没有正确地保存和读取值。我在屏幕上得到了空白分数。我试图将两种方法的for循环中的条件更改为您建议的条件。强制关闭已消失,但它现在没有保存和读取值。为什么要使用FileIO文件?请尝试忽略此项,并使用m中所述的SharedReferences答案是肯定的,我相信它会奏效。
String gameUsername = sp.getString("username", "default");
int gameScore = sp.getInt("score", 0);