java 做猜数字小游戏_Java Properties类:利用Properties类制作猜数字小游戏

在学习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;

}

}

}

运行效果:

5ed66b7d34b15e9668f0ec2e13a5e801.png

1ba816d6b795a8f132e63e817a036014.png

994129fcf09abe112e5111cb1e4a9a2d.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值