Java百分比和标题

Java百分比和标题,java,Java,嗨,我目前正在用Java开发一个游戏,它测试用户在没有音乐的情况下更好地工作的能力。我正在使用write文件并创建了一个.txt文件来保存结果。我不确定如何为保存的结果创建标题,例如“名字:第二个名字”。我也有点不知所措,无法从结果中计算出一个百分比。结果存储在变量Score和Score 2中,我只想显示结果的百分比。多谢各位 if(Globals.score2 > Globals.score) { //Better with music MessageBox.infoBo

嗨,我目前正在用Java开发一个游戏,它测试用户在没有音乐的情况下更好地工作的能力。我正在使用write文件并创建了一个.txt文件来保存结果。我不确定如何为保存的结果创建标题,例如“名字:第二个名字”。我也有点不知所措,无法从结果中计算出一个百分比。结果存储在变量Score和Score 2中,我只想显示结果的百分比。多谢各位

if(Globals.score2 > Globals.score) {
    //Better with music
    MessageBox.infoBox("You scored " + Globals.score + " without music & " + Globals.score2 + " with music!\n" + "You seemed to concentrate better while music was playing!" , "Score");
} else if (Globals.score2 < Globals.score) {
    //Better Without Music
    MessageBox.infoBox("You scored " + Globals.score + " without music & " + Globals.score2 + " with music!\n" + "You seemed to concentrate better without any music playing!" , "Score");
} else {
    //Equal Reults
    MessageBox.infoBox("You scored " + Globals.score +  " without music & " + Globals.score2 + " with music!\n" + "Your concentration stayed the same through each play through!" , "Score");
}  
// JOptionPane.showMessageDialog(frame, "Your score for game one was" + "" + Globals.score2 );
new FinalScore().setVisible(true);
this.dispose();
AudioPlayer.player.stop(as);

toolkit.beep();
timer.cancel(); //Not necessary because we call System.exit
//System.exit(0); //Stops the AWT thread (and everything else)

所以基本上你会问两件事:

如何计算分数的百分比? 你并没有说什么百分比,但我假设你想从
score+score2
计算
score
的分数(与
score2
类似)

我假设
Globals.score
Globals.score2
都是
double
类型。因此,该百分比将计算为:

double scorePercentage = 100 * Globals.score / (Globals.score + Globals.score2)
如何在保存游戏结果之前打印标题“行”? 嗯。。。与保存游戏结果一样。它甚至更容易,因为标题是静态已知的

但是,如果我没有弄错的话,创建一个单独的
BufferedWriter
来打印标题,结果不会附加到文件中,而是覆盖以前的输出。我的建议是实例化
BufferedWriter
,作为
WriteFile
初始化的一部分。
WriteFile
类可以如下所示:

public final class WriteFile {

    private WriteFile(){/* This is a class with only static methods - make the constructor private to prohibit instantiation */}

    public static final String heading = "UserID, First Name, Last Name, Age, Ethnic, Gender, Score Game 1, Score Game 2";

    private static BufferedWriter bufferedFileWriter = null;

    static {
        File file = new File("ResultsSaves.txt");

        // if file exists, then overwrite current saved file with new
        if (!file.exists()) {
            file.createNewFile();
        }

        FileWriter fw = new FileWriter(file.getAbsoluteFile(), true);
        bufferedFileWriter = new BufferedWriter(fw);
    }

    private static String getGameResult() {
        return Globals.IDUser + ", " + Globals.firstName + ", " + Globals.lastName + ", " + Globals.userAge + ", " + Globals.eThnic + ", " + Globals.maleOrFem  + ", " + " In the first game you scored, " + Globals.score + ", " + " In the second game you scored." + Globals.score2; 
    }

    /** Not really needed, but there to stay true to the original design */
    public static void printHeading() throws IOException {
        save(heading); 
    }

    /** Not really needed, but there to stay true to the original design */
    public static void saveGame() throws IOException {
        save(getGameResult()); 
    }

    public static void save(String content) throws IOException {

        bufferedFileWriter.write(content);
        bufferedFileWriter.newLine();
        // flush to get the contents stored to file
        bufferedFileWriter.flush();  

        JOptionPane.showMessageDialog(null, "Your details have been saved!");   

        }
    }

    /** Don't close the writer before all desired content is written. */
    public static void close() throws IOException {
        bufferedFileWriter.close(); 
    }
}


友好建议:使用全局变量时要小心。它们可能会成为调试的噩梦。您应该更喜欢将它们作为参数传递给方法

那么写入文件的代码在哪里呢?或者,您的问题是如何用Java编写文件,在这种情况下,可能已经有很多答案,例如,@KuluLimpa Hi,我已经添加了用于提取到上面文本文件的代码。
public final class WriteFile {

    private WriteFile(){/* This is a class with only static methods - make the constructor private to prohibit instantiation */}

    public static final String heading = "UserID, First Name, Last Name, Age, Ethnic, Gender, Score Game 1, Score Game 2";

    private static BufferedWriter bufferedFileWriter = null;

    static {
        File file = new File("ResultsSaves.txt");

        // if file exists, then overwrite current saved file with new
        if (!file.exists()) {
            file.createNewFile();
        }

        FileWriter fw = new FileWriter(file.getAbsoluteFile(), true);
        bufferedFileWriter = new BufferedWriter(fw);
    }

    private static String getGameResult() {
        return Globals.IDUser + ", " + Globals.firstName + ", " + Globals.lastName + ", " + Globals.userAge + ", " + Globals.eThnic + ", " + Globals.maleOrFem  + ", " + " In the first game you scored, " + Globals.score + ", " + " In the second game you scored." + Globals.score2; 
    }

    /** Not really needed, but there to stay true to the original design */
    public static void printHeading() throws IOException {
        save(heading); 
    }

    /** Not really needed, but there to stay true to the original design */
    public static void saveGame() throws IOException {
        save(getGameResult()); 
    }

    public static void save(String content) throws IOException {

        bufferedFileWriter.write(content);
        bufferedFileWriter.newLine();
        // flush to get the contents stored to file
        bufferedFileWriter.flush();  

        JOptionPane.showMessageDialog(null, "Your details have been saved!");   

        }
    }

    /** Don't close the writer before all desired content is written. */
    public static void close() throws IOException {
        bufferedFileWriter.close(); 
    }
}