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

相关推荐
g***9690几秒前
springboot设置多环境配置文件
java·spring boot·后端
Jtti5 分钟前
PHP项目缓存占用硬盘过大?目录清理与优化
java·缓存·php
catchadmin7 分钟前
使用 PHP 和 Raylib 也可以开发贪吃蛇游戏
开发语言·游戏·php
二川bro21 分钟前
2025年Python机器学习全栈指南:从基础到AI项目部署
人工智能·python·机器学习
未若君雅裁33 分钟前
JVM基础总结
java·jvm·java-ee
p***434837 分钟前
JavaScript数据分析实战
开发语言·javascript·ecmascript
星释38 分钟前
Rust 练习册 66:密码方块与文本加密
java·前端·rust
q***318944 分钟前
Spring Boot 实战篇(四):实现用户登录与注册功能
java·spring boot·后端
Learn Beyond Limits1 小时前
Correlation vs Cosine vs Euclidean Distance|相关性vs余弦相似度vs欧氏距离
人工智能·python·神经网络·机器学习·ai·数据挖掘
专注于大数据技术栈1 小时前
java学习--==和equals
java·python·学习