亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

猜字母游戲!練練手!大家來找找茬!

猜字母游戲!練練手!大家來找找茬!

冰山點水 2016-06-09 11:16:37
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=======這太小兒科了!你需要更大挑戰!======="); } } }
查看完整描述

6 回答

?
Genment

TA貢獻43條經驗 獲得超25個贊

挺不錯的,但是為什么45行和57行要用到 label a 呢?直接 continue 不可以嗎?

查看完整回答
1 反對 回復 2016-06-09
  • 冰山點水
    冰山點水
    謝謝提醒!之前中間加了其它循環體,改了之后忘記刪掉a標簽了
  • Genment
    Genment
    哈哈,加油!
?
奈何MJ

TA貢獻1條經驗 獲得超0個贊

讓我想起了我大一的時候

查看完整回答
反對 回復 2016-06-10
?
此生不變丶

TA貢獻36條經驗 獲得超19個贊

大神幫忙回答下我這個問題行不http://www.xianlaiwan.cn/wenda/detail/319256

查看完整回答
反對 回復 2016-06-09
?
求學者ph

TA貢獻6條經驗 獲得超3個贊

做的真好 我現在看都看不懂呢

查看完整回答
反對 回復 2016-06-09
  • 6 回答
  • 2 關注
  • 2806 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號