Introduction to Java Programming编程题9.31<刽子手游戏>

/*
Enter a letter in word: ******** > t
t is not in the word.
Enter a letter in word: ******** > a
Enter a letter in word: ******a* > l
Enter a letter in word: ******al > n
Enter a letter in word: *****nal > o
Enter a letter in word: ****onal > s
Enter a letter in word: ***sonal > a
a is already in the word.
Enter a letter in word: ***sonal > r
Enter a letter in word: **rsonal > e
Enter a letter in word: *ersonal > p
The word is: personal. You missed 2 times.
Do you want to guess for another word? Enter y or n> y
Enter a letter in word: *** > h
Enter a letter in word: *h* >
-------------------------------------------------------
Enter a letter in word: ** > o
o is not in the word.
Enter a letter in word: ** > i
Enter a letter in word: i* > t
The word is: it. You missed 1 time.
Do you want to guess for another word? Enter y or n> i
Invalid value.
Do you want to guess for another word? Enter y or n> n
Done.
 */
import java.util.Scanner;
import java.util.Random;

public class GuessWord {
    public static void main(String[] args) {
        String[] words = {"many", " experts", "call", "it", "the", "best",
                "personal", "computer", "operating", "system", "on", "earth"};

        int key;
        Random random = new Random(); 
        Scanner input = new Scanner(System.in);

        for (int j = 0; j < words.length; j++) {
            key = random.nextInt(11); // 生成0~11的随机整数

            System.out.print("Enter a letter in word: ");
            char[] guess = initialize(words[key]);

            int missedTimes = guess(guess, words[key]);

            System.out.println("The word is: " + words[key] + ". You missed " + missedTimes
                    + ((missedTimes > 1) ? " times." : " time."));

            while (j < words.length) {
                System.out.print("Do you want to guess for another word? Enter y or n> ");
                String s = input.next();
                if (s.charAt(0) == 'n') {
                    j = words.length;
                    System.out.println("Done.");
                }
                else if (s.charAt(0) == 'y')
                    break;
                else
                    System.out.println("Invalid value.");
            }
        }
    }

    public static char[] initialize(String word) {
    /* 创建char数组guess并将其全部初始化为'*',不建议使用字符串,使用字符串
     * 处理的话会很麻烦。
     */
        char[] guess = new char[word.length()]; 

        for (int i = 0; i < word.length(); i++) {
            guess[i] = '*';
            System.out.print(guess[i]);
        }
        System.out.print(" > ");

        return guess;
    }

    public static int guess(char[] g, String w) {
        Scanner input = new Scanner(System.in);
        int i, missedTimes = 0, j = 0;

        while (j < w.length()) {
            String s = input.next();
            boolean equalWS = true;

            for (i = 0; i < w.length(); i++)
                if (w.charAt(i) == s.charAt(0) && g[i] == s.charAt(0)) {
                    System.out.println(s.charAt(0) + " is already in the word.");
                    missedTimes++;
                    break;
                }
            if (i == w.length()) {
                for (i = 0; i < w.length(); i++) {
                    if (w.charAt(i) == s.charAt(0)) {
                        g[i] = s.charAt(0);
                        j++;
                        equalWS = false;
                    } else if (equalWS && (i == w.length() - 1)) {
                        System.out.println(s.charAt(0) + " is not in the word.");
                        missedTimes++;
                    }
                }
            }
            if (j < w.length()) // 如果去掉if (j < w.length()),while循环会多执行一次
                continueGuess(g);
        }
        return missedTimes;
    }

    public static void continueGuess(char[] g) {
        System.out.print("Enter a letter in word: ");
        for (int i = 0; i < g.length; i++)
            System.out.print(g[i]);
        System.out.print(" > ");
    }
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值