时至今日,java的基础部分已经完成,之后我会发java高级的内容,敬请期待。
回顾一下目前学的内容:常量、变量的定义,打印语句的定义,两种选择结构,三种循环结构,一维数组和二维数组,类和对象,接口和异常,以及关键字:equals、this、extends、implements、super、try、catch等,如果能达到看到标题就能想到他们对应的内容的程度,就可以说学的还不错了,还有哪些地方比较模糊的可以看看我之前的文章,进行巩固。
这里给出一个项目对java基础知识进行巩固,可以使用目前所学到的知识完成。
项目名:QuickHi
需求:(简述)程序随机生成字符串,玩家在规定的时间内对照程序生成的字符串输入相同的字符串,若相同则游戏成功,等级提升1,若不相同则游戏失败。
具体如下:
1、根据输入速率和正确率将玩家分为不同级别
2、级别越高,一次显示的字符数越多,玩家正确输入一次的得分也越高
3、规定时间内完成规定次数的输入,正确率达到规定要求,则升级
4、玩家最高级别为6 级、初始级别一律为 1 级
5、用户错误输入一次,游戏结束
这里给出我的代码:
Game类代码:
public class Game { Level lev = new Level(); Player player = new Player(); public Game() { show(); } void show(){ Level lev = new Level(); Player player = new Player(); long startTime1 = System.currentTimeMillis() / 1000; //获取开始时间 for (player.levelNow = 1; player.levelNow <= 6; player.levelNow++) { long startTime = System.currentTimeMillis() / 1000; //获取开始时间 int flag = (int) (Math.random() * 20); char[] chars = {'a', 'b', 'c', 'd', 'e', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'n', 'm', 'o', 'p', 'q', '[', ']'}; String sr = ""; lev.strTimes = 3; for (int i = 0; i < lev.strTimes; i++) { char a = chars[flag]; sr += a; } System.out.println(sr); Scanner sc = new Scanner(System.in); String s = sc.next(); if (sr.equals(s)) { player.currScore += 3; lev.strTimes += 2; System.out.println("玩家当前得分:" + player.currScore); System.out.println("等级加1,玩家此时等级为:" + player.levelNow); System.out.println("游戏进入下一阶段"); if (player.levelNow == 6) { System.out.println("游戏通关"); System.out.println("游戏数据重置"); break; } } else { System.out.println("游戏失败,请重新努力,再接再厉"); break; } long endTime = System.currentTimeMillis() / 1000; //获取结束时间 long o=endTime-startTime; System.out.println("此关通关时间:"+o+"s"); if (o>player.levelNow*3){ System.out.println("你超时了,游戏结束"); break; } } long endTime1 = System.currentTimeMillis() / 1000; //获取开始时间 System.out.println("程序总共运行时间:"+(endTime1-startTime1)); }
Level类代码:public class Level { public int levelNo;//级别号 public int strLength;//字符串长度 public int strTimes;//字符串次数 public int timeLimit;//各级别时间限制 public int perScore;//正确输入一次的得分 }
Player类代码:
public class Player { public int levelNow;//当前等级 public int currScore;//所得的分 public int startTime;//开始时间 public int elapsedTime;//已用时间 }
text类代码:
public class text { public static void main(String[] args) { new Game(); } }