猜字游戏

猜字游戏

要求:
创建一个经典的美国游戏,这是一个猜谜游戏。用户需要通过输入逐个字符来猜测一个单词。

要猜测的词由一排- - - - - - -表示(每一个‘-’代表相应字母的位置)。
如果玩家猜到单词中存在的字母,脚本会以所有正确的位置写入该字母。
玩家有10个回合来猜单词。

您可以通过更改变量轻松自定义游戏。您将使用计数器和循环。示例执行:Secret Word: - - - - - - -
Enter Letter: a
Output: There is no letter ‘a’ in the secret word. You have 9 tries left
Enter Letter: d
Secret Word: - - d - - - -
等等。

如果用户在最大猜测次数之前猜到了正确的单词,则将显示单词和消息“Congratulations: You guessed the correct WORD”。比如Secret Word: Midterm. Congratulations: You guessed the correct WORD.
如果用户在最大猜测次数之前没有猜到正确的单词,则将显示一条消息。 例如:You have reached your maximum guesses. The secret word was Midterm. You failed to guess the secret word.

请记住,用户最多只能猜测 10 次。

注意:该字母将更换为其所有位置。 例如:如果机密单词是Welcome,并且用户猜到了字母"e"。 字母"e"在单词中出现 2 次,因此在字母的所有匹配项中替换短划线。 例如: - e - - - - e

代码

import java.util.Scanner;

/**
 * @Author ZR
 * @Time 2019/11/9 7:37
 * @Description 猜字游戏
 */
public class GuessGame {
    public static void main(String[] args) {
        //需要猜的单词
        String word="welcome";
        //猜测的单词
        String guessWord="-------";
        Scanner sc=new Scanner(System.in);
        System.out.println("Secret Word: "+guessWord);
        //一共有10此猜错的机会
        int times=10;
        while (times>0){
            String guess="";
            System.out.print("Enter Letter:");
            //输入字母
            String letter=sc.next();
            //如果word中不包含输入的字母
            if (!word.contains(letter)){
                times--;
                System.out.println("Output: There is no letter '"+letter+"' in the secret word. You have "+times+" tries left");
            }else {//如果word中包含输入的字母
                System.out.print("Secret Word: ");
                //遍历word
                for (int j=0;j<word.length();j++){
                    //获取对应坐标的字符
                    String ch=word.charAt(j)+"";
                    //如果对应坐标的字符和输入的字符一样
                    if (ch.equals(letter)){
                        //输出该字符
                        System.out.print(letter);
                        //拼接到guess
                        guess=guess+ch;
                    }else {//如果对应坐标的字符和输入的字符一样
                        //获取已经猜测的单词对应坐标的字符
                        String ch2=guessWord.charAt(j)+"";
                        //输出
                        System.out.print(ch2);
                        //拼接到guess
                        guess=guess+ch2;
                    }
                }
                //换行输出
                System.out.println();
                //重新赋值猜测的单词
                guessWord=guess;

                //如果猜测的单词和需要猜的单词一样,就输出成功,并结束进程
                if (word.equals(guessWord)){
                    System.out.println("Secret Word: "+guessWord+". Congratulations: You guessed the correct WORD.");
                    return;
                }
            }
        }
        //遍历10次仍未猜出,输出失败
        System.out.println("You have reached your maximum guesses. The secret word was "+word+". You failed to guess the secret word.");
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值