尝试以墨西哥海浪式的模式将字符转换为大写。

huangapple go评论78阅读模式
英文:

Attempting to change characters to upper case in a mexican wave style pattern

问题

以下是翻译好的内容:

我试图逐个字符将字符串转换为大写字母,以墨西哥风格波浪图案的方式。应该是这个样子的:

输入:hello

输出:Hello
        hEllo
        heLlo
        helLo
        hellO

然而,我得到的只是:

输入:hello

输出:hello
        hello
        hello
        hello
        hello

这是我目前循环的样子:

for(int i=0;i<s.length();i++)
   {
     a1=s.charAt(i);
     a2=Character.toUpperCase(a1);
     System.out.println(s);
    }

其中s是带有a1和a2的字符串,用于存储当前值。

我对“Character.toUpperCase”不熟悉,但我在网上找到了它,希望它能帮助解决我的问题,但似乎没有起作用。

英文:

I'm trying to take a string and convert one character at a time to a capital letter, in a mexican wave style pattern. This is what it should look like

Input: hello

Output: Hello
        hEllo
        heLlo
        helLo
        hellO

However, what I am getting is just

Input: hello

Output: hello
        hello
        hello
        hello
        hello

This is what my loop currently looks like

for(int i=0;i<s.length();i++)
   {
     a1=s.charAt(i);
     a2=Character.toUpperCase(a1);
     System.out.println(s);
    }

s being the String with a1 and a2 being two random chars to store the current value in.

I'm not familiar with the "Character.toUpperCase" but I found it online hoping it would help to solve my problems however it doesnt seem to be

答案1

得分: 2

你正在打印相同的字符串。要获得所需的输出,你可以像下面这样做:

String s = "hello";
for (int i = 0; i < s.length(); i++) {
    char a1 = s.charAt(i);
    char a2 = Character.toUpperCase(a1);
    if (i == 0)
        System.out.println(a2 + s.substring(i + 1));
    else
        System.out.println(s.substring(0, i) + a2 + s.substring(i + 1));
}

如果你想跳过if-else条件,可以使用以下代码:

String s = "hello";
System.out.println(s.charAt(0) + s.substring(1)); // 对于第一个字符
for (int i = 1; i < s.length(); i++) // 对于其余的字符
{
    char a1 = s.charAt(i);
    char a2 = Character.toUpperCase(a1);
    System.out.println(s.substring(0, i) + a2 + s.substring(i + 1));
}
英文:

You are printing the same String. To get the desired output, you can do something like following,

String s = &quot;hello&quot;;
for(int i=0;i&lt;s.length();i++)
{
    char a1 = s.charAt(i);
    char a2 = Character.toUpperCase(a1);
    if (i == 0)
        System.out.println(a2 +s.substring(i+1));
    else
        System.out.println( s.substring(0,i) +a2 +s.substring(i+1));
}

If yo want to skip if-else condition,

String s = &quot;hello&quot;;
System.out.println(s.charAt(0) +s.substring(1)); // for first char
for(int i=1;i&lt;s.length();i++) // for rest of the chars
{
    char a1 = s.charAt(i);
    char a2 = Character.toUpperCase(a1);
    System.out.println( s.substring(0,i) +a2 +s.substring(i+1));
}

答案2

得分: 0

String 是一个不可变的类。你无法直接在原地修改一个 String。因此,将 String 中的字符设置为大写并不会改变它。

你需要做的是要么使用可变的东西,比如 StringBuilder,要么每次想要进行更改时创建一个新的 String

下面的代码创建了一个新的 String。首先,我获取源 String 中单个字符的数组 - 根据你的问题,它是 hello。然后,我遍历数组中的字符,依次将每个字符设置为大写,同时将之前更改的字符恢复为小写。在每次迭代中,我都为当前的字符数组创建一个新的 String

char[] letters = "hello".toCharArray();
for (int i = 0; i < letters.length; i++) {
    if (i - 1 >= 0) {
        letters[i - 1] = Character.toLowerCase(letters[i - 1]);
    }
    letters[i] = Character.toUpperCase(letters[i]);
    System.out.println(new String(letters));
}

运行上述代码的结果是:

Hello
hEllo
heLlo
helLo
hellO
英文:

String is an immutable class. You can't change a String in place. Hence setting a character in the String to uppercase does not change it.

What you need to do is either use something that is mutable, like StringBuilder or create a new String each time you want to make a change.

The below code creates a new String. First I get an array of the individual characters in the source String - which is hello according to your question. Then I iterate through the characters in the array setting each one, in turn, to uppercase while at the same time reverting the previously changed character back to lowercase. And in each iteration I create a new String for the current array of letters.

char[] letters = &quot;hello&quot;.toCharArray();
for (int i = 0; i &lt; letters.length; i++) {
    if (i - 1 &gt;= 0) {
        letters[i - 1] = Character.toLowerCase(letters[i - 1]);
    }
    letters[i] = Character.toUpperCase(letters[i]);
    System.out.println(new String(letters));
}

The result of running the above code is:

Hello
hEllo
heLlo
helLo
hellO

huangapple
  • 本文由 发表于 2020年4月9日 20:44:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/61121451.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定