package?com.jc1;
import?java.util.*;
public?class?GuessGame2?{
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分,每猜1次扣除10分,分數為0則退出游戲;\n"?
+?"4、游戲中字符長度可自定義選擇;\n"?
+?"5、猜字符過程中輸入“==”退出游戲。\n\n"
+?"------------------游戲開始!Ready!Go!------------------");
int?score?=?1000;?//?總分
int?chs?=?0,?loc?=?0;?//?聲明猜對字符的個數及位置正確的字符個數
int?choice,?count;
char[]?answer?=?null;
do?{
System.out.println("請選擇猜測的字符種類:\n"?
+?"1.數字????2.小寫字母????3.大寫字母????4.大小寫字母組合????5.大小寫字母與數字組合");
choice?=?scan.nextInt();
if?(choice?<?1?||?choice?>?5)?{
System.out.println("你的輸入有誤!請重新輸入!");
}
}?while?(choice?<?1?||?choice?>?5);
System.out.println("請自定義要猜測的字符長度:(數字1~10,字母1~26)");
count?=?scan.nextInt();?//?自定義字符的個數
scan.nextLine();?//?將nextInt()丟棄掉的回車符(用戶上一次輸入完成后的回車符)接收,以保證后續nextLine()能正常接收輸入
//?調用生成隨機字符方法(共有五種隨機方法)
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);?//?輸出系統隨機生成的字符
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);?//?調用比較方法返回有chs與loc信息的數組check
chs?=?var[0];
loc?=?var[1];
System.out.println("你猜對字符個數:"?+?chs?+?",其中位置正確的字符個數:"?+?loc);
score?-=?10;
if?(score?==?0)?{
System.out.println("你太差勁了!IQ有待提升!");
break;
}
if?(chs?==?count?&&?loc?==?count)?{
rank(count,?score);?//?調用rank方法輸出段位
}
}?while?(chs?!=?count?||?loc?!=?count);
scan.close();?//?關閉輸出
}
//?效率非常高且常用的生成隨機字符方法一(純數字)
public?static?char[]?generate1(int?n)?{
//?方法一:將26個大寫字母放到一個數組里面,然后隨機生成這個數組的索引(下標),通過索引得到隨機字母
//?方法二:隨機生成26個大寫字母所對應的int型數字(65~90),再轉換成字母即可
char[]?allLetters?=?{?'0',?'1',?'2',?'3',?'4',?'5',?'6',?'7',?'8',?'9'?};
boolean[]?isRep?=?new?boolean[allLetters.length];?//?創建一個boolean型數組,與allLetters長度一致,所有元素默認為false
char[]?letter?=?new?char[n];?//?創建數組接收隨機生成的n個字符
int?temp;?//?聲名數組索引(下標)
for?(int?i?=?0;?i?<?letter.length;?i++)?{
do?{
temp?=?new?Random().nextInt(allLetters.length);?//?生成隨機數方法一(allLetters數組下標0~25)
//?temp?=?(int)?(Math.random()?*?allLetters.length);?//?生成隨機數方法二
letter[i]?=?allLetters[temp];?//?將allLetters數組中下標為temp的元素賦值給letter數組中索引為i的元素
}?while?(isRep[temp]);
isRep[temp]?=?true;?//?letter每一次賦值完成后,將與allLetters數組下標對應的isRep數組下標所對應的元素值改為true
}
return?letter;
}
//?效率非常高且常用的生成隨機字符方法二(小寫字母)
public?static?char[]?generate2(int?n)?{
//?方法一:將26個大寫字母放到一個數組里面,然后隨機生成這個數組的索引(下標),通過索引得到隨機字母
//?方法二:隨機生成26個大寫字母所對應的int型數字(65~90),再轉換成字母即可
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];?//?創建一個boolean型數組,與allLetters長度一致,所有元素默認為false
char[]?letter?=?new?char[n];?//?創建數組接收隨機生成的n個字符
int?temp;?//?聲名數組索引(下標)
for?(int?i?=?0;?i?<?letter.length;?i++)?{
do?{
temp?=?new?Random().nextInt(allLetters.length);?//?生成隨機數方法一(allLetters數組下標0~25)
//?temp?=?(int)?(Math.random()?*?allLetters.length);?//?生成隨機數方法二
letter[i]?=?allLetters[temp];?//?將allLetters數組中下標為temp的元素賦值給letter數組中索引為i的元素
}?while?(isRep[temp]);
isRep[temp]?=?true;?//?letter每一次賦值完成后,將與allLetters數組下標對應的isRep數組下標所對應的元素值改為true
}
return?letter;
}
//?效率非常高且常用的生成隨機字符方法三(大寫字母)
public?static?char[]?generate3(int?n)?{
//?方法一:將26個大寫字母放到一個數組里面,然后隨機生成這個數組的索引(下標),通過索引得到隨機字母
//?方法二:隨機生成26個大寫字母所對應的int型數字(65~90),再轉換成字母即可
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];?//?創建一個boolean型數組,與allLetters長度一致,所有元素默認為false
char[]?letter?=?new?char[n];?//?創建數組接收隨機生成的n個字符
int?temp;?//?聲名數組索引(下標)
for?(int?i?=?0;?i?<?letter.length;?i++)?{
do?{
temp?=?new?Random().nextInt(allLetters.length);?//?生成隨機數方法一(allLetters數組下標0~25)
//?temp?=?(int)?(Math.random()?*?allLetters.length);?//?生成隨機數方法二
letter[i]?=?allLetters[temp];?//?將allLetters數組中下標為temp的元素賦值給letter數組中索引為i的元素
}?while?(isRep[temp]);
isRep[temp]?=?true;?//?letter每一次賦值完成后,將與allLetters數組下標對應的isRep數組下標所對應的元素值改為true
}
return?letter;
}
//?效率非常高且常用的生成隨機字符方法四(大小寫字母)
public?static?char[]?generate4(int?n)?{
//?方法一:將26個大寫字母放到一個數組里面,然后隨機生成這個數組的索引(下標),通過索引得到隨機字母
//?方法二:隨機生成26個大寫字母所對應的int型數字(65~90),再轉換成字母即可
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];?//?創建一個boolean型數組,與allLetters長度一致,所有元素默認為false
char[]?letter?=?new?char[n];?//?創建數組接收隨機生成的n個字符
int?temp;?//?聲名數組索引(下標)
for?(int?i?=?0;?i?<?letter.length;?i++)?{
do?{
temp?=?new?Random().nextInt(allLetters.length);?//?生成隨機數方法一(allLetters數組下標0~25)
//?temp?=?(int)?(Math.random()?*?allLetters.length);?//?生成隨機數方法二
letter[i]?=?allLetters[temp];?//?將allLetters數組中下標為temp的元素賦值給letter數組中索引為i的元素
}?while?(isRep[temp]);
isRep[temp]?=?true;?//?letter每一次賦值完成后,將與allLetters數組下標對應的isRep數組下標所對應的元素值改為true
}
return?letter;
}
//?效率非常高且常用的生成隨機字符方法五(大小寫字母與數字組合)
public?static?char[]?generate5(int?n)?{
//?方法一:將26個大寫字母放到一個數組里面,然后隨機生成這個數組的索引(下標),通過索引得到隨機字母
//?方法二:隨機生成26個大寫字母所對應的int型數字(65~90),再轉換成字母即可
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];?//?創建一個boolean型數組,與allLetters長度一致,所有元素默認為false
char[]?letter?=?new?char[n];?//?創建數組接收隨機生成的n個字符
int?temp;?//?聲名數組索引(下標)
for?(int?i?=?0;?i?<?letter.length;?i++)?{
do?{
temp?=?new?Random().nextInt(allLetters.length);?//?生成隨機數方法一(allLetters數組下標0~25)
//?temp?=?(int)?(Math.random()?*?allLetters.length);?//?生成隨機數方法二
letter[i]?=?allLetters[temp];?//?將allLetters數組中下標為temp的元素賦值給letter數組中索引為i的元素
}?while?(isRep[temp]);
isRep[temp]?=?true;?//?letter每一次賦值完成后,將與allLetters數組下標對應的isRep數組下標所對應的元素值改為true
}
return?letter;
}
//?比較系統隨機生成的字符與用戶輸入的字符
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("恭喜你!猜對了!你的得分:"?+?score?+?"分!\n\n段位:========最強王者========");
}?else?if?(score?>=?900)?{
System.out.println("恭喜你!猜對了!你的得分:"?+?score?+?"分!\n\n段位:========超凡大師========");
}?else?if?(score?>=?850)?{
System.out.println("恭喜你!猜對了!你的得分:"?+?score?+?"分!\n\n段位:========璀璨鉆石========");
}?else?if?(score?>=?800)?{
System.out.println("恭喜你!猜對了!你的得分:"?+?score?+?"分!\n\n段位:========華貴鉑金========");
}?else?if?(score?>=?750)?{
System.out.println("恭喜你!猜對了!你的得分:"?+?score?+?"分!\n\n段位:========榮耀黃金========");
}?else?if?(score?>=?700)?{
System.out.println("恭喜你!猜對了!你的得分:"?+?score?+?"分!\n\n段位:========不屈白銀========");
}?else?if?(score?>=?600)?{
System.out.println("恭喜你!猜對了!你的得分:"?+?score?+?"分!\n\n段位:========英勇黃銅========");
}?else?{
System.out.println("恭喜你!猜對了!你的得分:"?+?score?+?"分!\n\n======加油吧!騷年!======");
}
}?else?{
System.out.println("大神!恭喜你!猜對了!你的得分:"?+?score?+?"分!\n\n=======這太小兒科了!你需要更大挑戰!=======");
}
}
}
添加回答
舉報
0/150
提交
取消