极其简单的 Java IO 和枚举问题

发布于 2024-10-06 15:08:50 字数 986 浏览 0 评论 0原文

我知道我缺少一些非常简单的东西,但以下代码不会使用 println 写入文本:

import java.io.*;   
import javax.swing.JOptionPane;

public class ConfigureSettings{   
    private PrintWriter SettingsFile;   
    private enum Dodge{   
        Ascending, Descending, Off  
    };   
    private enum Damage{   
        Ascending, Descending, Off   
    };   
    private enum DPS{   
        Ascending, Descending, Off   
    };   

    ConfigureSettings(){
        try{
            SettingsFile = new PrintWriter(new File("UserSettings"));
        }
        catch(IOException e){
            JOptionPane.showMessageDialog(null, "Sorry cannot open UserSettings File");
        }
        SettingsFile.println("k");
        JOptionPane.showMessageDialog(null, "Done");
    }

    public static void main(String args[]){
        new ConfigureSettings();
    }
}

我的枚举问题是我可以创建枚举的枚举吗?我的意思是我可以将我拥有的三个当前枚举变量(Dodge Damage 和 DPS)包装在超级枚举类中吗?

PS我知道我的代码显示不正确,对未来的帖子/编辑有什么提示吗?

I know there is something dead simple I'm missing, but the following code won't write the text using println:

import java.io.*;   
import javax.swing.JOptionPane;

public class ConfigureSettings{   
    private PrintWriter SettingsFile;   
    private enum Dodge{   
        Ascending, Descending, Off  
    };   
    private enum Damage{   
        Ascending, Descending, Off   
    };   
    private enum DPS{   
        Ascending, Descending, Off   
    };   

    ConfigureSettings(){
        try{
            SettingsFile = new PrintWriter(new File("UserSettings"));
        }
        catch(IOException e){
            JOptionPane.showMessageDialog(null, "Sorry cannot open UserSettings File");
        }
        SettingsFile.println("k");
        JOptionPane.showMessageDialog(null, "Done");
    }

    public static void main(String args[]){
        new ConfigureSettings();
    }
}

My Enumeration question is can I create an Enumeration of Enumeration's? I mean can I wrap the three current enum variables I have, Dodge Damage and DPS, in Super enum class?

P.S. I know my code isn't displaying correctly, any hints for future posts/edits?

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

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

发布评论

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

评论(1

梦里兽 2024-10-13 15:08:50

关于缺少println输出:您是否在println之后尝试过flushPrintWriter ,并在不再需要时显式关闭它?

至于你的枚举的枚举问题 - 枚举的枚举有什么用处?你的意思是这样的吗(我刚刚编造的——我很确定它不会编译):

private enum Enumenum {
    enum Dodge {
        Ascending, Descending, Off
    },
    enum Damage {
    Ascending, Descending, Off
    },
    enum DPS {
        Ascending, Descending, Off
    }
}

你会用这样一个复杂的枚举做什么?为什么不简单地:

    enum OneForAll {
        Ascending, Descending, Off
    }

然后使用这个枚举而不是其他三个,因为它们都包含相同的枚举值?

(更新:但是请注意,使用相同的枚举而不是三个单独的枚举很可能会降低代码的类型安全性,因此上述建议可能不一定是一件好事。)

Concerning the missing println output: Have you tried flushing the PrintWriter after println, and explicitly closeing it when you no longer need it?

As to your enum of enums question — what would an enum of enums be good for? Do you mean something like this (which I just made up — I'm quite sure it won't compile):

private enum Enumenum {
    enum Dodge {
        Ascending, Descending, Off
    },
    enum Damage {
    Ascending, Descending, Off
    },
    enum DPS {
        Ascending, Descending, Off
    }
}

What would you do with such a complicated enum? Why not simply:

    enum OneForAll {
        Ascending, Descending, Off
    }

And then use this enum instead of the other three, since they all contain the same enumeration values anyway?

(Update: Note however that using the same enum instead of three separate enums might very well reduce the type safety of your code, so the above suggestion might not necessarily be a good thing.)

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