栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java Propetties案例:猜数字游戏

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java Propetties案例:猜数字游戏

需求:猜数字游戏只能试玩三次,过后需充值才能继续
思路:1写一个游戏类,里面有一个猜数字的小游戏 2.写一个测试类;从文件中读取数据到Properties集合,用load
()方法实现;文件已经存在game.txt;里面有一个数据值count=0;通过Properties集合获取到玩游戏的次数;判断
次数是否到达3次,如果达到给出提示,如果没有,先玩,次数+1,重新写回文件,用Properties的store()方法实现

package com.io.properties;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;


public class PropertiesGameDemo {
    public static void main(String[] args) throws IOException {
        Properties pro = new Properties();
        FileReader fr = new FileReader("my\game.txt");
        pro.load(fr);
        fr.close();
        String count = pro.getProperty("count");
        int number = Integer.parseInt(count);
        if(number>3){
            System.out.println("试玩结束");
        }else {
            PropertiesGame.start();
            FileWriter fw = new FileWriter("my\game.txt");
            number++;
            pro.store(fw,null);
            fw.close();
        }
        
    }
}

猜数字方法

package com.io.properties;

import java.util.Random;
import java.util.Scanner;

//猜数字游戏
public class PropertiesGame {
    private PropertiesGame(){}
    public static void start(){
        Random rand = new Random();
        int number = rand.nextInt(100)+1;
        while (true){
            Scanner sc = new Scanner(System.in);
            System.out.println("输入你要猜的数字");
            int i = sc.nextInt();
            if(number>i){
                System.out.println("你猜的小了");
            }else if(number
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/295227.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号