一、项目分析

根据输入速率和正确率将玩家分为不同等级,级别越高,一次显示的字符数越多,玩家正确输入一次的得分也越高。如果玩家在规定时间内完成规定次数的输入,正确率达到规定要求,则玩家升级。玩家最高级别为6级,初始级别一律为一级!

二、掌握的技能点

①面向对象设计的思想

②使用类图理解类的关系

③类的封装

④构造方法的使用

⑤this和static关键字的使用

类的属性:

①玩家(Player)类的属性:当前级别号levelNo、当前级别积分currScore、当前级别开始时间startTime和当前级别已用时间elapsedTime

②级别(Level)类的属性:各级别编号levelNo、各级别一次输出字符串的长度strLength、各级别输出字符串的次数strTime、各级别闯关的时间限制timeLimit和各级别正确输入一次得分

类的方法:

游戏Game类的主要方法有2个:输出字符串、返回字符串用于和玩家的输入进行比较[String printStr]确认玩家输入是否正确[void printResult(String out,String in)],比较游戏输出out和玩家输入in

玩家Player类的方法:玩游戏play()

LevelParam类:定义一个长度为6的Level数组,用来存放各级别的具体参数信息

关键代码:

Player类:

package cn.happy;

import java.util.Scanner;

public class Player {
public int levelNo; //级别号 public int curScore; //积分 public long startTime; //开始时间 public int elapsedTime; //已用时间 public int getLevelNo() {
return levelNo;
} public void setLevelNo(int levelNo) {
this.levelNo = levelNo;
} public int getCurScore() {
return curScore;
} public void setCurScore(int curScore) {
this.curScore = curScore;
} public long getStartTime() {
return startTime;
} public void setStartTime(long startTime) {
this.startTime = startTime;
} public int getElapsedTime() {
return elapsedTime;
} public void setElapsedTime(int elapsedTime) {
this.elapsedTime = elapsedTime;
} public Player()
{} public Player(int levelNo,int curScore,long startTime,int elapsedTime)
{
this.levelNo=levelNo;
this.curScore=curScore;
this.startTime=startTime;
this.elapsedTime=elapsedTime; }
//玩游戏的方法
public void play()
{
Game game=new Game(this);
Scanner input=new Scanner(System.in);
//外层循环,循环一次级别晋一级
for (int i = 0; i < LevelParam.levels.length; i++) {
//晋级
this.levelNo+=1;
//晋级后计时清零,积分清零
this.startTime=System.currentTimeMillis();
this.curScore=0;
//内层循环 循环一次完成一次字符串的输入,输出,比较 for (int j = 0; j < LevelParam.levels[levelNo-1].getStrTimes(); j++) {
//游戏输出字符串
String outstr=game.printStr();
//接收用户输入
String instr=input.next();
//游戏判断玩家输入的是否正确
game.printResult(outstr,instr);
}
} } }

Level类:

package cn.happy;

public class Level {
public int levelNo; //各级别编号 public int strLength;// 各级别一次输出字符串的长度 public int strTimes;// 各级别输出字符串的次数 public int timeLimit;// 各级别闯关的时间限制 public int perScore;// 各级别正确输入一次的得分 public int getLevelNo() {
return levelNo;
} public void setLevelNo(int levelNo) {
this.levelNo = levelNo;
} public int getStrLength() {
return strLength;
} public void setStrLength(int strLength) {
this.strLength = strLength;
} public int getStrTimes() {
return strTimes;
} public void setStrTimes(int strTimes) {
this.strTimes = strTimes;
} public int getTimeLimit() {
return timeLimit;
} public void setTimeLimit(int timeLimit) {
this.timeLimit = timeLimit;
} public int getPerScore() {
return perScore;
} public void setPerScore(int perScore) {
this.perScore = perScore;
} public Level()
{} public Level(int levelNo,int strLength,int strTimes,int timeLimit,int perScore)
{
this.levelNo=levelNo;
this.strLength = strLength;
this.strTimes = strTimes;
this.timeLimit = timeLimit;
this.perScore = perScore;
} }

LevelParam类

package cn.happy;

public class LevelParam {
//级别参数类,配置各个级别参数 //对应6个级别
public final static Level levels[]=new Level[6];
static{
levels[0]=new Level(1,2,10,30,1);
levels[1]=new Level(2,3,9,26,2);
levels[2]=new Level(3,4,8,22,5);
levels[3]=new Level(4,5,7,18,8);
levels[4]=new Level(5,6,6,15,10);
levels[5]=new Level(6,7,5,12,15);
} }

Game类:

package cn.happy;

import java.util.Random;

public class Game {
//玩家
private Player player; public Game()
{} public Game(Player player)
{
this.player=player;
} public String printStr()
{ // 获取级别对应的要输出字符串的长度
int strLength = LevelParam.levels[player.getLevelNo() -1].getStrLength(); StringBuffer buffer=new StringBuffer(); Random random=new Random();
//通过循环生成要输出的字符串
for (int i = 0; i < strLength; i++) {
//产生随机数
int rand=random.nextInt(strLength);
//根据随机数拼接字符串
switch(rand)
{
case 0:
buffer.append(">");
break;
case 1:
buffer.append("<");
break;
case 2:
buffer.append("*");
break;
case 3:
buffer.append("$");
break;
case 4:
buffer.append("%");
break;
case 5:
buffer.append("#");
break;
} }
// 输出字符串
System.out.println(buffer);
// 返回该字符串的值,用于和用户输入字符串的值作比较
return buffer.toString();
} //判断玩家输入的是否正确,并输出相应的结果
public String printResult(String outstr,String instr)
{
boolean flag=false;
if(outstr.equals(instr))
{
flag=true;
}
else
{
System.out.println("输入错误!哈哈");
System.exit(0);
}
if(flag)
{
long currentTime=System.currentTimeMillis();
//如果超时
if((currentTime-player.getStartTime())/100>LevelParam.levels[player.getLevelNo()-1].getTimeLimit())
{
System.out.println("你输入的太慢了,已经超时,退出!");
System.exit(1);
} //计算玩家当前积分
player.setCurScore(player.getCurScore()+LevelParam.levels[player.getLevelNo()-1].getPerScore()); //计算玩家已用时间
player.setElapsedTime((int)(currentTime-player.getStartTime())/1000); //输出玩家当前级别,当前积分,当前时间
System.out.println("输入正确,您的级别是"+player.levelNo+",您的积分是"+player.curScore+",已用时间"+player.elapsedTime+""); }
return "hh";
} }

