当前位置:网站首页>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();
}
}
边栏推荐
- Debezium系列之:引入对 LATERAL 运算符的支持
- Unity dynamically merges mesh textures
- Line test - graphic reasoning -7- different graphic classes
- CTF练习
- Innovation today | five key elements for enterprises to promote innovation
- Microservice Remote debug, nocalhost + rainbond microservice Development second Bomb
- 0-5VAC转4-20mA交流电流隔离变送器/转换模块
- Circumvention Technology: Registry
- ASP. Net core introduction V
- QT graphicsview graphical view usage summary with flow chart development case prototype
猜你喜欢

Microbial Health Network, How to restore Microbial Communities

Talk about DART's null safety feature

Time convolution Network + soft threshold + attention mechanism to realize residual life prediction of mechanical equipment

ASP.NET Core入门五

Explain in detail the communication mode between arm A7 and risc-v e907 on Quanzhi v853
Apple further entered the financial sector through the 'virtual card' security function in IOS 16

Why is network i/o blocked?

Microbial health network, how to restore microbial communities

Transparent i/o model from beginning to end

Interview questions: how to test app performance?
随机推荐
面试百问:如何测试App性能?
Yarn开启ACL用户认证之后无法查看Yarn历史任务日志解决办法
Personal statement of testers from Shuangfei large factory: is education important for testers?
It's no exaggeration to say that this is the most user-friendly basic tutorial of pytest I've ever seen
定位到最底部[通俗易懂]
Line test - graphic reasoning - 1 - Chinese character class
Redis cluster installation
Anta DTC | Anta transformation, building a growth flywheel that is not only FILA
Quick sort (diagram +c code)
There is another problem just online... Warm
数据库每日一题---第22天:最后一次登录
QT graphicsview graphical view usage summary with flow chart development case prototype
Leetcode19. Delete the penultimate node of the linked list [double pointer]
Brush question 4
Line test - graphic reasoning - 4 - alphabetic class
每日一题——PAT乙级1002题
Unity与WebGL的相爱相杀
Unity technical notes (I) inspector extension
Txt file virus
微服務遠程Debug,Nocalhost + Rainbond微服務開發第二彈