zoukankan      html  css  js  c++  java
  • Java基础知识强化之IO流笔记70:Properties练习之 如何让猜数字小游戏只能玩5次的案例

    1. 使用Properties完成猜数字小游戏只能玩5次的案例:

    2. 代码实现:

    (1)猜数字游戏GuessNumber

     1 package cn.itcast_08;
     2 
     3 import java.util.Scanner;
     4 
     5 /**
     6  * 这是猜数字小游戏
     7  * 
     8  * @author 风清扬
     9  * @version V1.1
    10  * 
    11  */
    12 public class GuessNumber {
    13     private GuessNumber() {
    14     }
    15 
    16     public static void start() {
    17         // 产生一个随机数
    18         int number = (int) (Math.random() * 100) + 1;
    19 
    20         // 定义一个统计变量
    21         int count = 0;
    22 
    23         while (true) {
    24             // 键盘录入一个数据
    25             Scanner sc = new Scanner(System.in);
    26             System.out.println("请输入数据(1-100):");
    27             int guessNumber = sc.nextInt();
    28 
    29             count++;
    30 
    31             // 判断
    32             if (guessNumber > number) {
    33                 System.out.println("你猜的数据" + guessNumber + "大了");
    34             } else if (guessNumber < number) {
    35                 System.out.println("你猜的数据" + guessNumber + "小了");
    36             } else {
    37                 System.out.println("恭喜你," + count + "次就猜中了");
    38                 break;
    39             }
    40         }
    41     }
    42 }

    (2)测试类PropertiesTest2,如下:

     1 package cn.itcast_08;
     2 
     3 import java.io.FileReader;
     4 import java.io.FileWriter;
     5 import java.io.IOException;
     6 import java.io.Reader;
     7 import java.io.Writer;
     8 import java.util.Properties;
     9 
    10 /*
    11  * 我有一个猜数字小游戏的程序,请写一个程序实现在测试类中只能用5次,超过5次提示:游戏试玩已结束,请付费。
    12  */
    13 public class PropertiesTest2 {
    14     public static void main(String[] args) throws IOException {
    15         // 读取某个地方的数据,如果次数不大于5,可以继续玩。否则就提示"游戏试玩已结束,请付费。"
    16         // 创建一个文件
    17         // File file = new File("count.txt");
    18         // if (!file.exists()) {
    19         // file.createNewFile();
    20         // }
    21 
    22         // 把数据加载到集合中
    23         Properties prop = new Properties();
    24         Reader r = new FileReader("count.txt");
    25         prop.load(r);
    26         r.close();
    27 
    28         // 我自己的程序,我当然知道里面的键是谁
    29         String value = prop.getProperty("count");
    30         int number = Integer.parseInt(value);
    31 
    32         if (number > 5) {
    33             System.out.println("游戏试玩已结束,请付费。");
    34             System.exit(0);
    35         } else {
    36             number++;
    37             prop.setProperty("count", String.valueOf(number));
    38             Writer w = new FileWriter("count.txt");
    39             prop.store(w, null);
    40             w.close();
    41 
    42             GuessNumber.start();
    43         }
    44     }
    45 }

    运行5次之后,就会出现,如下的结果:

  • 相关阅读:
    头文件里面的ifndef /define/endif的作用
    互联网小知识
    IT技术开发人员获得成功的六大步骤
    ab网站压力测试
    微软校招试题
    C程序编译过程浅析
    makefile变量赋值
    printf的题目
    Oracle命名规范
    javascript(js)小数精度丢失的解决方案
  • 原文地址:https://www.cnblogs.com/hebao0514/p/4877624.html
Copyright © 2011-2022 走看看