黑马程序员技术交流社区

标题: Java语言程序设计(基础篇)编程练习题 [打印本页]

作者: tljwh    时间: 2016-12-4 15:38
标题: Java语言程序设计(基础篇)编程练习题
本帖最后由 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);
                                }
                                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 = finalGuess.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 = 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)) {
                                System.out.println(ch + " is already in the word");
                                c[0]++;
                                break;
                        }
                        if(ch.equals(word)) {
                                fG = ch;               
                        }
                }
                return fG;
        }
         
        public static boolean isRight(String[] f, String[] g) {
                boolean yon = Arrays.equals(f, g);
                return !yon;
        }
}





作者: 百度还是阿里    时间: 2016-12-4 18:13
厉害了666
作者: lieyemu    时间: 2016-12-4 23:03
赞一个              




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2