当前位置:网站首页>Guessing game (read data from file)
Guessing game (read data from file)
2022-07-07 23:07:00 【Anny Linlin】
Guess the game : There are three chances to guess numbers , If you use up three opportunities, you need to recharge the website . If you haven't used up three chances, you can continue to play . The number of games played is stored in the file .
Ideas :( To be added )
import java.util.Random;
import java.util.Scanner;
public class GuessNumber {
public GuessNumber() {
}
static void guess() {
Random r = new Random();
int rNum = r.nextInt(100);
while (true) {
System.out.println(" Please enter an integer :");
Scanner scan = new Scanner(System.in);
int num = scan.nextInt();
scan.nextLine();
if (num < rNum) {
System.out.println(" Guess a little !");
} else if (num > rNum) {
System.out.println(" Guess the !");
} else {
System.out.println(" You guessed it !");
break;
}
}
}
}
```java
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Properties;
public class Test {
public static void main(String[] args) throws IOException {
// Read data from file to properties Collection , use load Method realization
Properties p = new Properties();
FileReader fr = new FileReader("count.txt");
p.load(fr);
fr.close();
// adopt properties Collection to get data
String s = p.getProperty("count");
int count = Integer.parseInt(s);
System.out.println(count);
if (count >= 3) {
System.out.println(" You have used up 3 Second chance !");
} else {
GuessNumber.guess();
count++;
}
// take count write file
FileWriter fw = new FileWriter("count.txt");
Properties pw = new Properties();
pw.setProperty("count", String.valueOf(count));
pw.store(fw, null);
fw.close();
}
}
边栏推荐
- 详解全志V853上的ARM A7和RISC-V E907之间的通信方式
- LeetCode707. Design linked list
- Unity technical notes (II) basic functions of scriptableobject
- 定位到最底部[通俗易懂]
- 面试百问:如何测试App性能?
- Are the microorganisms in the intestines the same as those on the skin?
- How to operate DTC community?
- 微生物健康网,如何恢复微生物群落
- 行测-图形推理-9-线条问题类
- 0-5VAC转4-20mA交流电流隔离变送器/转换模块
猜你喜欢

小程序多种开发方式对比-跨端?低代码?原生?还是云开发?

详解全志V853上的ARM A7和RISC-V E907之间的通信方式

今日创见|企业促进创新的5大关键要素

Knowledge drop - PCB manufacturing process flow

行测-图形推理-3-对称图形类

Brush question 4

Why is network i/o blocked?

行测-图形推理-1-汉字类

微生物健康网,如何恢复微生物群落
Apple further entered the financial sector through the 'virtual card' security function in IOS 16
随机推荐
Are the microorganisms in the intestines the same as those on the skin?
Line test - graphic reasoning -5- one stroke class
每日一题——PAT乙级1002题
Use JfreeChart to generate curves, histograms, pie charts, and distribution charts and display them to JSP-1
Redis集群安装
行测-图形推理-6-相似图形类
2021-01-12
Debezium series: set role statement supporting mysql8
微服務遠程Debug,Nocalhost + Rainbond微服務開發第二彈
Microbial health network, how to restore microbial communities
Basic knowledge of binary tree
关于海康ipc的几个参数
7-51 combination of two ordered linked list sequences
Line test - graphic reasoning - 4 - alphabetic class
QT graphicsview graphical view usage summary with flow chart development case prototype
How to operate DTC community?
Software evaluation center ▏ what are the basic processes and precautions for automated testing?
[language programming] exe virus code example
PCL .vtk文件与.pcd的相互转换
Line test - graphic reasoning - 1 - Chinese character class