黑皮书猜单词游戏

import java.util.Scanner;
class Demo508{
    public static void main(String[] args){
        Scanner scanner=new Scanner(System.in);
        String[] words={"write","that","hello","program","this"};
        while(true){
            int num=(int)(Math.random()*5);
            int count=0,count1=0;
            String word=words[num];
            String repeat="";
            char[] arr=new char[words[num].length()];
            block(word,words[num]);
            int t=0;
            while(true){
                String str=scanner.nextLine();
                if(str.equals("")){
                    block(word,words[num]);
                    continue;
                }
                char letter=str.charAt(0);
                int temp=judge(word,letter);
                if(temp>-1){
                    word=delete(word,temp);
                    repeat=repeat+words[num].charAt(temp);
                    count1++;
                }else if(isRepeat(letter,repeat)){
                    System.out.println(letter+" is already in the word");
                    count++;
                }else{
                    System.out.println(letter+" is not in the word");
                    count++;
                }
                
                if(word.length()==count1){
                        break;
                }else{
                    block(word,words[num]);
                }
            }
            if(count>1){
                System.out.println("The word is "+words[num]+" . You missed "+count+"times");
            }else {
                System.out.println("The word is "+words[num]+" . You missed "+count+"time");
            }
            boolean flag=true;
            
            while(true){
                System.out.print("Do you want to guess another word? Enter y or n>");
                String onceMore1=scanner.nextLine();
                if(onceMore1.equals("")){
                    continue;
                }
                char onceMore=onceMore1.charAt(0);
                if(onceMore=='n'){
                    flag=false;
                    break;
                }else if(onceMore=='y'){
                    break;
                }
            }
            if(!flag){
                break;
            }
        }
    }
    public static boolean isRepeat(char letter,String repeat){
        for(int i=0;i<repeat.length();i++){
            if(letter==repeat.charAt(i)){
                return true;
            }
        }
        return false;
    }
    public static int judge(String word,int letter){
        for(int i=0;i<word.length();i++){
            if(word.charAt(i)==letter){
                return i;
            }
        }
        return -1;
    }
    public static String delete(String word,int temp){
        String str="";
        for(int i=0;i<word.length();i++){
            if(i!=temp){
                str+=word.charAt(i);
            }else{
                str+=" ";
            }
        }
        return str;
    }
    public static void block(String word1,String word2){
        System.out.print("(Guess) Enter a letter in word ");
        System.out.print(word1+" "+word2+" ");
        for(int i=0,j=0;j<word2.length();i++,j++){
            
            if(word1.charAt(i)!=word2.charAt(j)){
                System.out.print(word2.charAt(j));
                
            }else{
                System.out.print('*');
            }
        }
        System.out.print('>');
    }
    public static void block(char[] arr){
        System.out.print("(Guess) Enter a letter in word ");
        for(int i=0;i<arr.length;i++){
            arr[i]='*';
            System.out.print('*');
        }
        System.out.print('>');
    }

    
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值