📄 26.txt
字号:
/**控制台应用程序开?知识点:
2 标准I/O操作,文件I/O操作
2 例外处理
2 程序流程控制
目标/功能要求:
定义一个基于控制台的游戏程序,实现下述功能:
1. 程序运行后,产生一个[0,100]之间的随机数,提示并接收用户从键盘输入的猜测值,返回比较信息(偏大或偏小),直至用户猜对为止,显示总的猜测次数。
2. 每次游戏结束时提示用户是否重新开始,根据用户输入确定重新开始或退出程序。
3. 将程序运行的最好记录(TOP1,成功猜测所用次数最少)的玩家信息登记到记录文件中,并适时更新。
4. 对用户可能通过键盘输入的非法格式的数据进行过滤处理,而不应出错退出。
5. 其他可能的功能。 /// 破纪录,保存在第一行,否则保存在文件结尾。
*/
import java.io.*;
import java.util.*;
public class GameTest {
public static void main (String args[])
{
NumGame game = new NumGame();
game.showGame(); // 显示进入游戏界面
String s = "";
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
do
{
game.setRandom(); // 设置 随机数
s = game.readString(br,0); // 读入用户数据,已去除前后空格
while(!s.equalsIgnoreCase("q")) // q 则放弃猜数
{
if(game.isValid(s)) // 合法性检查
{
if(game.isCorrect(Integer.parseInt(s))) // 评价
{
game.getResult();
break; // 猜对 则退出
}
}
s = game.readString(br,0);
}
game.saveRecord(s,br); // 保存 纪录
s = game.readString(br,1);
}while(!s.equalsIgnoreCase("y")); // y 程序退出,enter 继续下一轮猜测
}
}
class NumGame // 游戏包
{
int random; // 随机数
String name ; // 玩家名字
int record ; // 记录
int times ; // 计数器
int games; // 已玩人次
public NumGame()
{
this.name = "";
this.record = 0;
this.times = 0 ;
this.games= 1;
}
public void setRandom()
{
random=(int)(101*Math.random());
}
public String readString (BufferedReader br ,int i) // 读取用户输入 i==0猜数,i==1询问是否退出
{
if(i==0)
{
System.out.print("\n["+(++times)+"] 请输入一个[0,100]之间的整数,放弃[q] : ");
}
else
{
System.out.print("\n还想再玩吗?想[Enter],退出[y] : ");
times=0;
}
try
{
return br.readLine().trim(); // 用户输入,去除前后空格
}
catch (IOException e) {
return "";
}
}
public boolean isValid (String s) // 合法性检查
{
if(s.length()==0) { // length == 0 ;或含有非数字 is valid
System.out.print("<<非法输入>> ");
return false;
}
for (int i=0 ;i<s.length();i++)
if(s.charAt(i)<'0'|| s.charAt(i)>'9')
{
System.out.print("<<非法输入>> ");
return false;
}
return true;
}
public void showGame() // 进站画面,并且显示最高纪录
{
String[] str; //读取文件中最高纪录,并记录已经有多少人次玩游戏
try
{
File f = new File("record.txt");
FileReader fr = new FileReader(f);
BufferedReader d = new BufferedReader(fr);
String s=d.readLine();
StringTokenizer st = new StringTokenizer(s);
str = new String [st.countTokens()];
for(int i=0;st.hasMoreTokens();i++)
{
str[i] = st.nextToken();
}
while(s!=null) {games++;s=d.readLine(); }
name = str[1];
record = Integer.parseInt(str[2]);
d.close();
}
catch (Exception e) {
}
System.out.print("\n\t\t 猜数字游戏 \n ");
System.out.print("\n\t1 . 程序每次产生一个[0,100]之间的随机数; ");
System.out.print("\n\t2 . 从键盘输入的猜测值,返回比较信息(偏大或偏小) ");
System.out.print("\n\t3 .程序运行的最好记录 是: "+name+"\t"+record+"\ttimes.\n\n");
}
public boolean isCorrect(int n) // 判断是否已经猜对,并打出信息
{
if (n==random) return true ;
else
{
if(n>random ) System.out.print("\t---- * ---- 太 大 ----- * ---");
if(n<random ) System.out.print("\t---- * ---- 太 小 ----- * ---");
return false;
}
}
public void getResult() // 返回正确结果
{
System.out.print("\n\t 正确结果是: "+random+" .你猜对了。共猜了:"+times+"次.");
}
public void saveRecord(String s,BufferedReader br) // 保存 纪录 :名字 和次数
{
Date date = new Date();
if(!s.equals("q"))
{
System.out.print("\n\t请输入 Your Name : ");
try
{
name = br.readLine(); //br.close();
} catch (IOException e) {}
ToFile file = new ToFile();
if(times>record) // 本次游戏 没有超过 记录,保存在文件最后一行
{
file.openFile("record.txt",true);
file.writeFile("["+games+"]\t"+name+"\t"+times+"\t"+date);
file.closeFile();
}
else // 新的游戏记录,保存在第一行
{
String [] sr = new String [games];
sr[0] = "";
try // 读取文件记录
{
File f = new File("record.txt");
FileReader fr = new FileReader(f);
BufferedReader d = new BufferedReader(fr);
StringTokenizer st = new StringTokenizer(d.readLine());
String[] str = new String [st.countTokens()];
for(int i=0;st.hasMoreTokens();i++)
{
str[i] = st.nextToken();
if(i>0&&i<3) sr[0] = sr[0]+str[i]+"\t";
if(i>2) sr[0] = sr[0]+str[i]+" ";
}
String t = d.readLine();
for(int i=1;t!=null;i++)
{
sr[i]="";
st = new StringTokenizer(t);
str = new String [st.countTokens()];
for(int j=0;st.hasMoreTokens();j++)
{
str[j] = st.nextToken();
if(j>0&&j<3) sr[i] = sr[i]+str[j]+"\t";
if(j>2) sr[i] = sr[i]+str[j]+" ";
}
t=d.readLine();
}
d.close();
}
catch (Exception e) { }
file.openFile("record.txt"); //压后一行,重新写入
file.writeFile("[0]\t"+name+"\t"+times+"\t"+date);
for(int i= 1;i<games;i++)
file.writeFile("["+i+"]\t"+sr[i-1]);
file.closeFile();
}
}
}
class ToFile // 文件读取写入 封装类
{
private FileWriter fin ;
private PrintWriter out ;
public void openFile(String s,boolean b)
{
try
{ fin = new FileWriter (s,b);
}
catch(IOException e){}
out = new PrintWriter (fin);
}
public void openFile(String s)
{
try
{ fin = new FileWriter (s);
}
catch(IOException e){}
out = new PrintWriter (fin);
}
public void writeFile(String s)
{
out.println(s);
}
public void closeFile()
{
out.close();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -