我真的不知道如何編程...我在做這個的計算機科學類java.lang.StringIndexOutOfBoundsException:字符串索引超出範圍:7
Instruction: Use nested loops to print out the square word pattern show below. I'm guessing the error is in the toString method, but I can't spot where.
所需的輸出是: (當輸入爲正方形)
SQUARE
Q R
U A
A U
R Q
ERAUQS
的代碼: 進口靜態java.lang.System中*;
class BoxWord
{
private String word;
public BoxWord()
{
word="";
}
public BoxWord(String s)
{
setWord(s);
}
public void setWord(String w)
{
word=w;
}
public String toString()
{
String output=word +"\n";
for(int i =0;i<word.length(); i++){
output += word.charAt(i);
for(int j = 2; j<word.length();j++)
output += " ";
output+= word.charAt(word.length()-(i-1))+ "\n";
}
for(int k=0; k<word.length(); k++)
output+= word.charAt(k);
return output+"\n";
}
}
主:
import static java.lang.System.*;
public class Lab11f
{
public static void main(String args[])
{
BoxWord test = new BoxWord("square");
out.println(test);
}
}
請加上堆棧跟蹤012 –
如果輸入是'平方',輸出應該是什麼? –
使用IDE並調試程序。這並不難。 – Jayan