看到这么多人查看,更有信心继续写了,好久没写了上传一个小游戏吧
import java.util.Random;
import java.util.Scanner;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class GuessGame3 extends JFrame{
//private JPanel imagePanel;
//private ImageIcon background;
JLabel jlabel;
//public GuessGame3(){
//background=new ImageIcon("bg.jpg");
//jlabel=new JLabel(new ImageIcon("bg.jpg"));
//}
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
System.out.println("--------欢迎大家来到猜字谜游戏-------");
System.out.println("游戏规则:\n"
+"1、游戏中的字符种类可自定义选择(字母和数字)并且区分大小写;\n"
+"2、输入的字符与位置和随机生成的字符与位置一一对应才算正确\n"
+"3、游戏总分1000分,每猜一次扣除10分,分数为0退出游戏\n"
+"4、游戏每运行一次系统都会随机生成不重复的字符;\n"
+"5、游戏中字符长度可自由设置\n"
+"6、猜字过程中输入“==”退出游戏。\n\n"
+"--------------游戏开始!Ready Go!--------\n");
int score=1000;
int chs=0,loc=0;//声明猜对字符的个数几位置正确的字符个数
String choiceStr=null,countStr=null;
int choice=0,count=0;
char[] answer=null;
while(choice<1||choice>5){
System.out.println("请设置字符类型:(输入类型前面的序号进行选择)\n"
+"1、数字 2、小写字母 3、大写字母 4、字母组合 5、字母与数字组合");
choiceStr=scan.nextLine();//输入要选择的字符类型
if(isNumer(choiceStr)){
choice=Integer.parseInt(choiceStr);
if(choice<1||choice>5){
System.out.println("你的选择有误,请重新输入");continue;
}
}else{
System.out.println("你输入了非法字符,请重新输入");
}
}
while(count<1||count>26){
System.out.println("请设置字符长度(数字类型长度为1-10,其他类型长度1-26)");
countStr=scan.nextLine();//要输入要猜字符的长度
if(isNumer(countStr)){
count=Integer.parseInt(countStr);//将输入的字符传转换为整形
if(choice==1){//如果选择的是1类型
if(count<1||count>10){
System.out.println("你选择猜测的字符类型为数字,与长度不匹配,请重新输入长度\n");
count=0;continue;
}
}else{
if(count<1||count>26){
System.out.println("你选择猜测的字符类型长度不匹配,请重新输入");
}
}
}else{
System.out.println("你输入了非法字符,请重新书");
}
}
//调用生成随机字符方法(共有五种随即方法)
if(choice==1){
answer=generate1(count);
}else if(choice==2){
answer=generate2(count);
}else if(choice==3){
answer=generate3(count);
}else if(choice==4){
answer=generate4(count);
}else{
answer=generate5(count);
}
do{
System.out.println("猜猜看吧,请输入"+count+"个字符");
String s=scan.nextLine();
//String s=scan.next().trim().toUpperCase();
//接收用户输入的字母并自动将小写转换为大写(几不区分大小写)
//检测用户输入==退出游戏
if(s.equals("==")){
System.out.println("------退出游戏!欢迎继续挑战--------");break;
}
if(s.length()!=count){
System.out.println("你的输入有误,字符长度必须是"+count+"位");continue;
}
char[] input=s.toCharArray();//输入的字符存到数组中
int[] var=check(answer,input);//调用比较方法返回有cha loc信息的数组chock
chs=var[0];
loc=var[1];
System.out.println("你猜对字符个数"+chs+"其中位置正确的个数"+loc);
score-=10;//猜一次扣10分
if(count==0){
System.out.println("\nYOU LOSE!唉----真替你智商着急");break;
}
if(chs==count&&loc==count){
rank(count,score);//调用方法输出段位
}
}while(chs!=count||loc!=count);
scan.close();//输入关闭
}
public static char[] generate1(int n){
//将10个数字放到一个数组里面然后随机生成这个数组的索引,通过随即索引得到数字
char[] allLetters={'0','1','2','3','4','5','6','7','8','9'};
boolean[] isRep=new boolean[allLetters.length];//创建数组与长度一致
char[] letter=new char[n];//创建数组接收随机生成的N个字符
int temp;//声明数组索引
for(int i=0;i<letter.length;i++){
do{
temp=new Random().nextInt(allLetters.length);//生成随机数方法一
letter[i]=allLetters[temp];//将allLetters数组中下表为temp元素赋值给letter数组中索引为I的元素
}while(isRep[temp]);
isRep[temp]=true;//letter每一次赋值完成后,将于allLetters数组下标对应的isReb数组下标所对应元素改为true
}
return letter;
}
public static char[] generate2(int n){
char[] allLetters={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
boolean[] isRep=new boolean[allLetters.length];
char[] letter=new char[n];
int temp;
for(int i=0;i<letter.length;i++){
do{
temp=new Random().nextInt(allLetters.length);//生成随机数方法一
letter[i]=allLetters[temp];//将allLetters数组中下表为temp元素赋值给letter数组中索引为I的元素
}while(isRep[temp]);
isRep[temp]=true;//letter每一次赋值完成后,将于allLetters数组下标对应的isReb数组下标所对应元素改为true
}
return letter;
}
public static char[] generate3(int n){
char[] allLetters={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W'
,'X','Y','Z'};
boolean[] isRep=new boolean[allLetters.length];
char[] letter=new char[n];
int temp;
for(int i=0;i<letter.length;i++){
do{
temp=new Random().nextInt(allLetters.length);//生成随机数方法一
letter[i]=allLetters[temp];//将allLetters数组中下表为temp元素赋值给letter数组中索引为I的元素
}while(isRep[temp]);
isRep[temp]=true;//letter每一次赋值完成后,将于allLetters数组下标对应的isReb数组下标所对应元素改为true
}
return letter;
}
public static char[] generate4(int n){
char[] allLetters={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W'
,'X','Y','Z'};
boolean[] isRep=new boolean[allLetters.length];
char[] letter=new char[n];
int temp;
for(int i=0;i<letter.length;i++){
do{
temp=new Random().nextInt(allLetters.length);//生成随机数方法一
letter[i]=allLetters[temp];//将allLetters数组中下表为temp元素赋值给letter数组中索引为I的元素
}while(isRep[temp]);
isRep[temp]=true;//letter每一次赋值完成后,将于allLetters数组下标对应的isReb数组下标所对应元素改为true
}
return letter;
}
public static char[] generate5(int n){
char[] allLetters={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W'
,'X','Y','Z'};
boolean[] isRep=new boolean[allLetters.length];
char[] letter=new char[n];
int temp;
for(int i=0;i<letter.length;i++){
do{
temp=new Random().nextInt(allLetters.length);//生成随机数方法一
letter[i]=allLetters[temp];//将allLetters数组中下表为temp元素赋值给letter数组中索引为I的元素
}while(isRep[temp]);
isRep[temp]=true;//letter每一次赋值完成后,将于allLetters数组下标对应的isReb数组下标所对应元素改为true
}
return letter;
}
public static boolean isNumer(String str){
for(int i=0;i<str.length();i++){
if(!Character.isDigit(str.charAt(i))){
return false;
}
}
return true;
}
//比较系统随机生成的字符与用户输入的字符
public static int[] check(char[] answer,char[] input){
int m=0,n=0;
for(int i=0;i<answer.length;i++){
for(int j=0;j<input.length;j++){
if(answer[i]==input[j]){
m++;//猜中的字符个数
if(i==j){
n++;//位置对应的字符个数
}
break;
}
}
}
return new int[]{m,n};//将猜中的字符个数与位置对应的字符个数存放到数组中
}
public static void rank(int n,int score){
if(n>=4){
if(score>=950){
System.out.println("\n恭喜你,猜对了,你的得分"+score+"分\n\n段位"+"-----最强王者");
}else if(score>=900){
System.out.println("\n恭喜你,猜对了,你的得分"+score+"分\n\n段位"+"-----超凡大师");
}else if(score>850){
System.out.println("\n恭喜你,猜对了,你的得分"+score+"分\n\n段位"+"-----璀璨钻石");
}else if(score>800){
System.out.println("\n恭喜你,猜对了,你的得分"+score+"分\n\n段位"+"-----华贵铂金");
}else if(score>750){
System.out.println("\n恭喜你,猜对了,你的得分"+score+"分\n\n段位"+"-----荣耀黄金");
}else if(score>700){
System.out.println("\n恭喜你,猜对了,你的得分"+score+"分\n\n段位"+"-----不屈白银");
}else if(score>600){
System.out.println("\n恭喜你,猜对了,你的得分"+score+"分\n\n段位"+"-----英勇黄铜");
}else{
System.out.println("\n恭喜你,猜对了,你的得分"+score+"分\n\n段位"+"----- 加油吧!骚年");
}
}else{
System.out.println("\n恭喜你,猜对了,你的得分"+score+"分\n\n段位"+"-----这太小儿科了,你需要更大的挑战");
}
}
}
}