从键盘中随意输入一串字符,统计并输出该字符串中各字符(数字、大写字母、小写字母、标点符号等)各自出现的次数。用面向对像的思想实现。

从键盘中随意输入一串字符,统计并输出该字符串中各字符(数字、大写字母、小写字母、标点符号等)各自出现的次数。

用面向对像的思想实现。

import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        Scanner scnn = new Scanner(System.in);
        System.out.println("请输入一段字符串");
        String str = scnn.next();
        char[] array = str.toCharArray();
        sumWord(array);
    }

    public static void sumWord(char[] array) {
        int countNumber = 0;
        int countBigWord = 0;
        int countSmallWord = 0;
        int countOthers = 0;
        for (int i = 0; i < array.length; i++) {
            if (array[i] >= '0' && array[i] <= '9')
                countNumber++;
            else if (array[i] >= 'a' && array[i] <= 'z') {
                countSmallWord++;
            } else if (array[i] >= 'A' && array[i] <= 'Z') {
                countBigWord++;
            } else {
                countOthers++;
            }
        }
        System.out.println("数字有:" + countNumber);
        System.out.println("大写字母有:" + countBigWord);
        System.out.println("小写字母有:" + countSmallWord);
        System.out.println("其他字符有:" + countOthers);
    }
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,让我们来编写一个类 ChatGPT 的 AI 程序,用于实现输入一个字符串统计的非字母数、大写字母数、小写字母数和数字数。这里我使用了 OpenAI 的 GPT 模型,可以完成自然语言的生成和理解。代码如下: ```python import openai import re import string openai.api_key = "YOUR_API_KEY" # 需要替换成你自己的 OpenAI API KEY def count_chars(input_str): # 去除标点符号和空格 input_str = input_str.translate(str.maketrans('', '', string.punctuation + ' ')) # 统计非字母数、大写字母数、小写字母数和数字数 non_alpha_count = len(re.findall(r'[^a-zA-Z]', input_str)) upper_alpha_count = len(re.findall(r'[A-Z]', input_str)) lower_alpha_count = len(re.findall(r'[a-z]', input_str)) digit_count = len(re.findall(r'\d', input_str)) # 返回统计结果 return non_alpha_count, upper_alpha_count, lower_alpha_count, digit_count def generate_response(input_str): # 调用 GPT 模型生成回复 prompt = f"统计字符串的非字母数、大写字母数、小写字母数和数字数:\n\n输入字符串:{input_str}\n\n" response = openai.Completion.create( engine="davinci", prompt=prompt, temperature=0.5, max_tokens=60, n=1, stop=None, timeout=10, ) # 解析 GPT 模型的回复结果 result = response.choices[0].text.strip() return result # 测试程序 input_str = "Hello World! 123" non_alpha_count, upper_alpha_count, lower_alpha_count, digit_count = count_chars(input_str) output_str = f"非字母数:{non_alpha_count},大写字母数:{upper_alpha_count},小写字母数:{lower_alpha_count},数字数:{digit_count}" response = generate_response(input_str) # 输出统计结果和 AI 回复 print(output_str) print(response) ``` 这个程序首先使用了正则表达式去除了字符串标点符号和空格,然后统计了其的非字母数、大写字母数、小写字母数和数字数。接着,程序调用了 OpenAI 的 GPT 模型生成了一个 AI 回复,回复内容包含了输入字符串统计结果。你可以将这段代码复制到 Python 环境运行,然后输入一个字符串,即可获得统计结果和 AI 回复。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

超翔之逸

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值