该小游戏的内容是让玩家猜数字,无论猜对猜错都算为一次,只能玩三次,三次过后就要充钱。
public static void main(String[] args) throws IOException {
//前期准备工作
Properties prop = new Properties();
FileReader fr = new FileReader("idea_test/test3.txt");
prop.load(fr);
int play_time = Integer.parseInt(prop.getProperty("count"));
int num,input;
//游戏开始
while(play_time<3){
Random r = new Random();
num = r.nextInt(30);
System.out.println("输入你猜的数字:");
Scanner sc = new Scanner(System.in);
input = sc.nextInt();
if(num == input)
System.out.println("猜中了!");
else
System.out.println("错了!数是"+num);
play_time ++;
if(play_time<3) {
System.out.println("是否再玩一次?\n1、是 2、否");
input = sc.nextInt();
if (input == 1)
continue;
else
break;
}else { //不能漏掉让玩家充钱的机会
System.out.println("玩家次数已达3次!你需要充钱才能继续玩");
break;
}
}
System.out.println("游戏结束!");
write_In(play_time,prop);
fr.close();
}
private static void write_In(int play_time,Properties prop)
throws IOException { //将数据写回本地
FileWriter fw = new FileWriter("idea_test/test3.txt");
prop.setProperty("count",String.valueOf(play_time));
prop.store(fw,null);
}