您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)Java怎樣實(shí)現(xiàn)猜字小游戲,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
語法基礎(chǔ):StringBuilder 類似列表,可以更改元素。
package Practice;
public class tt {
public static void main(String[] args) {
String str = "banana";
System.out.println(str.indexOf('z')); // -1
System.out.println(str.indexOf('a', 2)); // 3
StringBuilder words = new StringBuilder();
for (int i = 0; i < 5; i++)
words.append('*'); // "*****"
System.out.println(words.length()); // 5
System.out.println(words.indexOf("a"));// -1
System.out.println(words.indexOf("*", 1)); // 1
words.setCharAt(3, 'a'); // "***a*"
System.out.println(words);
}
}
package Practice;
import java.util.Scanner;
public class Guess {
public static String words[] = {"banana", "telecommunication", "programming", "bupt"};
public static boolean[] guessed = new boolean[words.length]; // 判斷猜過
public static int num_guessed = 0; // 猜過的單詞數(shù)量
public static char keep; // 是否繼續(xù)y or n
public static void main(String[] args) {
// for(int i = 0; i < guessed.length; i ++ ) System.out.println(guessed[i]);
Scanner scanner = new Scanner(System.in);
do{
// 隨機(jī)產(chǎn)生要猜測的單詞 ans
int index = (int) (Math.random() * words.length);
String ans = words[index];
// 再來一次時(shí)的重復(fù)檢測
while(guessed[index] == true)
{
index = (int) (Math.random() * words.length);
ans = words[index];
}
// 初始化,StringBuilder類似list
StringBuilder guessedWord = new StringBuilder();
for (int i = 0; i < ans.length(); i++)
guessedWord.append('*');
int numberOfCorrectLettersGuessed = 0, numberOfMisses = 0;
// 模擬過程
while (numberOfCorrectLettersGuessed < ans.length()) {
System.out.print("(Guess) Enter a letter in word " + guessedWord
+ " > ");
String s = scanner.nextLine();
char letter = s.charAt(0);
if (guessedWord.indexOf(letter + "") >= 0) { // 猜中,但重復(fù),不算錯(cuò)誤次數(shù)
System.out.println(" " + letter + " is already in the word");
} else if (ans.indexOf(letter) < 0) { // 猜錯(cuò)
System.out.println(" " + letter + " is not in the word");
numberOfMisses++;
} else { // 猜中,進(jìn)行標(biāo)記與賦值
int k = ans.indexOf(letter);
while (k >= 0) {
guessedWord.setCharAt(k, letter);
numberOfCorrectLettersGuessed++;
k = ans.indexOf(letter, k + 1);
}
}
}
System.out.println("The word is " + ans + ". You missed "
+ numberOfMisses + ((numberOfMisses <= 1) ? " time" : " times"));
guessed[index] = true;
num_guessed += 1;
if(num_guessed < words.length)
{
System.out.print("Do you want to guess for another word? Enter y or n> ");
keep = scanner.nextLine().charAt(0);
}
}while(keep == 'y' && num_guessed < words.length);
if(keep == 'y')
System.out.println("Perfect!!!");
else
System.out.println("You have guessed " + num_guessed + ((num_guessed <= 1) ? " word~" : " words~"));
}
}
關(guān)于“Java怎樣實(shí)現(xiàn)猜字小游戲”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。