资源描述:
《新人Eclipse编写的Java小游戏.pdf》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、Eclipse编写的Java小游戏初学java时编写的简单小游戏,非界面版,纯java基础组成,新人都能看得懂可以做的来,主要是其中的逻辑思想。使用eclipse编程,直接建立一个java复制代码进去就可以了。希望对大家学习java编程有点帮助游戏一:随即取5个数排列,要求猜出这5个数packageday;importjava.util.Random;importjava.util.Scanner;importjava.util.regex.Matcher;importjava.util.regex.Pattern;pub
2、licclassGame{publicstaticvoidmain(String[]args){Randomr=newRandom();inta1=r.nextInt(10);inta2=r.nextInt(10);inta3=r.nextInt(10);inta4=r.nextInt(10);inta5=r.nextInt(10);System.out.println("游戏规则:随即取5个数,请输入5个数,数字顺序要求一致,会告诉你答对几个,看你能在几次猜对");Scannerscan=newScanner(Syste
3、m.in);for(inti=1;;i++){intc=0;System.out.println("请输入5个数:");Stringdir=scan.nextLine();Patternpattern=Pattern.compile("^[0-9]{5}$");Matchermatcher=pattern.matcher(dir);if(matcher.find()){intb1=Integer.parseInt(dir.substring(0,1));intb2=Integer.parseInt(dir.substrin
4、g(1,2));intb3=Integer.parseInt(dir.substring(2,3));intb4=Integer.parseInt(dir.substring(3,4));intb5=Integer.parseInt(dir.substring(4,5));if(a1==b1){c++;}if(a2==b2){c++;}if(a3==b3){c++;}if(a4==b4){c++;}if(a5==b5){c++;}if(c==5){if(i<=5){System.out.println("恭喜你猜对了,猜对
5、次数"+i+",妖才级别");break;}elseif(i<=15){System.out.println("恭喜你猜对了,猜对次数"+i+",天才级别");break;}elseif(i<=25){System.out.println("恭喜你猜对了,猜对次数"+i+",聪明人级别");break;}elseif(i<=40){System.out.println("恭喜你猜对了,猜对次数"+i+",常人级别");break;}elseif(i<=60){System.out.println("恭喜你猜对了,猜对次数"
6、+i+",迟钝级别");break;}else{System.out.println("恭喜你猜对了,猜对次数"+i+",傻瓜级别");break;}}else{System.out.println("猜对了"+c+"个,请继续努力");}}else{System.out.println("只能输入5位数字,请重试");}}}}游戏二:packageday;importjava.util.Arrays;importjava.util.Random;importjava.util.Scanner;importjava.uti
7、l.regex.Matcher;importjava.util.regex.Pattern;publicclassGame1{publicstaticvoidmain(String[]args){String[]str={"鼠","牛","虎","兔","龙","蛇","马","羊","猴","鸡","狗","猪"};System.out.println("玩个小游戏,现在你手上有100分,从十二生肖中选择押注,只能压一个数目不限,随机选取5次");System.out.println("中1个翻1倍,2个翻3倍,3个翻6
8、倍,4个翻10倍,5个翻15倍(输入如:龙5)。输入exit时则停止;");System.out.println("(十二生肖:鼠、牛、虎、兔、龙、蛇、马、羊、猴、鸡、狗、猪)");Randomran=newRandom();intmanly=100,many=manly;Scannersan=newS