游戏设置文件问题

发布于 2024-12-17 05:08:35 字数 1859 浏览 4 评论 0原文

您好,我正在开发一个设置加载器系统,但它不想工作,我尝试了不同的方法,这个看起来最好,除了它不起作用,我可以请一些帮助吗,因为我需要完成这项工作到一天结束时。

这是代码:

public GameSettings getGameSettings() throws Exception
{
    Resolution GameResolution;
    GameSettings gs = new GameSettings(true, true, false, 50, new Resolution(800, 600));

    for(int k = 0; k < lines.size(); k++)
    {
        String[] s1 = lines.get(k).split("=");

        if(s1[0].equals("volume"))
        {
            gs.setVolume(Integer.parseInt(s1[1]));
        }
        if(s1[0].equals("musicOn"))
        {
            gs.setMusicOn(Boolean.parseBoolean(s1[1]));
        }
        if(s1[0].equals("soundOn"))
        {
            gs.setSoundOn(Boolean.parseBoolean(s1[1]));
        }
        if(s1[0].equals("aOpenGL"))
        {
            gs.setAOpenGL(Boolean.parseBoolean(s1[1]));
        }
        if(s1[0].equals("GameRes"))
        {
            String[] s2 = s1[1].split("x");
            GameResolution = new Resolution(Integer.parseInt(s2[1]), Integer.parseInt(s2[1]));
            gs.setGameResolution(GameResolution);
        }
        if(s1[0].startsWith("#"))
        {
            continue;
        }
        else
        {
            System.err.println("WARNING: Unknow setting in settings file ('" + s1[0] + "')");
            continue;
        }
    }

    return gs;
}

,这是“settings.dat”文件内容:

#************************** Settings
#Settings file version 1.5
musicOn=false
volume=75
soundOn=true
aOpenGL=false
GameRes=1000x800
#end of settings file

最后是控制台输出:

WARNING: Unknow setting in settings file ('musicOn')
WARNING: Unknow setting in settings file ('volume')
WARNING: Unknow setting in settings file ('soundOn')
WARNING: Unknow setting in settings file ('aOpenGL')
WARNING: Unknow setting in settings file ('GameRes')

Hi I'm working on a settings loader system and it doesn't want to work, I have tried different ways of doing it and this one looks the best, except it not working, can I please have some help because I need this done by the end of the day.

Here is the code:

public GameSettings getGameSettings() throws Exception
{
    Resolution GameResolution;
    GameSettings gs = new GameSettings(true, true, false, 50, new Resolution(800, 600));

    for(int k = 0; k < lines.size(); k++)
    {
        String[] s1 = lines.get(k).split("=");

        if(s1[0].equals("volume"))
        {
            gs.setVolume(Integer.parseInt(s1[1]));
        }
        if(s1[0].equals("musicOn"))
        {
            gs.setMusicOn(Boolean.parseBoolean(s1[1]));
        }
        if(s1[0].equals("soundOn"))
        {
            gs.setSoundOn(Boolean.parseBoolean(s1[1]));
        }
        if(s1[0].equals("aOpenGL"))
        {
            gs.setAOpenGL(Boolean.parseBoolean(s1[1]));
        }
        if(s1[0].equals("GameRes"))
        {
            String[] s2 = s1[1].split("x");
            GameResolution = new Resolution(Integer.parseInt(s2[1]), Integer.parseInt(s2[1]));
            gs.setGameResolution(GameResolution);
        }
        if(s1[0].startsWith("#"))
        {
            continue;
        }
        else
        {
            System.err.println("WARNING: Unknow setting in settings file ('" + s1[0] + "')");
            continue;
        }
    }

    return gs;
}

, this is the 'settings.dat' file content:

#************************** Settings
#Settings file version 1.5
musicOn=false
volume=75
soundOn=true
aOpenGL=false
GameRes=1000x800
#end of settings file

and finally the console output:

WARNING: Unknow setting in settings file ('musicOn')
WARNING: Unknow setting in settings file ('volume')
WARNING: Unknow setting in settings file ('soundOn')
WARNING: Unknow setting in settings file ('aOpenGL')
WARNING: Unknow setting in settings file ('GameRes')

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

潇烟暮雨 2024-12-24 05:08:35

您需要将这些 ifelse 链接起来。

if (str.equals("a")) {
  ...
} else if (str.equals("b")) {
  ...
} else {
  // failed to match anything
}

否则,每次循环运行时都会对所有 if 进行求值,因此对于所有非注释行都会显示 Unkown 消息,无论它们之前是否匹配某些内容。

You need to chain those ifs with elses.

if (str.equals("a")) {
  ...
} else if (str.equals("b")) {
  ...
} else {
  // failed to match anything
}

Otherwise, all the ifs are evaluated each time the loop runs, so the Unkown message will show for all lines that are not comments, regardless of whether they matched something before or not.

清欢 2024-12-24 05:08:35

显然,在您的代码中,对于任何未注释的行,您都会打印一条警告。

我猜你实际上想每次都使用 else if 而不是 if,因为在你的代码中,最后的 else 指的是 if仅限以上。

Apparently, in your code for any non-commented line you print a warning.

I guess you actually wanted to use else if instead of if every time, as in your code the final else refers to the if above only.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文