在学习Random类时我们制作过猜数字小游戏,学习了Properties类后,可以为此游戏添加保存得分记录功能
游戏规则:
玩家可以选择玩游戏还是查看得分
玩游戏:
首先玩家需要选择难度:
难度级别
范围
简单
1~10
中等
1~100
困难
1~1000
专家
1~10000
程序会根据随机生成一个难度随机生成一个整数。
玩家现在可以猜数,系统会判断猜大了还是猜小了。
最后根据玩家的尝试次数产生得分,并保存到文件中。
查看得分:
根据难度显示得分
制作步骤:
首先编写游戏主菜单方法:
public static void MainMenu() {
System.out.println("====主菜单====");
System.out.println("1.玩游戏");
System.out.println("2.查看得分");
System.out.println("0.返回");
}
选择难度方法:
public static voidChooseLevel() {
Scanner sc= newScanner(System.in);while(true) {
System.out.println("请选择难度:");
System.out.println("1.简单");
System.out.println("2.中等");
System.out.println("3.困难");
System.out.println("4.专家");switch(sc.next()) {//跟据难度传参
case "1":
NewGame(10);break;case "2":
NewGame(100);break;case "3":
NewGame(1000);break;case "4":
NewGame(10000);break;default:continue;
}break;
}
}
新游戏方法:
public static void NewGame(intmax) {
Scanner sc= newScanner(System.in);int number = newRandom().nextInt(max);
System.out.println("神秘数字已产生,(0~"+max+")");int count = 1;while(true) {
System.out.println("第"+count+"次猜:");int guess =sc.nextInt();if(guess>number) {
System.out.println("您猜大了");
count++;continue;
}else if(guess
System.out.println("您猜小了");
count++;continue;
}else{
System.out.println("您猜对了!一共猜了"+count+"次");if(updateFile(max,count)) {
System.out.println("新记录!");
}return;
}
}
}
更新得分文件
public static boolean updateFile(int type,intnum) {
FileInputStream fis= null;try{
fis= new FileInputStream("src\\practice\\scores");
}catch(FileNotFoundException e) {
e.printStackTrace();
}
Properties p= newProperties();try{
p.load(fis);
}catch(IOException e) {
e.printStackTrace();
}int oldscore =Integer.parseInt(p.getProperty(Integer.toString(type)));if(oldscore>num||oldscore==0) {
p.setProperty(Integer.toString(type), Integer.toString(num));
FileOutputStream fos= null;try{
fos= new FileOutputStream("src\\practice\\scores");
}catch(FileNotFoundException e) {
e.printStackTrace();
}try{
p.store(fos,null);
}catch(IOException e) {
e.printStackTrace();
}try{
fis.close();
fos.close();
}catch(IOException e) {
e.printStackTrace();
}return true;
}try{
fis.close();
}catch(IOException e) {
e.printStackTrace();
}return false;
}
重置得分
public static voidReset() {
FileOutputStream fos= null;try{
fos= new FileOutputStream("src\\practice\\scores");
}catch(FileNotFoundException e) {
e.printStackTrace();
}
Properties p= newProperties();
p.setProperty("10", "0");
p.setProperty("100", "0");
p.setProperty("1000", "0");
p.setProperty("10000", "0");try{
p.store(fos,null);
fos.close();
}catch(IOException e) {
e.printStackTrace();
}
}
获得得分:
public staticString getScore(String type) {
FileInputStream fis= null;try{
fis= new FileInputStream("src\\practice\\scores");
}catch(FileNotFoundException e) {
e.printStackTrace();
}
Properties p= newProperties();try{
p.load(fis);
}catch(IOException e) {
e.printStackTrace();
}try{
fis.close();
}catch(IOException e) {
e.printStackTrace();
}returnp.getProperty(type);
}
展示得分:
public static voidshowScores() {
Scanner sc= newScanner (System.in);while(true) {
System.out.println("得分记录:\n");
System.out.println("简单模式:"+(getScore("10").equals("0")?"暂无得分":getScore("10")));
System.out.println("中等模式:"+(getScore("100").equals("0")?"暂无得分":getScore("100")));
System.out.println("困难模式:"+(getScore("1000").equals("0")?"暂无得分":getScore("1000")));
System.out.println("专家模式:"+(getScore("10000").equals("0")?"暂无得分":getScore("10000")));
System.out.println("输入c重置得分,输入0返回");switch(sc.next()) {case "0":return;case "c":case "C":
Reset();break;
}
}
}
测试类:
public static voidmain(String[] args) {
Scanner sc= newScanner(System.in);while(true) {
MainMenu();switch(sc.next()) {case "1":
ChooseLevel();break;case "2":
showScores();break;case "0":return;default:continue;
}
}
}
运行效果: