package?com.jc1;
import?java.util.*;
public?class?GuessGame2?{
public?static?void?main(String[]?args)?{
Scanner?scan?=?new?Scanner(System.in);
System.out.println("====================歡迎來(lái)到猜字母游戲====================");
System.out.println("游戲規(guī)則:\n"?
+?"1、游戲中的字符種類可自定義選擇(字母和數(shù)字),并且區(qū)分大小寫;\n"?
+?"2、輸入字符的位置與隨機(jī)生成的字符位置一一對(duì)應(yīng)才算正確;\n"
+?"3、游戲總分1000分,每猜1次扣除10分,分?jǐn)?shù)為0則退出游戲;\n"?
+?"4、游戲中字符長(zhǎng)度可自定義選擇;\n"?
+?"5、猜字符過(guò)程中輸入“==”退出游戲。\n\n"
+?"------------------游戲開始!Ready!Go!------------------");
int?score?=?1000;?//?總分
int?chs?=?0,?loc?=?0;?//?聲明猜對(duì)字符的個(gè)數(shù)及位置正確的字符個(gè)數(shù)
int?choice,?count;
char[]?answer?=?null;
do?{
System.out.println("請(qǐng)選擇猜測(cè)的字符種類:\n"?
+?"1.數(shù)字????2.小寫字母????3.大寫字母????4.大小寫字母組合????5.大小寫字母與數(shù)字組合");
choice?=?scan.nextInt();
if?(choice?<?1?||?choice?>?5)?{
System.out.println("你的輸入有誤!請(qǐng)重新輸入!");
}
}?while?(choice?<?1?||?choice?>?5);
System.out.println("請(qǐng)自定義要猜測(cè)的字符長(zhǎng)度:(數(shù)字1~10,字母1~26)");
count?=?scan.nextInt();?//?自定義字符的個(gè)數(shù)
scan.nextLine();?//?將nextInt()丟棄掉的回車符(用戶上一次輸入完成后的回車符)接收,以保證后續(xù)nextLine()能正常接收輸入
//?調(diào)用生成隨機(jī)字符方法(共有五種隨機(jī)方法)
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);
}
//System.out.println(answer);?//?輸出系統(tǒng)隨機(jī)生成的字符
do?{
System.out.println("請(qǐng)輸入"?+?count?+?"個(gè)字符:");
String?s?=?scan.nextLine();
//?String?s?=?scan.next().trim().toUpperCase();?//
//?接收用戶輸入的字母并自動(dòng)將小寫轉(zhuǎn)換成大寫(即不區(qū)分大小寫)
//?檢查用戶輸入==退出游戲
if?(s.equals("=="))?{
System.out.println("------------------退出游戲!歡迎繼續(xù)挑戰(zhàn)!------------------");
break;
}
if?(s.length()?!=?count)?{
System.out.println("你的輸入有誤!字符長(zhǎng)度必須是"?+?count?+?"位!請(qǐng)重新輸入!");
continue;
}
char[]?input?=?s.toCharArray();
int[]?var?=?check(answer,?input);?//?調(diào)用比較方法返回有chs與loc信息的數(shù)組check
chs?=?var[0];
loc?=?var[1];
System.out.println("你猜對(duì)字符個(gè)數(shù):"?+?chs?+?",其中位置正確的字符個(gè)數(shù):"?+?loc);
score?-=?10;
if?(score?==?0)?{
System.out.println("你太差勁了!IQ有待提升!");
break;
}
if?(chs?==?count?&&?loc?==?count)?{
rank(count,?score);?//?調(diào)用rank方法輸出段位
}
}?while?(chs?!=?count?||?loc?!=?count);
scan.close();?//?關(guān)閉輸出
}
//?效率非常高且常用的生成隨機(jī)字符方法一(純數(shù)字)
public?static?char[]?generate1(int?n)?{
//?方法一:將26個(gè)大寫字母放到一個(gè)數(shù)組里面,然后隨機(jī)生成這個(gè)數(shù)組的索引(下標(biāo)),通過(guò)索引得到隨機(jī)字母
//?方法二:隨機(jī)生成26個(gè)大寫字母所對(duì)應(yīng)的int型數(shù)字(65~90),再轉(zhuǎn)換成字母即可
char[]?allLetters?=?{?'0',?'1',?'2',?'3',?'4',?'5',?'6',?'7',?'8',?'9'?};
boolean[]?isRep?=?new?boolean[allLetters.length];?//?創(chuàng)建一個(gè)boolean型數(shù)組,與allLetters長(zhǎng)度一致,所有元素默認(rèn)為false
char[]?letter?=?new?char[n];?//?創(chuàng)建數(shù)組接收隨機(jī)生成的n個(gè)字符
int?temp;?//?聲名數(shù)組索引(下標(biāo))
for?(int?i?=?0;?i?<?letter.length;?i++)?{
do?{
temp?=?new?Random().nextInt(allLetters.length);?//?生成隨機(jī)數(shù)方法一(allLetters數(shù)組下標(biāo)0~25)
//?temp?=?(int)?(Math.random()?*?allLetters.length);?//?生成隨機(jī)數(shù)方法二
letter[i]?=?allLetters[temp];?//?將allLetters數(shù)組中下標(biāo)為temp的元素賦值給letter數(shù)組中索引為i的元素
}?while?(isRep[temp]);
isRep[temp]?=?true;?//?letter每一次賦值完成后,將與allLetters數(shù)組下標(biāo)對(duì)應(yīng)的isRep數(shù)組下標(biāo)所對(duì)應(yīng)的元素值改為true
}
return?letter;
}
//?效率非常高且常用的生成隨機(jī)字符方法二(小寫字母)
public?static?char[]?generate2(int?n)?{
//?方法一:將26個(gè)大寫字母放到一個(gè)數(shù)組里面,然后隨機(jī)生成這個(gè)數(shù)組的索引(下標(biāo)),通過(guò)索引得到隨機(jī)字母
//?方法二:隨機(jī)生成26個(gè)大寫字母所對(duì)應(yīng)的int型數(shù)字(65~90),再轉(zhuǎn)換成字母即可
char[]?allLetters?=?{?'a',?'b',?'c',?'d',?'e',?'f',?'g',?'h',?'i',?'i',?'k',?'l',?'m',?'n',?'o',?'p',?'q',?'r',
's',?'t',?'u',?'v',?'w',?'x',?'y',?'z'?};
boolean[]?isRep?=?new?boolean[allLetters.length];?//?創(chuàng)建一個(gè)boolean型數(shù)組,與allLetters長(zhǎng)度一致,所有元素默認(rèn)為false
char[]?letter?=?new?char[n];?//?創(chuàng)建數(shù)組接收隨機(jī)生成的n個(gè)字符
int?temp;?//?聲名數(shù)組索引(下標(biāo))
for?(int?i?=?0;?i?<?letter.length;?i++)?{
do?{
temp?=?new?Random().nextInt(allLetters.length);?//?生成隨機(jī)數(shù)方法一(allLetters數(shù)組下標(biāo)0~25)
//?temp?=?(int)?(Math.random()?*?allLetters.length);?//?生成隨機(jī)數(shù)方法二
letter[i]?=?allLetters[temp];?//?將allLetters數(shù)組中下標(biāo)為temp的元素賦值給letter數(shù)組中索引為i的元素
}?while?(isRep[temp]);
isRep[temp]?=?true;?//?letter每一次賦值完成后,將與allLetters數(shù)組下標(biāo)對(duì)應(yīng)的isRep數(shù)組下標(biāo)所對(duì)應(yīng)的元素值改為true
}
return?letter;
}
//?效率非常高且常用的生成隨機(jī)字符方法三(大寫字母)
public?static?char[]?generate3(int?n)?{
//?方法一:將26個(gè)大寫字母放到一個(gè)數(shù)組里面,然后隨機(jī)生成這個(gè)數(shù)組的索引(下標(biāo)),通過(guò)索引得到隨機(jī)字母
//?方法二:隨機(jī)生成26個(gè)大寫字母所對(duì)應(yīng)的int型數(shù)字(65~90),再轉(zhuǎ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];?//?創(chuàng)建一個(gè)boolean型數(shù)組,與allLetters長(zhǎng)度一致,所有元素默認(rèn)為false
char[]?letter?=?new?char[n];?//?創(chuàng)建數(shù)組接收隨機(jī)生成的n個(gè)字符
int?temp;?//?聲名數(shù)組索引(下標(biāo))
for?(int?i?=?0;?i?<?letter.length;?i++)?{
do?{
temp?=?new?Random().nextInt(allLetters.length);?//?生成隨機(jī)數(shù)方法一(allLetters數(shù)組下標(biāo)0~25)
//?temp?=?(int)?(Math.random()?*?allLetters.length);?//?生成隨機(jī)數(shù)方法二
letter[i]?=?allLetters[temp];?//?將allLetters數(shù)組中下標(biāo)為temp的元素賦值給letter數(shù)組中索引為i的元素
}?while?(isRep[temp]);
isRep[temp]?=?true;?//?letter每一次賦值完成后,將與allLetters數(shù)組下標(biāo)對(duì)應(yīng)的isRep數(shù)組下標(biāo)所對(duì)應(yīng)的元素值改為true
}
return?letter;
}
//?效率非常高且常用的生成隨機(jī)字符方法四(大小寫字母)
public?static?char[]?generate4(int?n)?{
//?方法一:將26個(gè)大寫字母放到一個(gè)數(shù)組里面,然后隨機(jī)生成這個(gè)數(shù)組的索引(下標(biāo)),通過(guò)索引得到隨機(jī)字母
//?方法二:隨機(jī)生成26個(gè)大寫字母所對(duì)應(yīng)的int型數(shù)字(65~90),再轉(zhuǎn)換成字母即可
char[]?allLetters?=?{?'a',?'b',?'c',?'d',?'e',?'f',?'g',?'h',?'i',?'i',?'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];?//?創(chuàng)建一個(gè)boolean型數(shù)組,與allLetters長(zhǎng)度一致,所有元素默認(rèn)為false
char[]?letter?=?new?char[n];?//?創(chuàng)建數(shù)組接收隨機(jī)生成的n個(gè)字符
int?temp;?//?聲名數(shù)組索引(下標(biāo))
for?(int?i?=?0;?i?<?letter.length;?i++)?{
do?{
temp?=?new?Random().nextInt(allLetters.length);?//?生成隨機(jī)數(shù)方法一(allLetters數(shù)組下標(biāo)0~25)
//?temp?=?(int)?(Math.random()?*?allLetters.length);?//?生成隨機(jī)數(shù)方法二
letter[i]?=?allLetters[temp];?//?將allLetters數(shù)組中下標(biāo)為temp的元素賦值給letter數(shù)組中索引為i的元素
}?while?(isRep[temp]);
isRep[temp]?=?true;?//?letter每一次賦值完成后,將與allLetters數(shù)組下標(biāo)對(duì)應(yīng)的isRep數(shù)組下標(biāo)所對(duì)應(yīng)的元素值改為true
}
return?letter;
}
//?效率非常高且常用的生成隨機(jī)字符方法五(大小寫字母與數(shù)字組合)
public?static?char[]?generate5(int?n)?{
//?方法一:將26個(gè)大寫字母放到一個(gè)數(shù)組里面,然后隨機(jī)生成這個(gè)數(shù)組的索引(下標(biāo)),通過(guò)索引得到隨機(jī)字母
//?方法二:隨機(jī)生成26個(gè)大寫字母所對(duì)應(yīng)的int型數(shù)字(65~90),再轉(zhuǎn)換成字母即可
char[]?allLetters?=?{?'0',?'1',?'2',?'3',?'4',?'5',?'6',?'7',?'8',?'9',?'a',?'b',?'c',?'d',?'e',?'f',?'g',?'h',
'i',?'i',?'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];?//?創(chuàng)建一個(gè)boolean型數(shù)組,與allLetters長(zhǎng)度一致,所有元素默認(rèn)為false
char[]?letter?=?new?char[n];?//?創(chuàng)建數(shù)組接收隨機(jī)生成的n個(gè)字符
int?temp;?//?聲名數(shù)組索引(下標(biāo))
for?(int?i?=?0;?i?<?letter.length;?i++)?{
do?{
temp?=?new?Random().nextInt(allLetters.length);?//?生成隨機(jī)數(shù)方法一(allLetters數(shù)組下標(biāo)0~25)
//?temp?=?(int)?(Math.random()?*?allLetters.length);?//?生成隨機(jī)數(shù)方法二
letter[i]?=?allLetters[temp];?//?將allLetters數(shù)組中下標(biāo)為temp的元素賦值給letter數(shù)組中索引為i的元素
}?while?(isRep[temp]);
isRep[temp]?=?true;?//?letter每一次賦值完成后,將與allLetters數(shù)組下標(biāo)對(duì)應(yīng)的isRep數(shù)組下標(biāo)所對(duì)應(yīng)的元素值改為true
}
return?letter;
}
//?比較系統(tǒng)隨機(jī)生成的字符與用戶輸入的字符
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++;?//?猜中的字符個(gè)數(shù)
if?(i?==?j)?{
n++;?//?位置對(duì)應(yīng)的字符個(gè)數(shù)
}
break;
}
}
}
return?new?int[]?{?m,?n?};?//?將猜中的字符個(gè)數(shù)與位置對(duì)應(yīng)的字符個(gè)數(shù)存放到數(shù)組中
}
public?static?void?rank(int?n,?int?score)?{
if?(n?>=?4)?{
if?(score?>=?950)?{
System.out.println("恭喜你!猜對(duì)了!你的得分:"?+?score?+?"分!\n\n段位:========最強(qiáng)王者========");
}?else?if?(score?>=?900)?{
System.out.println("恭喜你!猜對(duì)了!你的得分:"?+?score?+?"分!\n\n段位:========超凡大師========");
}?else?if?(score?>=?850)?{
System.out.println("恭喜你!猜對(duì)了!你的得分:"?+?score?+?"分!\n\n段位:========璀璨鉆石========");
}?else?if?(score?>=?800)?{
System.out.println("恭喜你!猜對(duì)了!你的得分:"?+?score?+?"分!\n\n段位:========華貴鉑金========");
}?else?if?(score?>=?750)?{
System.out.println("恭喜你!猜對(duì)了!你的得分:"?+?score?+?"分!\n\n段位:========榮耀黃金========");
}?else?if?(score?>=?700)?{
System.out.println("恭喜你!猜對(duì)了!你的得分:"?+?score?+?"分!\n\n段位:========不屈白銀========");
}?else?if?(score?>=?600)?{
System.out.println("恭喜你!猜對(duì)了!你的得分:"?+?score?+?"分!\n\n段位:========英勇黃銅========");
}?else?{
System.out.println("恭喜你!猜對(duì)了!你的得分:"?+?score?+?"分!\n\n======加油吧!騷年!======");
}
}?else?{
System.out.println("大神!恭喜你!猜對(duì)了!你的得分:"?+?score?+?"分!\n\n=======這太小兒科了!你需要更大挑戰(zhàn)!=======");
}
}
}
猜字母游戲!練練手!大家來(lái)找找茬!
冰山點(diǎn)水
2016-06-09 11:16:37