测试Test类:

package cn.happy;

public class Test {

    /**
* @param args
*/
public static void main(String[] args) {
Player player=new Player();
player.play(); } }

-------------------------------------------------------------------------------------------------------------

------------------------------------------------------------------------------------------------

----------------------------------------------------------------------------------------------------

Quickhit快速击键的更多相关文章

  1. QuickHit快速击键小程序 --S2.4.5

    我们现在要做一个项目 一个小小的程序 叫做快速击键 很明了的目的 就是在规定时间内,每次出现一组字母的组合,这个字母只能在DFJK中生成 然后输入相应的文字,按回车 自动判断输入的是否正确 在规定时间 ...

  2. 05章项目: QuickHit快速击键

    一.项目分析 根据输入速率和正确率将玩家分为不同等级,级别越高,一次显示的字符数越多,玩家正确输入一次的得分也越高.如果玩家在规定时间内完成规定次数的输入,正确率达到规定要求,则玩家升级.玩家最高级别 ...

  3. 快速击键(MyEclipse编写的QuickHit项目)

    public class Level { private int levelNo;// 各级别编号 private int strLength;// 各级别一次输出字符串的长度 private int ...

  4. xdotool模拟击键和鼠标移动

    最近双十一抢红包的活动比较火,我也就去玩了一下,在一个小活动里,需要不停的点击左箭头和右箭头,让红包不停的跑,但自己点的比较慢,老是出现下面的图片 看到提示还有n多公里才跑完,感觉极度不爽,一怒之下, ...

  5. xdotool xdotool模拟击键和鼠标移动--CutyCapt是一个截图工具,xvfb-run

    最近在做一个生成网站缩略图的功能,从网上查到相关资料,现与大家分享,xvfb这个软件,安装上之后一条命令就能执行此操作.很容易的就生成了自己想要的缩略图. xvfb-run -运行在一个虚拟的X服务器 ...

  6. Win32 键盘事件 - 击键消息、字符消息、插入符号(光标)

    注:以下内容为学习笔记,多数是从书本.资料中得来,只为加深印象,及日后参考.然而本人表达能力较差,写的不好.因非翻译.非转载,只好选原创,但多数乃摘抄,实为惭愧.但若能帮助一二访客,幸甚! 以下内容主 ...

  7. 三星笔记本进入BIOS后找不到U盘启动项/快速启动键F12没有反应

    分析:BIOS开启了 Fast Bios Mode 解决方法: 开机按F2进入BIOS设置,选择Advanced菜单下Fast Bios Mode,设置为 Disabled,按F10键保存退出,重启时 ...

  8. linux下关闭键盘的重复击键

    https://askubuntu.com/questions/576421/disable-keyboard-repeat-from-command-line You can use the xse ...

  9. 模拟TAB键

    模拟TAB键 (2013/6/7 22:35:29) SelectNext(ActiveControl,True,True); 屏蔽Alt+F4关闭键 (2013/6/7 22:35:39) 启动某些 ...

随机推荐

  1. mysql死锁,等待资源,事务锁,Lock wait timeout exceeded; try restarting transaction解决

    前面已经了解了InnoDB关于在出现锁等待的时候,会根据参数innodb_lock_wait_timeout的配置,判断是否需要进行timeout的操作,本文档介绍在出现锁等待时候的查看及分析处理: ...

  2. Dungeon Game

    The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. ...

  3. POJ2451 Uyuw's Concert(半平面交)

    题意就是给你很多个半平面,求半平面交出来的凸包的面积. 半平面交有O(n^2)的算法,就是每次用一个新的半平面去切已有的凸包,更新,这个写起来感觉也不是特别好写. 另外一个O(nlogn)的算法是将半 ...

  4. LA 3713

    The Bandulu Space Agency (BSA) has plans for the following three space missions: Mission A: Landing ...

  5. 小鲜肉初学JS做得仿京东淘宝竖排二级导航

    <!DOCTYPE html><html><head><meta charset="utf-8"><meta http-equ ...

  6. Java-数据结构与算法-逢3减1-面向对象

    1.要求:有一群人围成一圈数数,逢3退1人,要求算出最后留下来的人的下标2.用面向对象思想,有三个步骤:(1)有哪些类:找名词--"有一群人围成一圈",所以有类People,Peo ...

  7. Fatal error: cannot allocate memory for the buffer pool

    mysql有时候会被系统kill掉,原因是内存不够了,一般都是Ubuntu出现的,因为Ubuntu吃内存,你们又给的不多.. 咋解决呢? 重启服务器是可以的,起码暂时可以了, 可以考虑加内存,或者增加 ...

  8. 317. Shortest Distance from All Buildings

    题目: Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where th ...

  9. Linux命令-wc

    wc命令用于统计指定文本的行数.字数.字节数 格式:wc [参数] 文本 [root@localhost test]# wc test.txt test.txt [root@localhost tes ...

  10. javascript Klass 实现

    var Klass=function(Parent,props){ var Child,F,i; Child=function(){ if(Child.uber && Child.ub ...