大厂Java笔试题之判断字母大小写

java 复制代码
/**
 * 题目:如果一个由字母组成的字符串,首字母是大写,那么就统计该字符串中大写字母的数量,并输出该字符串中所有的大写字母。否则,就输出
 * 该字符串不是首字母大写
 */
public class Demo2 {
    public static void main(String[] args) {
//        outputWordAscii();
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextLine()) {
            String s = scanner.nextLine();
            char[] chars = s.toCharArray();
            if (isUpperCase(chars[0])) {
                int upperCaseCount = getUpperCaseCount(chars);
                System.out.print(upperCaseCount + " ");
                System.out.println();
            } else {
                System.out.println("不是首字母大写");
            }
        }
        scanner.close();
    }

    public static boolean isUpperCase(char aChar) {
        if (aChar >= 65 && aChar <= 90) {
            return true;
        } else {
            return false;
        }
    }

    public static int getUpperCaseCount(char[] chars) {
        int res = 0;
        for (int i = 0; i < chars.length; i++) {
            if (isUpperCase(chars[i])) {
                System.out.print(chars[i] + " ");
                res++;
            }
        }
        return res;
    }

    public static void outputWordAscii() {
        for (int i = 65; i <= 122; i++) {
            if (i > 90 && i< 97) {
                continue;
            }
            System.out.print((char) i + " ");
        }
    }
}

如果大家需要视频版本的讲解,欢迎关注我的B站:

相关推荐
coder!mq25 分钟前
说几个常见的语法糖?
java·开发语言·算法
gugucoding27 分钟前
38. 【Java】Stream API(下):高级操作与性能
java·开发语言
Lhan.zzZ35 分钟前
在 Visual Studio 2022 中打造可扩展的动态链接库模块:从零搭建到原理解析
开发语言·c++·visual studio
心平气和量大福大1 小时前
android-实例-蒲公英-更新与安装-5-签名与生成APK
android·java·开发语言
码哥DFS1 小时前
构造函数、实例对象、对象原型 ----三者关系
开发语言·javascript·原型模式
Python大数据分析@1 小时前
使用大模型MCP采集数据,爬虫已经无门槛
python·网络爬虫
952362 小时前
Spring-cloud配置中心
java·spring·spring cloud·微服务
quantdash_cc2 小时前
告别自建 Requests/BS4 网页爬虫:基于 QuantDash 搭建零维保的高性能量化行情流水线
开发语言·爬虫·python·pandas·量化·quantdash
ydd1001002 小时前
寻找两个正序数组的中位数 Java 题解,二分分割详解
java·开发语言
65岁退休Coder2 小时前
LangChain v1.3.4 笔记 - 07 补充:链式调用 LCEL
后端·python·langchain