JavaOOP 快速击键

来源:互联网 发布:python常数的矩阵次方 编辑:程序博客网 时间:2024/04/20 16:57
package cn.hello;import java.util.Random;/** * 游戏类 * @author Administrator * */public class Game {public Player player;    public Game(Player player2) {this.player=player2;}public String printStr(){    int strLength = LevelParam.levels[player.getLeveNo() - 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 void printResult(String out,String in){    boolean flag;    if (out.equals(in)) {flag=true;}else {flag=false;}    if (flag) {long currentTime=System.currentTimeMillis();//如果超时if ((currentTime-player.getStartTime())/1000>LevelParam.levels[player.getLeveNo()-1].getTimeLimit()) {System.out.println("你输入太慢了,已经");System.exit(1);}else {//计算当前积分player.setCurrScore(player.getCurrScore()+LevelParam.levels[player.getLeveNo()-1].getPetScore());// 1.2.2、 计算已用时间player.setElasedTime((int) ((currentTime - player.getStartTime()) / 1000));// 1.2.3、输出当前积分、当前级别、已用时间System.out.println("输入正确,您的级别"+ player.getLeveNo()+" ,您的积分" + player.getCurrScore() + ",已用时间"+ player.getElasedTime() + "秒。");// 1.2.4、判断用户是否已经闯过最后一关if (player.getLeveNo() == 6) {int score=LevelParam.levels[player.getLeveNo() - 1].getPetScore() * LevelParam.levels[player.getLeveNo() - 1].getStrTime();//计算闯关分数if (player.getCurrScore() == score) {System.out.println("你已闯关成功,成为绝世高手,恭喜你!!!");System.exit(0);}}}// 2、如果输入错误} else {System.out.println("输入错误,退出!");System.exit(1);}}}    package cn.hello;/** * 级别类 * @author Administrator * */public class Level {private int leveNo;//各级别编号private int strLength;//各级别输出的字符串长度private int strTime;//各级别输出的字符串次数private int timeLimit;//各级别闯关的时间限制private int petScore;//各级别正确输入一次的得分public Level(int i, int j, int k, int l, int m) {this.leveNo=i;this.strLength=j;this.strTime=k;this.timeLimit=l;this.petScore=m;}public int getLeveNo() {return leveNo;}public void setLeveNo(int leveNo) {this.leveNo = leveNo;}public int getStrLength() {return strLength;}public void setStrLength(int strLength) {this.strLength = strLength;}public int getStrTime() {return strTime;}public void setStrTime(int strTime) {this.strTime = strTime;}public int getTimeLimit() {return timeLimit;}public void setTimeLimit(int timeLimit) {this.timeLimit = timeLimit;}public int getPetScore() {return petScore;}public void setPetScore(int petScore) {this.petScore = petScore;}}package cn.hello;public class LevelParam {    public class levels {}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);    }} package cn.hello;import java.util.Scanner;/** * 玩家类 * @author Administrator * */public class Player {    private int leveNo;//玩家当前级别号    private int currScore;//玩家当前级别积分    private long startTime;//当前级别开始时间    private int   elasedTime;//当前级别已用时间       public int getLeveNo() {return leveNo;}public void setLeveNo(int leveNo) {this.leveNo = leveNo;}public int getCurrScore() {return currScore;}public void setCurrScore(int currScore) {this.currScore = currScore;}public long getStartTime() {return startTime;}public void setStartTime(long startTime) {this.startTime = startTime;}public int getElasedTime() {return elasedTime;}public void setElasedTime(int elasedTime) {this.elasedTime = elasedTime;}   public void play(){   Game game=new Game(this);   Scanner input = new Scanner(System.in);   //外层循环一次 升一级   for (int i = 0; i < LevelParam.levels.length; i++) {   //晋级   this.leveNo+=1;   //晋级后计时清零    this.startTime=System.currentTimeMillis();   this.currScore=0;   //内层循环一次 完成一次字符串的输出,输入,比较   for (int j = 0; j < LevelParam.levels[leveNo-1].getStrTime(); j++) {//游戏输出字符串   String outString=game.printStr();   //接受用户输入   String instrString=input.next();   //判断 玩家输入是否正确,并输出相应的结果   game.printResult(outString, instrString);}   }      }}package cn.hello;public class Test {public static void main(String[] args) {Player player=new Player();player.play();}}

0 0
原创粉丝点击