【问题标题】:Outputting a string alternating uppercase and lowercase without using the Character class在不使用 Character 类的情况下输出大写和小写交替的字符串
【发布时间】:2017-03-01 00:36:40
【问题描述】:
            for (int i = 0, len = input.length(); i < len; i++) {
                char ch = input.charAt(i);
                if (i % 2 == 0) {
                    System.out.print(Character.toLowerCase(ch));
                } else {
                    System.out.print(Character.toUpperCase(ch));

                }
            }

我想在不使用字符类的情况下执行此操作。仅使用 tolowerCase 和 toUpperCase 以及基本循环。

因此,基本程序将在不使用字符类的情况下将诸如“Hello World”之类的字符串转换为“HeLlo WoRlD”。

我听说你可以做到,但我想不通。真的很烦我

【问题讨论】:

  • 看一下 ASCII 表
  • 你也可以使用子字符串
  • 子字符串!谢谢!!!

标签: java capitalization


【解决方案1】:

我想通了。就是这样:

            for (int i = 0, len = input.length(); i < len; i++) {

                char ch = input.charAt(i);
                String str = ch + ""; //<- converts char to a string!

                if (i % 2 == 0) {
                    System.out.print(str.toLowerCase());
                } else {
                    System.out.print(str.toUpperCase());

不知道为什么这会受到如此多的反对,我花了大约一个小时在谷歌上寻找类似的东西。我想我很讨厌谷歌。

【讨论】:

    猜你喜欢
    • 2021-08-14
    • 1970-01-01
    • 2016-07-14
    • 1970-01-01
    • 1970-01-01
    • 2011-09-13
    • 2011-02-20
    • 2018-03-24
    • 2021-05-31
    相关资源
    最近更新 更多