Java 如何重复一个字符串,其中每个字符的重复次数递减?
啊,是的,我带着另一个Java问题回来了。所以这里我要重复一个字符串,它的字符重复的次数越来越少。第一个字符应重复字符串的长度多次Java 如何重复一个字符串,其中每个字符的重复次数递减?,java,Java,啊,是的,我带着另一个Java问题回来了。所以这里我要重复一个字符串,它的字符重复的次数越来越少。第一个字符应重复字符串的长度多次 public static void main(String[] args) { String str = "Howdy"; int characterLoc = 0; int repCount = str.length(); while (characterLoc < str.length()) {
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();
}
}
以下是输出的示例:
HHHHH
oooo
www
dd
y
根据我在下面编写的代码,我接下来应该做什么
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;
}
字符串go(字符串a)
{
字符串y=“”;
对于(int i=0;i
请随意指出我犯的任何明显错误。我是Java新手,刚刚了解到Java和Javascript不是一回事 我们可以维护两个计数器-1用于从字符串中提取字符(characterLoc),另一个用于指定字符的重复次数(repCount) 外部while循环用于提取字符,内部循环用于将提取的字符重复指定次数
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();
}
}
publicstaticvoidmain(字符串[]args){
String str=“你好”;
int characterLoc=0;
int repCount=str.length();
while(characterLoc0;x--){
系统输出打印(str.charAt(characterLoc));
}
characterLoc++;
重复计数--;
System.out.println();
}
}
我们可以维护两个计数器-1用于从字符串中提取字符(characterLoc),另一个用于指定字符的重复次数(repCount)
外部while循环用于提取字符,内部循环用于将提取的字符重复指定次数
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();
}
}
publicstaticvoidmain(字符串[]args){
String str=“你好”;
int characterLoc=0;
int repCount=str.length();
while(characterLoc0;x--){
系统输出打印(str.charAt(characterLoc));
}
characterLoc++;
重复计数--;
System.out.println();
}
}
当我运行您在问题中发布的代码时,我得到了以下结果:
HHHHH
ooooo
wwwww
ddddd
yyyyy
这不是你想要的。为了得到您想要的,您只需要在代码中做一个更改。您需要更改循环的内部
。这是您的代码和所需的添加
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;
}
哪个是你想要的,不是吗?当我运行你在问题中发布的代码时,我得到了以下结果:
HHHHH
ooooo
wwwww
ddddd
yyyyy
这不是你想要的。
为了得到您想要的,您只需要在代码中做一个更改。您需要更改
循环的内部。这是您的代码和所需的添加
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;
}
哪个是你想要的,不是吗?这可能是?通过添加解释来说服读者怎么样?如果你解释它是如何工作的以及它为什么要解决OPS问题,那么读者可能会认为它是一个解决方案。即使你出于某种原因没有…@yunnosch我真的看到了更新的发生+感谢社区帮助彼此变得更好。@RichardBarker谢谢。这当然是我的目标。我试图用不同的注释让人们只解释他们的代码。坦率地说,我没想到这次会成功。。。。编辑后的第一次投票甚至不是我的。这让我特别高兴,因为它证实了我的观点,解释可以弥补。这可能是什么?通过添加解释来说服读者怎么样?如果你解释它是如何工作的以及它为什么要解决OPS问题,那么读者可能会认为它是一个解决方案。即使你出于某种原因没有…@yunnosch我真的看到了更新的发生+感谢社区帮助彼此变得更好。@RichardBarker谢谢。这当然是我的目标。我试图用不同的注释让人们只解释他们的代码。坦率地说,我没想到这次会成功。。。。编辑后的第一次投票甚至不是我的。这让我非常高兴,因为它证实了我的观点,即解释构成了upvotable。请注意,在Java中创建字符串是一个相对昂贵的操作,因此如果在循环中运行,最好在循环开始之前创建一个new StringBuilder()
,在循环中使用append
,然后在循环完成后使用toString()
,而不是使用+
。使用字符串生成器也会极大地简化代码。请注意,在Java中创建字符串是一个相对昂贵的操作,因此如果在循环中运行,最好创建一个新的字符串生成器()
在循环开始之前,在循环内部使用append
,然后在循环完成后使用toString()
,而不是使用+
。使用字符串生成器也会大大简化代码。