簡體   English   中英

Java讀寫高分

[英]Java reading and writing High Scores

我目前正在為我的 Java class 編寫一個程序來玩豬游戲,但無法弄清楚如何在文件中讀取和寫入高分。 我想在這個程序上得到任何可能的幫助。 謝謝,代碼現在編譯得很好並且運行正常。 這是游戲的用戶與計算機代碼:這是我到目前為止的代碼:


public class ComputerPig
{
    //Scanner usersName;
    String usersName="";
    Boolean humanTurn = true;
    Boolean computerTurn = true;
    int dice;
    int humanTurnPoints, computerTurnPoints;
    int humanTotalPoints = 0;
    int computerTotalPoints = 0;
    private Scanner keyboard;
    private Scanner key;
    // System.out.print("Please enter your name: ");
    // usersName = new Scanner(System.in);
    // setStart(usersName.nextLine());
    public void roll()
    {
        dice = (int) (Math.random() * 6) + 1;
    }
    public int humanTurnScore()
    {
        {
            humanTurnPoints = dice + humanTurnPoints;
            System.out.println(usersName + "threw: " + dice);
            System.out.println(usersName + "have scored: " + humanTurnPoints + " in your turn.");
        }
        return humanTurnPoints;
    }
    public void humanTurnZero()
    {
        humanTurnPoints = 0;
    }
    public int computerTurnScore()
    {
        {
            computerTurnPoints = dice + computerTurnPoints;
            System.out.println("Computer has scored: " + computerTurnPoints + " in its turn.");
        }
        return computerTurnPoints;
    }
    public void computerTurnZero()
    {
        computerTurnPoints = 0;
    }
    public ComputerPig(String xUsersName)
    {
        usersName=xUsersName;
        humanGame();
        if (!humanTurn)
        {
            computerTurn();
        }
    }
    public int humanGame()
    {
        System.out.println("To start the game please press 'r'.");
        key = new Scanner(System.in);
        String start = key.nextLine();
        if (!start.equalsIgnoreCase("R"))
        {
            System.out.println("Make sure you are pressing 'r'.");
            humanGame();
        }
        if (start.equalsIgnoreCase("R"))
        {
            System.out.println(usersName + "pressed +  'r'.");
            System.out.println("Lets start.");
            do
            {
                roll();
                if (dice == 1)
                {
                    System.out.println(usersName + " got 1 and you lost your turn.");
                    System.out.println("Computer's GRAND TOTAL score is: " + computerTotalPoints);
                    humanTurnZero();
                    computerTurn();
                }
                else if (dice != 1)
                {
                    humanTotalPoints += dice;
                    if (humanTotalPoints >= 100)
                    {
                        System.out.println(usersName + " threw: " + dice);
                        System.out.println(usersName +" r GRAND TOTAL score is: " + humanTotalPoints);
                        System.out.println("Congratulations, you win!");
                        System.exit(0);
                    }
                    humanTurnScore();
                    System.out.println(usersName +" r GRAND TOTAL score is: " + humanTotalPoints);
                    System.out.println("Computer's GRAND TOTAL score is: " + computerTotalPoints);
                    System.out.println(usersName + " can hold or roll again.");
                    System.out.println("To roll again press 'r' or 'h' to hold.");
                    keyboard = new Scanner(System.in);
                    String choice = keyboard.nextLine();
                    if (choice.equalsIgnoreCase("R"))
                    {
                        System.out.println(usersName + " pressed 'r'.");
                        System.out.println("Lets roll again.");
                        roll();
                        if (!choice.equalsIgnoreCase("R"))
                        {
                            System.out.println(usersName + " didn't press 'r'. To make sure the program is running correctly please press 'r' to roll or 'h' to hold.");
                            humanGame();
                        }
                    }
                    if (choice.equalsIgnoreCase("h"))
                    {
                        System.out.println(usersName + " pressed 'h' and loose your turn.");
                        System.out.println(usersName +" r Grand total is: " + humanTotalPoints);
                        humanTurnZero();
                        computerTurn();
                    }
                }
            } while (humanTurn);
        }
        return dice;
    }
    public int computerTurn()
    {
        System.out.println("Now it's computer turn.");
        do
        {
            roll();
            if (dice != 1)
            {
                computerTotalPoints += dice;
                if (computerTotalPoints >= 100)
                {
                    System.out.println("Computer threw: " + dice);
                    System.out.println("Computer's GRAND TOTAL score is: " + computerTotalPoints);
                    System.out.println("Game Over! the computer wins");
                    System.exit(0);
                }
                System.out.println("Computer threw: " + dice);
                System.out.println("Computer's GRAND TOTAL score is: " + computerTotalPoints);
                System.out.println(usersName + "r Grand total is: " + humanTotalPoints);
                computerTurnScore();
                roll();
            }
            if (dice == 1)
            {
                System.out.println("Computer thrown 1 therefore it's your turn now.");
                computerTurnZero();
                humanGame();
            }
            if (computerTurnPoints >= 20)
            {
                System.out.println("Computer scored already " + computerTurnPoints + " you'd better start to focus.");
                System.out.println("Please play again");
                humanGame();
            }
        } while (computerTurn);
        return dice;
    }
    public static void main(String[] args)
    {
        System.out.println("Please enter your name:");
        Scanner vScanner = new Scanner(System.in);
        String vUserName=vScanner.nextLine();
        new ComputerPig(vUserName);
        vScanner.close(); 
        HighScore.main(null);

    }
}``` 




您可以使用純文本文件,或者您可以了解 JSON,更具體地說,是 GSON。

您希望將哪些信息具體存儲在文件中? 您甚至嘗試過實現這一目標嗎? 粘貼您的高分代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM