简体   繁体   English

制作将分数写入Java文件的高分函数

[英]Making a highscore function that writes the score to a file in java

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;


public class GameRunner {

    public static void main(String[] args) {
        Scanner scan = new Scanner (System.in);
        System.out.println("Welcome to blackjack!\nEnter your name please.");

        String name = scan.nextLine();

        System.out.println("Here are your cards " + name);


        Deck theDeck = new Deck(1, true);

        if (mySum > dealerSum && mySum <= 21 || dealerSum > 21){
            System.out.println("You win!");
        }//if
        else if (mySum == dealerSum) {
            System.out.println("It's a draw!");
        }//else
        else {
            System.out.println("Dealer wins!");
        }

    }//main

    public void highScore() throws IOException{
        File myFile=new File("highscore.txt");
        FileWriter fw=new FileWriter(myFile, true);
        String end= "You have beaten the dealer " + "times.";
        fw.write(end);
        fw.close();

    }

}

This isn't all of my code, but I want it to count the number of times the user wins and stop when the dealer wins, and then write the number, which is their high score, into a file. 这不是我的全部代码,但是我希望它计算用户获胜的次数,并在发牌人获胜时停止,然后将其最高得分的数字写入文件中。 Also, my file is not showing up when I refresh or run my program and I have searched everywhere for it. 另外,当我刷新或运行程序时,我的文件没有显示,并且在各处搜索它。

Make sure your flush it. 确保冲洗。

fw.flush();

As well as you create the .txt before hand. 以及您之前创建的.txt文件。

myFile.createFile();

Read this please, http://docs.oracle.com/javase/tutorial/essential/io/file.html 请阅读此内容, http://docs.oracle.com/javase/tutorial/essential/io/file.html

The highScore function seems to save "something". highScore函数似乎可以保存“内容”。 But you never actually call it. 但是您从来没有真正称呼它。 And even if you call it, it will write only this line in it: 即使您调用它,它也只会在其中写以下行:

You have beaten the dealer times. 您已经击败了经销商。

It doesn't make sense. 这没有道理。 It looks as if you want to write a number N in "beaten the dealer N times", but you're not doing that. 好像您想在“打败经销商N次”中写一个数字N一样,但是您没有这样做。 As such, this code looks incomplete, as if you stopped writing it in the middle. 因此,此代码看起来不完整,就像您停止在中间编写代码一样。

If you do call this method, then a file will be created. 如果你调用此方法,则文件将被创建。 Since in new File("highscore.txt"); 由于在new File("highscore.txt"); you used the relative path "highscore.txt" , the file will be created in the execution directory. 您使用相对路径"highscore.txt" ,该文件将在执行目录中创建。 If you run this from an IDE, then the execution directory is typically the project's directory. 如果从IDE运行此文件,则执行目录通常是项目的目录。 Another alternative is to use an absolute path, for example "c:/highscore.txt" , then you can definitely easily find it. 另一种选择是使用绝对路径,例如"c:/highscore.txt" ,那么您绝对可以轻松找到它。

Above stated answer is absolutely right but u can also save it statically nd just increase the counter number if u want to show how many times he has bitten by using simple char at function. 上面所说的答案是绝对正确的,但是您也可以静态保存它,并且如果您想通过使用函数中的简单字符来显示自己被咬的次数,则可以增加计数器的数量。 Nd clearing the file text with new text or just by replacing the number by replace function of string Nd清除文件文本为新文本或仅通过替换字符串功能替换数字

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM