英文:
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'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 = "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();
}
}
答案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 = "";
for (int i = 0; i < a.length(); i++) {
for (int j = 0; j < a.length() - i; j++) { // change here
y = y + a.charAt(i);
}
if (i == a.length() - 1) {
y = y + "";
}
else {
y = y + "\n";
}
}
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?
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论