英文:
StringIndexOutOfBoundsException:
问题
我知道这个错误已经被问过很多次了。我已经查看过它们,但仍然不太理解如何应用到我的问题上。我正在尝试编写一个程序,根据一个字符串创建一个菱形图案。
public static String nameDiamond(String s) {
int len = s.length();
int i = 0;
String out = "";
while(i < len) {
out = out + s.substring(0, i + 1) + "\n";
i++;
}
while(i > 0) {
int numSpaces = len - i + 1;
for(int j = 0; j < numSpaces; j++)
out = out + " ";
out = out + s.substring(len - i + 1, i - 1) + "\n";
i--;
}
return out;
}
现在我对substring
的理解是,它的结构类似于s.substring(startIndex, endIndex)
,而引起这个错误的原因之一可能是当结束索引小于起始索引时,但我不认为这是这里的问题。我还看到一些错误的原因是因为某些东西不存在,但我也不认为这是问题。我漏掉了什么吗?
当使用“Marty”进行测试时,它显示为-1
,而使用“Bill Nye”进行测试时,它显示为-2
。是空格导致了这个问题吗?
我查看了很多其他版本的问题,但似乎相关的是:
但我不认为它们适用。
供参考,这是问题的链接:
https://www.codestepbystep.com/problem/view/java/strings/nameDiamond
英文:
I know this error has been asked a lot. I have looked at them but still don't really understand how it applies to my problem. I am trying to write a program that creates a diamond pattern from a string.
public static String nameDiamond(String s) {
int len = s.length();
int i = 0;
String out = "";
while(i < len) {
out = out + s.substring(0, i + 1) + "\n";
i++;
}
while(i > 0) {
int numSpaces = len - i + 1;
for(int j = 0; j < numSpaces; j++)
out = out + " ";
out = out + s.substring(len - i + 1, i - 1) + "\n";
i--;
}
return out;
}
Now my understanding of substring
is, that it is structured like s.substring(startIndex, endIndex)
and a cause of this error can be when the end index is less then the start but I don't think this is the problem here. I also saw some causes of the error is when something does not exist but again I don't think this is the problem. What am I missing?
When it is tested with "Marty" it says -1
and with "Bill Nye" it says -2
. Would the space be causing this?
I looked at lots of other versions of the questions but the ones that seemed relevent were:
But I don't think they apply.
For reference, this is the problem:
https://www.codestepbystep.com/problem/view/java/strings/nameDiamond
答案1
得分: 0
这里有一个你可以用作示例的。它虽然不完全符合你的要求,但会演示一个重要的内容。对于固定宽度的字体,每一行的字符数必须是奇数。否则它们将无法正确对齐。
如果 n
是偶数,这个特定版本将会为 n
和 n+1
打印相同的菱形。它还受到空格字符串的限制。
public static void printDiamond(int len) {
String str = "*";
String space = " ";
for (int i = 0; i < len/2 + 1; i++) {
System.out.print(space.substring(0,len/2-i));
System.out.println(str);
str = str + "**";
}
str = str.substring(2);
for (int i = 0; i < len/2; i++) {
str = str.substring(2);
System.out.print(space.substring(0,i+1));
System.out.println(str);
}
}
在调试程序时,使用普遍的打印语句来输出不同的变量,比如索引和字符串长度。
英文:
Here is one you can use as an example. It doesn't do exactly what you want but it will illustrate one important thing. For fixed width fonts, each row must be an odd number of characters. Otherwise they won't line up properly.
If n
is an even number, this particular version will print the same diamond for n
and n+1
. It is also limited by the string of spaces.
public static void printDiamond(int len) {
String str = "*";
String space = " ";
for (int i = 0; i < len/2 + 1; i++) {
System.out.print(space.substring(0,len/2-i));
System.out.println(str);
str = str+"**";
}
str = str.substring(2);
for(int i = 0; i < len/2; i++) {
str = str.substring(2);
System.out.print(space.substring(0,i+1));
System.out.println(str);
}
}
As you debug your program, use ubiquitous print statements to print the different variables such as indices and string lengths.
专注分享java语言的经验与见解,让所有开发者获益!
评论