当前位置:网站首页>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();
}
}
边栏推荐
- Microservice Remote debug, nocalhost + rainbond microservice Development second Bomb
- 软件测评中心▏自动化测试有哪些基本流程和注意事项?
- Debezium系列之:支持 mysql8 的 set role 语句
- 行测-图形推理-2-黑白格类
- Common verification rules of form components -2 (continuously updating ~)
- 行测-图形推理-9-线条问题类
- 开发那些事儿:Go加C.free释放内存,编译报错是什么原因?
- Yarn开启ACL用户认证之后无法查看Yarn历史任务日志解决办法
- Transparent i/o model from beginning to end
- 2021-01-12
猜你喜欢
Talk about DART's null safety feature
ASEMI整流桥KBPC1510的型号数字代表什么
行测-图形推理-8-图群类
Line test - graphic reasoning - 4 - alphabetic class
LeetCode144. Preorder traversal of binary tree
LeetCode203. Remove linked list elements
聊聊 Dart 的空安全 (null safety) 特性
Brush question 4
肠道里的微生物和皮肤上的一样吗?
微服務遠程Debug,Nocalhost + Rainbond微服務開發第二彈
随机推荐
行测-图形推理-8-图群类
Apple further entered the financial sector through the 'virtual card' security function in IOS 16
Locate to the bottom [easy to understand]
Knowledge drop - PCB manufacturing process flow
行测-图形推理-9-线条问题类
Class implementation of linear stack and linear queue (another binary tree pointer version)
每日一题——PAT乙级1002题
CTF练习
Txt file virus
Debezium系列之:支持 mysql8 的 set role 语句
一次搞明白 Session、Cookie、Token,面试问题全稿定
【测试面试题】页面很卡的原因分析及解决方案
iNFTnews | NFT技术的广泛应用及其存在的问题
Cascade-LSTM: A Tree-Structured Neural Classifier for Detecting Misinformation Cascades-KDD2020
Line test - graphic reasoning - 3 - symmetric graphic class
Unity and webgl love each other
开发那些事儿:Go加C.free释放内存,编译报错是什么原因?
ASP.NET Core入门五
Ligne - raisonnement graphique - 4 - classe de lettres
详解全志V853上的ARM A7和RISC-V E907之间的通信方式