标签:static iter int() rand store exce 取数 获取 实现
游戏类:
public class GuessNumber { public 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; } } } }
测试类:
public class PropertiesTest { public static void main(String[] args)throws IOException { //从文件中读取数据到Properties集合,用load()方法实现 Properties prop = new Properties(); FileReader fr = new FileReader("myFile\\game.txt"); prop.load(fr); fr.close(); //通过Properties集合获取玩游戏的次数 String count = prop.getProperty("count"); int number = Integer.parseInt(count); //判断次数是否到了3次 if (number >= 3){ System.out.println("游戏试玩已结束,想玩请充值"); }else{ GuessNumber.start(); //次数+1 number++; prop.setProperty("count",String.valueOf(number)); //重新写回文件 FileWriter fw = new FileWriter("myFile\\game.txt"); prop.store(fw,null); fw.close(); } } }
第一次运行:
第二次运行:
第三次运行:
第四次运行:
标签:static iter int() rand store exce 取数 获取 实现
原文地址:https://www.cnblogs.com/pxy-1999/p/12771170.html