【发布时间】:2014-01-15 03:57:00
【问题描述】:
我真的不知道如何编程......我正在为计算机科学课做这个
说明:使用嵌套循环打印出如下所示的方字图案。 我猜错误出在 toString 方法中,但我找不到位置。
所需的输出是:(当输入为 SQUARE 时)
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);
}
}
【问题讨论】:
-
请添加堆栈跟踪
-
如果输入是
square,那么输出应该是什么? -
使用 IDE 并调试程序。这并不难。
-
看看当 i = 0 时会发生什么。
标签: java for-loop nested-loops