需求:寫程式實作猜數小程式試玩3次,超過三次后如果還要玩要提示:試玩結束,請申請新的試玩資格
分析:
1.寫一個游戲類,里面有一個猜數小游戲
2.寫一個測驗類
A:從檔案中讀取資料到Properties集合,load,檔案已存在,game.txt,資料值:count=0
B:通過Properties集合獲取玩游戲的次數
C:判斷是否超過三次
是:提示試玩結束,請申請新的試玩資格
否:可以繼續玩,次數加一,重寫回檔案,store方法
游戲類
public class Game {
private Game(){}
public static void play(){
//生成一個亂數
Random r=new Random();
int num = r.nextInt(100)+1;
while (true){
Scanner sc=new Scanner(System.in);
System.out.println("請輸入你猜的數字");
int i=sc.nextInt();
if (i>100||i<0){
System.out.println("輸入的數字不在范圍內,請重新輸入");
i=sc.nextInt();
}
if (i>num){
System.out.println("你猜的大的");
}else if (i<num){
System.out.println("你猜的小了");
}else{
System.out.println("猜對了");
break;
}
?
}
?
}
}
?
?
測驗類
public class GameDemo {
public static void main(String[] args)throws IOException {
//從檔案中讀取資料到Properties集合,load,檔案已存在,abc.txt,資料值:count=0
Properties pt=new Properties();
FileReader fr=new FileReader("E:\\abc.txt");
pt.load(fr);
fr.close();
//:通過Properties集合獲取玩游戲的次數
String count = pt.getProperty("count");
//判斷是否超過三次
int i = Integer.parseInt(count);
if (i>3){
System.out.println("提示試玩結束,請申請新的試玩資格");
}else{
Game.play();
i++;
pt.setProperty("count",String.valueOf(i));
FileWriter fw=new FileWriter("E:\\abc.txt");
pt.store(fw,null);
fw.close();
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/458108.html
標籤:其他
上一篇:Jenkins + Jmeter + Ant 持續集成搭建
下一篇:資料結構 - 跳表
