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

相关推荐
可观测性用观测云1 小时前
云原生架构下微服务接入 SkyWalking 最佳实践
java
刘安然1 小时前
网易云课堂零基础:21天搞定Python分布爬虫
python
青牛科技-Allen2 小时前
7个常见的DFM问题及其对PCB制造的影响
开发语言·单片机·制造·usb麦克风·立体声录音笔
「QT(C++)开发工程师」2 小时前
C++语言编程规范-风格
linux·开发语言·c++·qt
Rock_yzh2 小时前
AI学习日记——PyTorch深度学习快速入门:神经网络构建与训练实战
人工智能·pytorch·python·深度学习·神经网络·学习
中等生2 小时前
uv 完全指南:Python 包管理的现代化解决方案
python
hello kitty w2 小时前
Python学习(10) ----- Python的继承
开发语言·python·学习
newxtc2 小时前
【广州公共资源交易-注册安全分析报告-无验证方式导致安全隐患】
开发语言·selenium·安全·yolo
懒羊羊不懒@2 小时前
Java一、二维数组
开发语言·python
中等生2 小时前
uvicorn 和 gunicorn
python