javaoop第五章

来源:互联网 发布:淘宝商城电脑版登陆 编辑:程序博客网 时间:2024/03/29 05:31
                           玩家类
package bao;import java.util.Scanner;//玩家类public class Player {private int levelNo;//玩家当前级别号private int curScore;///玩家当前级别积分private long stratTime;//当前级别开始时间private  int elapseTime;//当前级别已用时间//封装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 getStratTime() {return stratTime;}public void setStratTime(long stratTime) {this.stratTime = stratTime;}public int getElapseTime() {return elapseTime;}public void setElapseTime(int elapseTime) {this.elapseTime = elapseTime;}//玩家玩游戏的方法public void play(){Game game=new Game(this);Scanner input=new Scanner(System.in);//外层循坏一次,游戏晋级一级for (int i = 0; i < levelProgram .levels.length; i++) {//晋级this.levelNo+=1;//晋级后计时清零,积分清零this.stratTime=System.currentTimeMillis();this.curScore=0;//内层循环一次完成一次字符串输出输入比较for (int j = 0; j < levelProgram .levels[levelNo-1].getStrTime(); j++) {String outstr=game.PrintStr();System.out.println(outstr);String instr=input.next();game.printResult(outstr, instr);}}}}



                                            游戏类

package bao;//游戏类import java.util.Random;public class Game {private Player player;public Game(Player player){this.player=player;}public String PrintStr(){StringBuffer buffer=new StringBuffer();Random random=new Random();//通过循环生成要输出的字符串for (int i = 0; i <2; i++) {int rand=random.nextInt(2);//根据随机数拼接字符串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;}}return buffer.toString();}    public void printResult(String out,String in)    {    if (out.equals(in)) {    long time=System.currentTimeMillis();    if((time-player.getStratTime())/1000>levelProgram.levels[player.getLevelNo()-1].getTimeLimit()){System.out.println("你输入太慢,已经超时,退出");System.exit(1);}else{//当前获取分值player.setCurScore(player.getCurScore()+ levelProgram.levels[player.getLevelNo() - 1].getPerScore());player.setElapseTime((int) (time - player.getStratTime()) / 1000);int levelNo=player.getLevelNo();int score=player.getCurScore();int elapsedTime=player.getElapseTime();System.out.println("输入正确,您的级别"+levelNo+"您的积分"+score+"已用时间"+elapsedTime+"秒。");if(levelNo==6){System.out.println("游戏结束");System.exit(1);}}}else{System.out.println("输入错误,退出");System.exit(1);}        }    }

                                                           级别

package bao;//级别类public class Level {private int levelNo;//级别编号private int strLength;//各级别一次输出字符串长度private int strTime;//各级别输出字符串的次数private int timeLimit;//各级别闯关的时间限制private 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 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 getPerScore() {return perScore;}public void setPerScore(int perScore) {this.perScore = perScore;}//无参构造public Level(){}//有参构造public Level(int levelNo,int StrinLength,int strTime,int timeLimit,int perScore ){this.levelNo=levelNo;this.strLength=StrinLength;this.strTime=strTime;this.timeLimit=timeLimit;this.perScore=perScore;}}
package bao;public class levelProgram extends Level {//继承父类无参构造public levelProgram() {super();// TODO Auto-generated constructor stub}//继承父类有参构造public levelProgram(int levelNo, int StrinLength, int strTime,int timeLimit, int perScore) {super(levelNo, StrinLength, strTime, timeLimit, perScore);// TODO Auto-generated constructor stub}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,14,10);levels[5]=new Level(6,7,5,10,15);}}

                                      测试类

package bao;public class Test {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubPlayer player=new Player();player.play();}}





0 0
原创粉丝点击