当前位置:网站首页>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();
}
}
边栏推荐
- Transparent i/o model from beginning to end
- Debezium系列之:支持 mysql8 的 set role 語句
- Digital collections accelerated out of the circle, and marsnft helped diversify the culture and tourism economy!
- DTC社群运营怎么做?
- What is fake sharing after filling the previous hole?
- Txt file virus
- Brush question 4
- 【测试面试题】页面很卡的原因分析及解决方案
- Unity与WebGL的相爱相杀
- LeetCode203. Remove linked list elements
猜你喜欢
Anta DTC | Anta transformation, building a growth flywheel that is not only FILA
行测-图形推理-6-相似图形类
0-5vac to 4-20mA AC current isolated transmitter / conversion module
Unity FAQ (I) lack of references
小程序多种开发方式对比-跨端?低代码?原生?还是云开发?
微生物健康網,如何恢複微生物群落
The wonderful relationship between message queue and express cabinet
Knowledge drop - PCB manufacturing process flow
Yarn cannot view the historical task log of yarn after enabling ACL user authentication. Solution
「开源摘星计划」Loki实现Harbor日志的高效管理
随机推荐
XMIND mind mapping software sharing
Some parameters of Haikang IPC
「开源摘星计划」Loki实现Harbor日志的高效管理
PCL .vtk文件与.pcd的相互转换
Debezium series: introducing support for the final operator
Class implementation of linear stack and linear queue (another binary tree pointer version)
Quick sort (diagram +c code)
软件测评中心▏自动化测试有哪些基本流程和注意事项?
What is ADC sampling rate (Hz) and how to calculate it
iNFTnews | NFT技术的广泛应用及其存在的问题
Personal statement of testers from Shuangfei large factory: is education important for testers?
今日创见|企业促进创新的5大关键要素
Develop those things: go plus c.free to free memory, and what are the reasons for compilation errors?
Line test - graphic reasoning - 1 - Chinese character class
2021-01-12
Debezium系列之:支持 mysql8 的 set role 語句
ASP. Net core introduction V
Digital transformation: five steps to promote enterprise progress
Software test classification
Debezium series: binlogreader for source code reading