简体   繁体   English

关于 String.charAt(); 的问题在 Java

[英]Questions regarding String.charAt(); in Java

I am recently working on an algorithm question from Leetcode.我最近正在研究 Leetcode 的一个算法问题。 But I am really confused with this line of code shown below.但是我真的对下面显示的这行代码感到困惑。 And I do not actually understand what is the meaning of而且我实际上不明白是什么意思

(char)(str.charAt(loop) + 1))

Can anyone explain this to me?谁能给我解释一下? Furthermore, sb means此外, sb表示

StringBuilder sb = new StringBuilder();

In case you guys do not know what is sb .如果你们不知道什么是sb

Here is the full code:这是完整的代码:

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            String str = scanner.nextLine();
            StringBuilder sb = new StringBuilder();
            for (int loop = 0; loop < str.length(); loop++) {
                if (str.charAt(loop) == 'Z') {
                    sb.append("a");
                    continue;
                }
                if (str.charAt(loop) >= '0' && str.charAt(loop) <= '9') {
                    sb.append(String.valueOf(str.charAt(loop)));
                    continue;
                }
                if (str.charAt(loop) >= 'A' && str.charAt(loop) < 'Z') {
                    sb.append(String.valueOf((char)(str.charAt(loop) + 1)).toLowerCase());
                    continue;
                }
                else {
                    switch (str.charAt(loop)) {
                        case('1'):
                            sb.append("1");
                            break;

                        case('a'):
                        case('b'):
                        case('c'):
                            sb.append("2");
                            break;

                        case('d'):
                        case('e'):
                        case('f'):
                            sb.append("3");
                            break;

                        case('g'):
                        case('h'):
                        case('i'):
                            sb.append("4");
                            break;

                        case('j'):
                        case('k'):
                        case('l'):
                            sb.append("5");
                            break;

                        case('m'):
                        case('n'):
                        case('o'):
                            sb.append("6");
                            break;

                        case('p'):
                        case('q'):
                        case('r'):
                        case('s'):
                            sb.append("7");
                            break;

                        case('t'):
                        case('u'):
                        case('v'):
                            sb.append("8");
                            break;

                        case('w'):
                        case('x'):
                        case('y'):
                        case('z'):
                            sb.append("9");
                            break;

                    }
                }
            }
            System.out.println(sb.toString());
        }
    }
}

You have to dissect the expression into its parts:您必须将表达式分解为各个部分:

String.valueOf(
    (char)
      (str.charAt(loop) + 1)
).toLowerCase());

So, on the "innermost" call (str.charAt(loop) + 1) a single char value is retrieved from str .因此,在“最内层”调用(str.charAt(loop) + 1)中,从str检索到单个 char 值。 Then 1 is added to that (remember that char is also a "numerical" type that allows for numerical operations).然后将 1 添加到其中(请记住, char 也是允许数字运算的“数字”类型)。 Then the result of that is turned into a String again, when is then forced to be a lower case string.然后它的结果再次变成一个字符串,然后强制成为一个小写字符串。

For the why to do that we would need more context.对于为什么要这样做,我们需要更多的上下文。

This line of code is simply encoding a string by caesar ciper of +1, in other word, if you put this line in a loop it will create a string of every char is the upper char in the alphabet, for example banana will be cboboc For more about caesar ciper look in https://en.m.wikipedia.org/wiki/Caesar_cipher这行代码只是用 +1 的 caesar ciper 对字符串进行编码,换句话说,如果你把这行放在一个循环中,它将创建一个字符串,每个字符都是字母表中的大字符,例如香蕉将是 cboboc有关凯撒 ciper 的更多信息,请查看https://en.m.wikipedia.org/wiki/Caesar_cipher

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM