大厂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站:

相关推荐
酷可达拉斯几秒前
Linux操作系统-shell编程(0)
linux·运维·服务器·python·云计算
W658034193 分钟前
2026年AI编程工具实测横评:Claude Code v2.1、Cursor 3.0、Trae SOLO、Copilot、Windsurf 谁更好用?
python·copilot·ai编程
深念Y14 分钟前
开发者如何清理和迁移C盘堆积的垃圾
c语言·开发语言
snow@li14 分钟前
技术栈对应:Vue (前端) + SpringBoot (Java 后端) =》 Python 全场景配套
python
野蛮人6号23 分钟前
黑马天机学堂系列问题之域名无法正常使用
java·spring cloud·天机学堂
学计算机的计算基25 分钟前
回溯算法下篇:四道经典题讲透约束剪枝、原地标记、预计算与状态压缩
java·笔记·算法
2501_9095091030 分钟前
DAY 28
开发语言·python
码云骑士34 分钟前
71-Agent记忆系统-短期记忆-长期记忆-向量知识库三层架构
python·架构
卷无止境36 分钟前
Python 的 exec 与 eval :动态代码执行的能力、风险与工程实践
后端·python
user-猴子36 分钟前
从零构建 2048 游戏,解析“Python-Use”范式的完整闭环
开发语言·python·游戏