如何使一个字符串重复,其中每个字符重复的次数逐渐减少?

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

How do I repeat a string where each character is repeated a decreasing number of times?

问题

当然,我会为你进行翻译。以下是翻译好的部分:

是的我又带着另一个 Java 问题回来了这里我需要重复一个字符串其中的字符重复次数递减第一个字符应该重复字符串长度次

以下是输出应该呈现的示例

HHHHH
oooo
www
dd
y


基于我下面写的代码,接下来该怎么做呢?

```java
String go(String a) 
{
  String y = "";
  for (int i = 0; i < a.length(); i++)
  {
    for (int j = 0; j < a.length() - i; j++)
    {
      y = y + a.charAt(i);
    }
    if (i != a.length() - 1)
    {
      y = y + "\n";
    }
  }
  return y;
}

请随意指出我可能犯的明显错误。我是 Java 新手,刚刚了解到 Java 和 JavaScript 并不相同!


<details>
<summary>英文:</summary>

Ah yes, I am back with another Java question. So here I am supposed to repeat a string where its characters repeat a decreasing number of times. The first character should be repeated the string&#39;s length number of times.

Here is an example of what the output should look like:

HHHHH
oooo
www
dd
y

What should I do next based on the code I have written below?

String go( String a)
{
String y = "";
for (int i = 0; i < a.length(); i++)
{
for (int j = 0; j < a.length(); j++)
{
y = y + a.charAt(i);
}
if (i == a.length() - 1)
{
y = y + "";
}
else
{
y = y + "\n";
}
}
return y;
}

Feel free to point out any obvious mistakes I have made. I am new to Java and just learned that Java and Javascript are not the same thing!

</details>


# 答案1
**得分**: 6

我们可以维护两个计数器 - 一个用于从字符串中提取字符(characterLoc),另一个用于指定字符重复的次数(repCount)。

外部 while 循环用于提取字符,内部循环用于重复提取的字符指定的次数。

```java
public static void main(String[] args) {
    String str = "Howdy";
    int characterLoc = 0;
    int repCount = str.length();

    while (characterLoc < str.length()) {
        for (int x = repCount; x > 0; x--) {
            System.out.print(str.charAt(characterLoc));
        }
        characterLoc++;
        repCount--;
        System.out.println();
    }
}
英文:

We can maintain two counters - 1 for extracting the character from string (characterLoc) and the other for specifying the number of times a character is to be repeated (repCount).

The outer while loop is used for extracting the character and inner loop is used for repeating the extracted character a specified number of times.

public static void main(String[] args) {
	String str = &quot;Howdy&quot;;
	int characterLoc = 0;
	int repCount = str.length();

	while (characterLoc &lt; str.length()) {
		for (int x = repCount; x &gt; 0; x--) {
			System.out.print(str.charAt(characterLoc));
		}
		characterLoc++;
		repCount--;
		System.out.println();
	}
}

答案2

得分: 2

当我运行您在问题中发布的代码时,我得到了以下结果:

HHHHH
ooooo
wwwww
ddddd
yyyyy

这并不是您想要的结果。为了得到您想要的效果,您只需在代码中做一个修改。您需要更改内部的 for 循环。以下是带有所需更改的代码:

private static String go(String a) {
    String y = "";
    for (int i = 0; i < a.length(); i++) {
        for (int j = 0; j < a.length() - i; j++) { // 在这里进行更改
            y = y + a.charAt(i);
        }
        if (i == a.length() - 1) {
            y = y + "";
        }
        else {
            y = y + "\n";
        }
    }
    return y;        
}

当您运行此代码时,它会产生以下输出:

HHHHH
oooo
www
dd
y

这就是您想要的结果,对吗?

英文:

When I ran the code you posted in your question, I got this result:

HHHHH
ooooo
wwwww
ddddd
yyyyy

which is not what you want.
In order to get what you want, you simply need to make one change in your code. You need to change the inner for loop. Here is your code with the required addition.

private static String go(String a) {
    String y = &quot;&quot;;
    for (int i = 0; i &lt; a.length(); i++) {
        for (int j = 0; j &lt; a.length() - i; j++) { // change here
            y = y + a.charAt(i);
        }
        if (i == a.length() - 1) {
            y = y + &quot;&quot;;
        }
        else {
            y = y + &quot;\n&quot;;
        }
    }
    return y;        
}

When you run that code, it produces the following output.

HHHHH
oooo
www
dd
y

which is what you want, isn't it?

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

发表评论

匿名网友

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

确定