Properties的使用

该程序实现了一个猜数字游戏,用户需在指定次数内猜出随机数。同时,通过读写文件来管理试玩机会,当次数用完时提示用户充值。程序包含错误检查,确保输入有效,并提供相应反馈。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public static void plays() throws IOException {
    Random random = new Random();
    int num = random.nextInt(100);
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
    int s = -1;
    int n = 0;
    while (num != s) {
        System.out.print("请输入数字:");
        n++;
        s = Integer.parseInt(bufferedReader.readLine());
        if (s > num) {
            System.out.println("你输入的数字太大了");
        }else if (s < num){
            System.out.println("你输入的数字太小了");
        }

    }
    System.out.println("恭喜你猜中了,你一共猜了" + n + "次");
}


public static void count() throws IOException {

    Properties prop = new Properties();

    prop.load(new FileReader("D:\\IEAD\\data\\untitled\\count.txt"));
    int count = Integer.parseInt(prop.getProperty("count"));
    if (count > 0) {
        plays();
        count--;
        prop.setProperty("count",String.valueOf(count));
        prop.store(new FileWriter("D:\\IEAD\\data\\untitled\\count.txt"),null);
        System.out.println("你还有" + count + "次,试玩机会。");
    }else {
        System.out.println("你的三次试用机会以用完,请到www.opt.ccc.com网站充值");
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值