java - Java读写高分
问题描述
我目前正在编写一个程序来为我的 Java 课程玩猪游戏,但无法弄清楚如何在文件中读取和写入高分。我想在这个程序上得到任何可能的帮助。谢谢你!代码现在编译得很好并且运行正常,这是游戏的用户与计算机代码。到目前为止,这是我的代码:
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。
您希望将哪些信息具体存储在文件中?您甚至尝试过实现这一目标吗?粘贴您的高分代码。
推荐阅读
- javascript - console.log 没有显示正确的值
- mysql - 排序顺序 laravel 雄辩关系
- swift - Swift 调试参数
- ipython - 在 IPython 中编辑多行代码
- reactjs - 如何将 Props 传递给 reactjs 处理程序?
- java - 骆驼收件人列表太长会导致 StackOverflow 错误
- vuejs2 - 我在 Vue 项目中的哪里添加我的 .eslintrc.js 文件?
- android - 创建 URL 时遇到问题。无法访问分支服务器,请稍后再试
- vim - "+ 注册不能在支持剪贴板的 vim 中工作
- cordova - 离子科尔多瓦文件传输插件未通过 HTTPS 在 android 中下载文件