猜数字游戏次数

猜数字游戏次数_第1张图片

 

 

 游戏类代码

package com.io.liushuaishuai;

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

public class GuessNumber {
    private GuessNumber() {
    }


    public static void start() {
        Random r = new Random();
        int number = r.nextInt(100) + 1;

        while (true) {
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入您要猜的数字");
            int guessNumber = sc.nextInt();
            //比较输入的数字和系统产生对的数据
            if (guessNumber > number) {
                System.out.println("您输入的数字" + guessNumber + "太大了");
            } else if (guessNumber < number) {
                System.out.println("您输入的数字" + guessNumber + "太小了");
            } else {
                System.out.println("恭喜您猜中了!!!!!");
                break;
            }
        }


    }

}

 测试类

package com.io.liushuaishuai;

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

import static com.io.liushuaishuai.GuessNumber.start;

public class PropertiesTest {
    public static void main(String[] args) throws IOException {
        //从文件读取数据到Properties,用load()方法实现
        Properties prop = new Properties();

        FileReader fr = new FileReader("myIOstream\\game.txt");
        prop.load(fr);
        fr.close();
        //通过集合获取玩游戏的次数
        String count = (String) prop.get("count");  或者//String count = prop.getProperty("count");
        int num = Integer.parseInt(count);
        //判断次数是否到了3次
        if (num >= 3) {
            System.out.println("游戏结束,想玩请充值");
        }else{
            start();
            num++;
            prop.setProperty("count",String.valueOf(num));
            FileWriter fw = new FileWriter("myIOstream\\game.txt");
            prop.store(fw,null);
            fw.close();
        }


    }
}

 

 game.txt中  count=0

转载于:https://www.cnblogs.com/lsswudi/p/11432063.html

你可能感兴趣的:(猜数字游戏次数)