Java猜单词

1.常用字符串方法


import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        String msg="hello world hello kitty";
        System.out.println(msg.length());//得字符串长度
        String[] arr=msg.split(" ");//分词
        System.out.println(Arrays.toString(arr));
        //23
        //[hello, world, hello, kitty]

        String new_msg=msg.replace("hello","你好");//替换
        System.out.println(new_msg);
        //你好 world 你好 kitty

        String sub_str=msg.substring(1,9);
        System.out.println(sub_str);//提取
        //ello wor

        int idx=msg.indexOf("hello");//查询是否包含,并返回下标
        int idx1=msg.lastIndexOf("hello");//从后往前查
        System.out.println(idx);
        System.out.println(idx1);
//        0
//        12
        char[] char_arr=msg.toCharArray();//转换为字符数组
        for(char c:char_arr){
            System.out.println(c);
        }
        System.out.println(char_arr[2]);
        //l

        System.out.println(msg.charAt(2));//返回该下标的内容,和上述代码一样效果
        //l

        String info=new String("xxxxx");
        String info1=new String("xxxxx");
        if (info1==info){//判断两个地址是否相等
            System.out.println("====");
        }
        else {
            System.out.println("!===");
        }
        if (info1.equals(info)){//判断两个内容是否相等
            System.out.println("====");
        }
        else {
            System.out.println("!===");
        }
        //!===
        //====


    }
}

2.猜单词游戏

import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;

public class GuessWord {
    public static void main(String[] args) {
        String sentence="static class extend interface replace apple banana";
        String[] words=sentence.split(" ");
        Random random=new Random();
        int random_idx= random.nextInt(words.length);
        String select_word=words[random_idx];
        //System.out.println(select_word);//随机出单词

        char[] word_arr=new char[select_word.length()];
        Arrays.fill(word_arr,'-');
        //System.out.println(word_arr);//填充“-”

        int guess_time=5;
        while (guess_time>0){
            System.out.println(word_arr);
            System.out.println("你还有"+guess_time+"次机会");
            System.out.println("请输入猜测字母:");
            Scanner scanner=new Scanner(System.in);
            char guess_char=scanner.nextLine().charAt(0);

            if (select_word.contains(String.copyValueOf(new char[]{guess_char}))){
                for (int i=0;i<select_word.length();i++){
                    if (select_word.charAt(i)==guess_char){
                        word_arr[i]=guess_char;
                    }
                }
                int i=0;
                for (;i<word_arr.length;i++){
                    if (word_arr[i]=='-'){
                        break;
                    }
                }
                if (i==word_arr.length){
                    break;
                }
            }
            else {
                guess_time--;
            }

        }
        if (guess_time>0){
            System.out.println("you win");
        }else {
            System.out.println("you lose");
        }
    }
}

  • 6
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您解答这个问题。这是一个简单的Python程序,可以让用户根据给定的字母猜单词。以下是程序的实现过程: 1. 首先,我们需要准备一个单词列表,程序将从中随机选择一个单词供用户猜测。例如,我们可以使用以下代码定义一个单词列表: ```python word_list = ["python", "java", "ruby", "javascript", "php"] ``` 2. 接下来,我们需要随机选择一个单词,并将其转换为一个由下划线组成的字符串,以便用户可以看到单词的长度。例如,我们可以使用以下代码选择一个单词并将其转换为下划线字符串: ```python import random word = random.choice(word_list) hidden_word = "_" * len(word) ``` 3. 然后,我们需要使用一个循环来让用户猜测单词中的字母。在每次循环中,我们将显示隐藏的单词,并要求用户输入一个字母。如果用户猜对了字母,我们将更新隐藏的单词,否则我们将减少用户的猜测次数。例如,我们可以使用以下代码实现这个循环: ```python guesses = 6 while guesses > 0: print(hidden_word) guess = input("Guess a letter: ") if guess in word: for i in range(len(word)): if word[i] == guess: hidden_word = hidden_word[:i] + guess + hidden_word[i+1:] if "_" not in hidden_word: print("You win!") break else: guesses -= 1 print("Wrong! You have", guesses, "guesses left.") else: print("You lose! The word was", word) ``` 在这个循环中,我们使用一个变量`guesses`来追踪用户剩余的猜测次数。如果用户猜对了一个字母,我们将更新隐藏的单词,将正确的字母插入到正确的位置。如果用户猜错了,我们将减少用户的猜测次数并显示剩余的猜测次数。如果用户猜对了所有的字母,我们将显示“你赢了!”消息并结束循环。如果用户用完了所有的猜测次数,我们将显示“你输了!单词是...”消息并结束循环。 这就是一个简单的Python程序,可以让用户根据给定的字母猜单词。希望这能帮助您解决问题!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值