猜单词

自己的答案

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 + "次机会");

                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

老师的答案


  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随机从题库中挑出一个单词给用户猜
        //题库怎么表达---字符串数组
        String[] words = {"static","abstract","extends","implements","throw","orange","student","select","group","interface"};
        //随机选出一个单词
        Random random =  new Random();
        int randomIndex = random.nextInt(words.length);//随机下标

        String selectWord = words[randomIndex];
        System.out.println(selectWord);

        //定义一个字符数组保存用户当前单词的猜中部分
         char[] wordNow = new char[selectWord.length()];

         //将字符数组中的每个字符初始设置成-
        for(int i = 0; i < wordNow.length; i++)
        {
            wordNow[i] = '-';
        }

       printWords(wordNow);
        //2.向用户输出和选出单词同等数量的横线-
        //需要知道单词有多少个字符 selectWord.length()


       int userTimes = 5;//用户可以猜5次;
       //让用户开始猜,接受用户输入一个字符,判断用户输入的字符是否在单词中
        //如果不在,机会减一;如果猜对了,就在字符对应的位置将该字符显示出来

        //上述步骤循环执行,有2种情况跳出循环
        //1.用户猜出了单词的全部字符,用户赢了
        //2.用户的机会减为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)//如果不在,机会减一;输出还剩多少次机会,并且输出当前单词的猜中部分
            {
                userTimes--;
                if(userTimes == 0)
                {
                    break;
                }
                System.out.println("还剩"+userTimes+"次机会");
                printWords(wordNow);
            }
            else//如果猜对了,就在字符对应的位置将该字符显示出来,其他的位置依然打印-
            {
                //在字符对应的位置将该字符显示出来,其他的位置依然打印-(有可能该字符在单词中出现多次,都要变身)
                for(int i = 0; i < selectWord.length(); i++)
                {
                    //取出单词的每一个字符
                    char tempC = selectWord.charAt(i);
                    if(tempC == strGuess.charAt(0))
                    {
                        wordNow[i] = tempC;
                    }
                }

                printWords(wordNow);
                //如果wordNow数组中不在包含-,说明所有的字符全部被猜出来了
                String strWordNow = String.valueOf(wordNow);//字符数组转为字符串
                if(!strWordNow.contains("-"))
                {
                    break;
                }

            }
        }

        //判断最终结果
        //1.用户猜出了单词的全部字符,用户赢了
        //2.用户的机会减为0了,用户输了
        if(userTimes > 0)//说明不是因为机会没了跳出的循环
        {
            System.out.println("恭喜,你猜对啦");
        }
        else
        {
            System.out.println("你输了,正确答案是");
            System.out.println(selectWord);

        }
    }
}


给定指定文件读出文件里的单词作为数组

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一个单词数组(字符串数组)
//2随机获取一个单词,随机一个下标(下标值在0到单词数组长度之间)
//3得到单词长度len,创建一个长度为len的字符数组word,数组元素初值均为-
//4循环以下步骤(退出循环的条件为,用户允许猜错的次数<=0或word字符中数组中不再包含-字符)
//5输出word到屏幕
//6获取客户输入,判断随机选出的单词中是否包含用户输入的字符
//7若包含,得到字符在单词中出现的位置,并将该位置的字符替换成用户输入的字符,返回到4
//8若不包含,用户可以猜错的次数减一,返回到4
//9.得出最后结论:判断用户允许猜错的次数是否大于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 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 + "次机会");
                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) + "次机会");
            System.out.println(worlds[index]);
        } else {

            System.out.println("你输啦");
        }


    }
}


你可能感兴趣的:(猜单词)