Java读写高分

标签 java

我目前正在为 Java 类编写一个程序来玩 pig 游戏,但不知道如何在文件中读取和写入高分。我希望获得有关此计划的任何可能的帮助。谢谢你!代码现在编译得很好并且运行正常,这是游戏的用户与计算机代码。这是我到目前为止的代码:


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。

您具体希望在文件中存储哪些信息?您是否尝试过实现这一目标?粘贴您的高分代码。

关于Java读写高分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58616776/

相关文章:

java - 带有自定义 CacheStoreAdapter 的 Apache Ignite 可用性问题

java - Apache-Camel 控制台输入

Java编码问题

java - 如何获取 JDBC 连接 url 字符串

java - eclipse 方法参数中的 Final

java - 如何在桌面浏览器上使用 webdriver 移动网页

java - RetryTemplate 只执行一次

Java .split() 方法极端情况 : what are they?

java - 绘画以筛选参数对象的位置

java - 强制用户在 Java 中声明变量