A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

tljwh

初级黑马

  • 黑马币:44

  • 帖子:14

  • 精华:0

本帖最后由 tljwh 于 2016-12-8 12:35 编辑

Java语言程序设计(基础篇)编程练习题7.35---------------------------------------------------------------------
2016.12.08增加内容:
忘了把题目附上了........便于一贴式服务
代码还有改进空间,比如if(Arrays.equals(fG, finalGuess) && (c[0] != i))和前面的int i =1; 里的i就是多余的
题目:7.35(游戏:猜字游戏)编写一个猜字游戏。随机产生一个单词,提示用户一次猜测一个字母,如运行示例所示。单词中的每个字母显示为一个星号。当用户猜测正确后,正确的字母显示出来。当用户猜出一个单词,显示猜错的次数,并且询问用户是否据需对另外一个单词进行游戏。声明一个数组来存储单词,如下所示:String[] words = {"tom", "help", "black", ........};
(guess) enter a letter in word ***** > b
(guess) enter a letter in word b**** > l
(guess) enter a letter in word bl*** > b
b is already in the word
(guess) enter a letter in word bl*** > a
(guess) enter a letter in word bla** > n
n is not in the word
(guess) enter a letter in word bla** > k
(guess) enter a letter in word bla*k > c
the word is black. you missed 1 time
do you want to guess another word? enter y or n>
n
byebye


---------------------------------------------------------------------
[Java] 纯文本查看 复制代码
import java.util.Arrays;
import java.util.Scanner;

public class Test {        
        public static void main(String[] args){
                String[] words = {"a", "my", "tom", "help", "black"};
                Scanner input = new Scanner(System.in);
                boolean again = true;
                
                while(again) {
                        String[] guessWord = getGuessWord(words);
                        String[] finalGuess = new String[guessWord.length];
                        Arrays.fill(finalGuess, "*");
                        int counts = 0;
                        String[] fG = new String[guessWord.length];
                        Arrays.fill(fG, "*");
                        
                        do {
                                int[] c = {0};
                                System.out.print("(guess) enter a letter in word ");
                                for(int i = 0; i < guessWord.length; i++) {
                                        System.out.print(finalGuess[i]);
                                }
                                System.out.print(" > ");
                                String youGuess = input.next();
                                fG = guess(youGuess, guessWord, fG, c);
                                int i = 1;
                                if(Arrays.equals(fG, finalGuess) && (c[0] != i)) {
                                System.out.println(youGuess + " is not in the word");
                                counts++;
                                }
                                System.arraycopy(fG, 0, finalGuess, 0, fG.length);
                        }while(isRight(finalGuess, guessWord));
                        
                        char[] finalGuessChar = new char[finalGuess.length];
                        
                        for(int i = 0; i < finalGuess.length; i++) {
                                finalGuessChar[i] = finalGuess[i].charAt(0);
                        }
                        
                        System.out.print("the word is ");
                        System.out.print(finalGuessChar);
                        System.out.println(". " + "you missed " + counts + " time");
                        System.out.println("do you want to guess another word? enter y or n>");
                        
                        String enter = input.next();
                        if(enter.toUpperCase().equals("Y")) {
                                again = true;
                        }else {
                                again = false;
                                System.out.println("byebye");
                        }
                }
        }
        
        public static String[] getGuessWord(String[] words) {
                String word = words[(int)(Math.random() * words.length)];
                String[] charr = new String[word.length()];
                for(int i = 0; i < word.length(); i++) {
                        charr[i] = word.charAt(i) + "";
                }
                return charr;
        }
        
        public static String[] guess(String ch, String[] word, String[] fG, int[] c) {
                for(int i = 0; i < word.length; i++) {
                        if(ch.equals(fG[i])) {
                                System.out.println(ch + " is already in the word");
                                c[0]++;
                                break;
                        }
                        if(ch.equals(word[i])) {
                                fG[i] = ch;                
                        }
                }
                return fG;
        }
         
        public static boolean isRight(String[] f, String[] g) {
                boolean yon = Arrays.equals(f, g);
                return !yon;
        }
}




2 个回复

倒序浏览
厉害了666
回复 使用道具 举报
赞一个              
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马