//我有一个猜数字小游戏的程序,请写一个程序实现在测试类中只能用5次,超过5次提示:游戏试玩已结束,请付费。
主代码:
package cn.study_11;
import java.io.*;
import java.util.Properties;
//我有一个猜数字小游戏的程序,请写一个程序实现在测试类中只能用5次,超过5次提示:游戏试玩已结束,请付费。
public class GuessGameDemo {
public static void main(String[] args) throws IOException {
Properties prop=new Properties();
File file =new File("times.txt");
Reader r =new FileReader(file);
prop.load(r);
r.close();
int i = Integer.parseInt(prop.getProperty("times"));
if (i > 1)
{
System.out.println("试玩结束,交钱交钱");
System.exit(0);
}
else {
GuessNumber.star();
i++;
prop.setProperty("times",String.valueOf(i));
Writer w = new FileWriter(file);
prop.store(w,null);
w.close();
r.close();
}
}
}
猜字游戏的代码
package cn.study_11;
import java.util.Scanner;
/**
* 这是用户功能类,这里不花时间了,就写个猜数字游戏,
* 以后想要玩什么再添加相对应功能就好了
* @author zsp
* @version v1.1
* 现在这个是I/O文件控制版本了
*/
public class GuessNumber {
public static void star(){
int number =(int)((Math.random()*100)+1);
int i=0;
Scanner sc =new Scanner(System.in);
System.out.println("---------------猜数字-----------------");
System.out.println("请输入0-100");
while(true) {
int scan =sc.nextInt();
i++;
if (scan>number)
{
System.out.println("猜大了");
}
else if (scan<number)
{
System.out.println("猜小了");
}
else
{
System.out.println("你猜了"+i+"次终于猜中了");
System.out.println("恭喜你猜中了");
break;
}
}
}
}