您好我正在开发一款Java游戏,用于测试用户在没有音乐的情况下更好地工作以进行分析的能力。我正在使用写文件并创建了一个.txt文件来保存结果。我不确定如何为保存的结果创建标题,例如名字:第二名。我也有点迷失,从结果中计算一个百分比。结果存储在变量,分数和分数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)
//写入文件的代码
public class WriteFile {
public static void SaveGame() throws IOException {
String content = (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);
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);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(content);
bw.newLine();
bw.close();
JOptionPane.showMessageDialog(null, "Your details have been saved!");
}
}
}
答案 0 :(得分:0)
所以基本上你会问两件事:
你并没有真正说出一定比例,但我想你要计算score
score + score2
的分数(和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();
}
}
友好建议:使用全局变量时要小心。他们可能成为调试噩梦的地狱。您应该更喜欢将它们作为参数传递给您的方法。