java猜数字小游戏

package test;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;
import java.util.Random;
import java.util.Scanner;

/**

  • 2:我有一个猜数字小游戏的程序,请写一个程序实现在测试类中只能用5次,超过5次提示:游戏试玩已结束,请付费。
    */

public class OtherIO8{
public static void main(String[] args) throws IOException {
Properties prop=new Properties();
prop.load(new FileReader(“playgame.txt”));
int count=Integer.parseInt(prop.getProperty(“count”));
if(count>0){
System.out.println(“欢迎试玩,你的试玩机会还有”+count+“次”);
if(playGame()){
System.out.print(“你真厉害,奖励你一次试玩机会!”);
}else{
System.out.print(“你输了,扣你一次机会!”);
count–;
}
System.out.println(“你的试玩机会还有”+count+“次”);
prop.setProperty(“count”, String.valueOf(count));
prop.store(new FileWriter(“playgame.txt”), “GameConuts”);
}else{
System.out.println(“游戏试玩已结束,请付费!”);
}
}
public static boolean playGame(){
Random random=new Random(System.currentTimeMillis());
int sum=random.nextInt(100)+1;
int scan=0;
Scanner sc=new Scanner(System.in);
System.out.println(“游戏开始了!”);
System.out.println(“游戏规则,输入1-100的整数,幸运数字为1-100的随机数,你一共有五次机会!”);
System.out.println(“输入你要猜的数字:”);
for(int i=1;i<=5;i++){
if(i5){
System.out.print(“你只有一次机会了!愿你赌神附体!,请输入:”);
scan=sc.nextInt();
if(scan
sum){
System.out.println("!!!猜对了!!!恭喜你!!!");
return true;
}else{
System.out.println("!很遗憾,下次吃鸡吧!");
return false;
}
}else{
System.out.print(“第”+i+“次机会:”);
scan=sc.nextInt();
if(scan>sum){
System.out.println(“猜的有点大”);
}else if(scan<sum){
System.out.println(“猜的有点小”);
}else{
System.out.println("!!!大吉大利,今晚吃鸡!!!");
return true;
}
}
}
sc.close();
return false;
}
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值