自己的答案
public class Guessworld {
public static void main(String[] args) {
String[] worlds = {"extends", "interface", "abstract", "implements", "throw", "orange", "student"};
Random random = new Random();
int index = random.nextInt(worlds.length);
// System.out.println(worlds[index]);
char[] world = new char[worlds[index].length()];
for (int i = 0; i < world.length; i++) {
world[i] = '-';
System.out.print(world[i]);
}
Scanner scanner = new Scanner(System.in);
int time = 5;
Guesstext guesstext = new Guesstext();
while (true) {
System.out.println();
char userg = scanner.next().charAt(0);
int num = worlds[index].indexOf(userg);
if (num < 0) {
time--;
if (time == 0) {
break;
}
System.out.println("還剩" + time + "次機(jī)會");
guesstext.src(world);
} else {
for (int i = 0; i < world.length; i++) {
if (userg == worlds[index].charAt(i)) {
world[i] = userg;
}
}
guesstext.src(world);
System.out.println();
}
String sre = String.valueOf(world);
if (!sre.contains("-")) {
break;
}
}
if (time > 0) {
System.out.println("你贏了!");
} else {
System.out.println("你輸了");
System.out.println(worlds[index]);
}
}
}
public class Guesstext {
public void src(char[] world){
for (int i = 0;i<world.length;i++)
{
System.out.print( world[i]);
}
}
}
老師的答案
public static void printWords(char[] wordNow)
{
for(int i = 0; i < wordNow.length; i++)
{
System.out.print(wordNow[i]);
}
}
public static void main(String[] args) {
//1隨機(jī)從題庫中挑出一個單詞給用戶猜
//題庫怎么表達(dá)---字符串?dāng)?shù)組
String[] words = {"static","abstract","extends","implements","throw","orange","student","select","group","interface"};
//隨機(jī)選出一個單詞
Random random = new Random();
int randomIndex = random.nextInt(words.length);//隨機(jī)下標(biāo)
String selectWord = words[randomIndex];
System.out.println(selectWord);
//定義一個字符數(shù)組保存用戶當(dāng)前單詞的猜中部分
char[] wordNow = new char[selectWord.length()];
//將字符數(shù)組中的每個字符初始設(shè)置成-
for(int i = 0; i < wordNow.length; i++)
{
wordNow[i] = '-';
}
printWords(wordNow);
//2.向用戶輸出和選出單詞同等數(shù)量的橫線-
//需要知道單詞有多少個字符 selectWord.length()
int userTimes = 5;//用戶可以猜5次;
//讓用戶開始猜思犁,接受用戶輸入一個字符衬鱼,判斷用戶輸入的字符是否在單詞中
//如果不在些膨,機(jī)會減一证薇;如果猜對了能犯,就在字符對應(yīng)的位置將該字符顯示出來
//上述步驟循環(huán)執(zhí)行师脂,有2種情況跳出循環(huán)
//1.用戶猜出了單詞的全部字符,用戶贏了
//2.用戶的機(jī)會減為0了伯诬,用戶輸了
while(true)
{
Scanner scanner = new Scanner(System.in);
System.out.println();
String strGuess = scanner.next();//用戶輸入的字符z
// boolean bIsContains = selectWord.contains(strGuess);
int nIndex = selectWord.indexOf(strGuess);//interface
if(nIndex < 0)//如果不在,機(jī)會減一嗅战;輸出還剩多少次機(jī)會,并且輸出當(dāng)前單詞的猜中部分
{
userTimes--;
if(userTimes == 0)
{
break;
}
System.out.println("還剩"+userTimes+"次機(jī)會");
printWords(wordNow);
}
else//如果猜對了,就在字符對應(yīng)的位置將該字符顯示出來,其他的位置依然打印-
{
//在字符對應(yīng)的位置將該字符顯示出來,其他的位置依然打印-(有可能該字符在單詞中出現(xiàn)多次驮捍,都要變身)
for(int i = 0; i < selectWord.length(); i++)
{
//取出單詞的每一個字符
char tempC = selectWord.charAt(i);
if(tempC == strGuess.charAt(0))
{
wordNow[i] = tempC;
}
}
printWords(wordNow);
//如果wordNow數(shù)組中不在包含-疟呐,說明所有的字符全部被猜出來了
String strWordNow = String.valueOf(wordNow);//字符數(shù)組轉(zhuǎn)為字符串
if(!strWordNow.contains("-"))
{
break;
}
}
}
//判斷最終結(jié)果
//1.用戶猜出了單詞的全部字符,用戶贏了
//2.用戶的機(jī)會減為0了,用戶輸了
if(userTimes > 0)//說明不是因為機(jī)會沒了跳出的循環(huán)
{
System.out.println("恭喜东且,你猜對啦");
}
else
{
System.out.println("你輸了,正確答案是");
System.out.println(selectWord);
}
}
}
給定指定文件讀出文件里的單詞作為數(shù)組
package exprerans;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
/**
* Created by ttc on 18-1-5.
*/
//1一個單詞數(shù)組(字符串?dāng)?shù)組)
//2隨機(jī)獲取一個單詞启具,隨機(jī)一個下標(biāo)(下標(biāo)值在0到單詞數(shù)組長度之間)
//3得到單詞長度len,創(chuàng)建一個長度為len的字符數(shù)組word珊泳,數(shù)組元素初值均為-
//4循環(huán)以下步驟(退出循環(huán)的條件為富纸,用戶允許猜錯的次數(shù)<=0或word字符中數(shù)組中不再包含-字符)
//5輸出word到屏幕
//6獲取客戶輸入,判斷隨機(jī)選出的單詞中是否包含用戶輸入的字符
//7若包含旨椒,得到字符在單詞中出現(xiàn)的位置晓褪,并將該位置的字符替換成用戶輸入的字符,返回到4
//8若不包含综慎,用戶可以猜錯的次數(shù)減一涣仿,返回到4
//9.得出最后結(jié)論:判斷用戶允許猜錯的次數(shù)是否大于0,如果大于0示惊,輸出用戶勝利好港,否則輸出用戶失敗
public class Guessworlw {
public static void bianli(char[] w) {
for (int i = 0; i < w.length; i++) {
System.out.print(w[i]);
}
}
public static void main(String[] args) throws IOException {
FileReader fileReader = new FileReader("e:/words.txt");
BufferedReader bufferedReader = new BufferedReader(fileReader);
List<String> stringList = new ArrayList<>();
String strWord = bufferedReader.readLine();
while (strWord != null) {
stringList.add(strWord);
strWord = bufferedReader.readLine();
}
String[] worlds = new String[stringList.size()];
stringList.toArray(worlds);
//String[] worlds = {"extends", "interface", "abstract", "implements", "throw", "orange", "student"};
Random random = new Random();
int index = random.nextInt(worlds.length);
char[] w = new char[worlds[index].length()];
System.out.println(worlds[index]);
for (int i = 0; i < w.length; i++) {
w[i] = '-';
System.out.print(w[i]);
}
int times = 3;
while (true) {
Scanner scanner = new Scanner(System.in);
System.out.println();
char userguess = scanner.next().charAt(0);
int aire = worlds[index].indexOf(userguess);
if (aire < 0) {
times--;
if (times == 0) {
break;
}
System.out.println("還剩" + times + "次機(jī)會");
bianli(w);
} else {
for (int i = 0; i < worlds[index].length(); i++) {
char user = worlds[index].charAt(i);
if (user == userguess) {
w[i] = user;
}
}
bianli(w);
}
String nowworld = String.valueOf(w);
if (!nowworld.contains("-")) {
break;
}
}
if (times > 0) {
System.out.println("你贏了!共用了" + (3 - times) + "次機(jī)會");
System.out.println(worlds[index]);
} else {
System.out.println("你輸啦");
}
}
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者