猜字游戏

编写一个猜字的游戏,随机产生一个单词 提示用户一次猜测一个字母
如运行实例所示,单词中的每个字母显示为一个星号, 当用户猜测正确后
正确的字母显示出来,当用户猜出一个单词, 显示猜错的个数, 并且询问用户是否继续对另一个单词进行游戏。声明一个数组来储存单词
在这里插入图片描述

/**
 *编写一个猜字的游戏 随机产生一个单词 提示用户一次猜测一个字母
 如运行实例所示 单词中的每个字母显示为一个星号 当用户猜测正确后
 正确的字母显示出来
 当用户猜出一个单词 显示猜错的个数 并且询问用户是否继续对另一个单词
  进行游戏 声明一个数组来储存单词
 */
import java.util.*;
class test581{
  public static void main(String[] args){
    Scanner scanner =new Scanner(System.in);
   //1,制作一个单词表
   String [] words=new String[]{"java","hhjhj","ionkd","great"};
   while(true){
   //2,从单词表中随机抽取一个单词
   Random random =new Random();
   String word=words[random.nextInt(words.length)];
   //3,制作一个单词表的状态表 true和false 默认情况是false
   boolean [] status=new boolean[word.length()];
   int miss=0;  //显示错误的次数
   //4,开始猜一个单词
    while(true){
   //5,根据单词和状态表获取密文
   String cipherTest=getCiphertext(word,status);
   //6,提示用户输入字母并输出密文
   System.out.print("Enter a letter in word"+cipherTest+" >");
   char letter=scanner.nextLine().charAt(0);
   //7,判断该单词是否包含这个字母
     if(isContains(word,letter)){
        if(!changeWordStatus(word,status,letter)){
           System.out.print("\t"+letter+"is already in the word");
        }
     }
     else{
         System.out.print("\t"+letter+" is not in the word");
         miss++;
     }
     if(isFinish(status)){
         System.out.print("the word is "+word+"You miss"+miss+"time");
         break;
     }
    }
    System.out.print("do you want to another word? Enter y or n:");
    String choice =scanner.nextLine();
    if(choice.equals("n")){
        System.out.print("welcome ! Thank you ! FUCK PROGRAM!");
        break;
    }
}
  }
  public static boolean isFinish(boolean []status){
             for(int i=0;i<status.length;i++){
                 if(!status[i]){
                     return false;
                 }
             }
             return true;
  }
  public static boolean changeWordStatus(String word,boolean [] status,char letter){
        for(int i=0;i<word.length();i++){
            if(word.charAt(i)==letter){
                if(status[i]){
                    return true;
                }
                else{
                    status[i]=true;
                }
            }
        }
        return true;
  }
  public static boolean isContains(String word,char letter){
        for(int i=0;i<word.length();i++){
            if(word.charAt(i)==letter){
                return true;
            }
        }
         return false;
  }
  public static String getCiphertext(String word,boolean [] status){
       String ciphertext="";
       for(int i=0;i<word.length();i++){
           if(status[i]){//status[i]是true说明单词已经修改
             ciphertext+=word.charAt(i);
           }
           else{
               ciphertext+="*";
           }
       }
         return ciphertext;
  }
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值