您好,欢迎访问三七文档
当前位置:首页 > 行业资料 > 酒店餐饮 > 新人Eclipse编写的Java小游戏
Eclipse编写的Java小游戏初学java时编写的简单小游戏,非界面版,纯java基础组成,新人都能看得懂可以做的来,主要是其中的逻辑思想。使用eclipse编程,直接建立一个java复制代码进去就可以了。希望对大家学习java编程有点帮助游戏一:随即取5个数排列,要求猜出这5个数packageday;importjava.util.Random;importjava.util.Scanner;importjava.util.regex.Matcher;importjava.util.regex.Pattern;publicclassGame{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(System.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.substring(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(恭喜你猜对了,猜对次数+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(恭喜你猜对了,猜对次数+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.util.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倍,4个翻10倍,5个翻15倍(输入如:龙5)。输入exit时则停止;);System.out.println((十二生肖:鼠、牛、虎、兔、龙、蛇、马、羊、猴、鸡、狗、猪));Randomran=newRandom();intmanly=100,many=manly;Scannersan=newScanner(System.in);for(;;){if(manymanly){many=manly;}System.out.println(游戏开始:);Stringsrc=san.nextLine();if(src.equalsIgnoreCase()){System.out.println(输入为空,请重试,您的余额为+manly);continue;}if(src.equalsIgnoreCase(exit)){System.out.println(游戏结束,您的余额为+manly);System.out.println(您获取的历史最高金额为+many);break;}Stringvb=src.substring(0,1);booleanv=false;System.out.println(vb);for(inti=0;istr.length;i++){if(str[i].equalsIgnoreCase(vb)){v=true;break;}}if(!v){System.out.println(请输入正确的押注名称!);continue;}Stringv1=src.substring(1,src.length());Patternpattern=Pattern.compile(^[0-9]+$);Matchermatcher=pattern.matcher(v1);if(!matcher.find()){System.out.println(请输入正确的押注分数!);continue;}intzhu=Integer.parseInt(v1);if(zhu==0){System.out.println(押注分数不能为0);continue;}String[]xto=newString[5];intn=0,i;for(i=0;i5;i++){xto[i]=str[ran.nextInt(str.length)];if(xto[i].equalsIgnoreCase(vb)){n++;}}intmanly1=manly;manly1-=zhu;if(manly10){System.out.println(不好意思,您的余额不足+zhu);continue;}manly=manly1;System.out.println(1.2.3开+Arrays.toString(xto));if(n==0){System.out.println(您没有中,再接再厉,余额为:+manly);}elseif(n==1){manly+=zhu*2;System.out.println(恭喜中了1个,余额为:+manly);}elseif(n==2){manly+=zhu*4;System.out.println(恭喜中了2个,余额为:+manly);}elseif(n==3){manly+=zhu*7;System.out.println(恭喜中了3个,余额为:+manly);}elseif(n==4){manly+=zhu*11;System.out.println(恭喜中了4个,余额为:+manly);}elseif(n==5){manly+=zhu*16;System.out.println(恭喜全中了,余额为:+manly);}if(manly==0){System.out.println(你的余额为0,游戏结束);System.out.println(您获取的历史最高金额为+many);break;}}}}游戏三:packageday;importjava.util.Arrays;importjava.util.Random;importjava.util.Scanner;publicclassGame2{publicstaticvoidmain(String[]args){intmanly=100;String[]i={龙,羊,猪,牛,虎,鸡,蛇,,虎,羊,牛,猪,鸡,蛇,牛,,羊,猪,蛇,鸡,猪,鸡,鸡,};Scannerscanner=newScanner(System.in);System.out.println(玩个老虎机小游戏,现在你手上有100分,从-龙虎猪牛羊鸡蛇-进行押注,一个押注最多不能超过9);System.out.println(龙10倍,虎5倍,蛇3倍,牛、羊、猪2倍,鸡一倍,输入押注方式(龙5;虎1;)。输入exit时则停止);Randomr=newRandom();for(;;){System.out.println(游戏开始);intx=r.nextInt(i.length);intma=0;charya=i[x].charAt(0);Stringstr=scanner.nextLine();if(str.equalsIgnoreCase()){System.out.println(进输入为空,请重试,您的余额为+manly);continue;}if(str.equalsIgnoreCase(exit)){System.out.println(游戏结束,您的余额为+manly);break;}intw=0;if(ya=='龙'){ma=10;}elseif(ya=='虎'){ma=5;}elseif(ya=='蛇'){ma=3;}elseif(ya=='牛'||ya=='羊'||ya=='猪'){ma=2;}elseif(ya=='鸡'){ma=1;}else{System.out.println(很不巧,您中了空,再接再厉);w=1;}char[]cha={};for(inta=0;astr.length();a++){cha=Arrays.copyOf(cha,a+1);intmanly1=manly;cha[cha.length-1]=str.charAt(a);if(a%3==1){manly1=manly-(cha[a]-'0');}if(manly10){System.out.println(不好意思,您的钱不够);w=1;break;}else{manly=manly1;}}if(w==0){intn=0;for(inta=0;acha.length;a+=3){if(ya==cha[a]){manly=manly+(cha[a+1]-'0')*ma;System.out.println(恭喜中了+ya+;您的余额为+manly);n=1;break;}}if(n==0){System.out.println(ya+;很遗憾您没有中;您的余额为+manly);}}if(manly==0){System.out.println(你的余额为0,游戏结束);}}}}
本文标题:新人Eclipse编写的Java小游戏
链接地址:https://www.777doc.com/doc-7275189 .html