我最近编写了一些应该将字符串放入框中的代码 如果输入是" hello",输出应如下所示:
hello
e l
l l
l e
olleh
我有几乎所有的代码,我把它变成了一个盒子,除了我似乎无法使这些字母排成一行。我知道我搞砸的部分,我只是不知道如何解决它。混乱的部分是引用并被评论。这是代码,谢谢!:
int num = 0;
for (int i=1;i<=word.length();i++){
for (int a=1;a<=word.length();a++){
if(i>1 && i<word.length() && a>1 && a<word.length())
System.out.print(" ");
else
//"System.out.print(word.charAt(num));
}
System.out.println("");
}
答案 0 :(得分:1)
您可以先将String
反转,然后将其存储在char[]
数组中。 (因为我忘了使用StringBuilder#reverse()
)。
然后你可以像你一样迭代2 for
个循环,顶部,底部和中间行的一些条件如下:
诀窍是使用原始字符串作为顶部和左部,而反向部分则使用右侧和底部。
另请注意,您从索引1~ word.lenght()
开始,这不是您想要的,因为从索引0
开始打印每个字符时不是1
public class WordSquare {
public static void main(String[] args) {
String word = "hello";
int num = 0;
char[] reverseWord = new char[word.length()];
int counter = 0;
for (int i = word.length() - 1; i >= 0; i--) {
reverseWord[counter] = word.charAt(i);
counter++;
}
for (int i = 0; i < word.length(); i++) {
counter = 0;
for (int j = 0; j < word.length(); j++) {
if (i == 0) {
System.out.print(word.charAt(j));
} else {
if (j == 0) {
System.out.print(word.charAt(i));
} else if (j < (word.length() - 1)) {
if (i < (word.length() - 1)) {
System.out.print(" ");
} else {
System.out.print(reverseWord[j]);
}
} else {
System.out.print(reverseWord[i]);
}
}
}
System.out.println("");
}
}
}
产生:
hello
e l
l l
l e
olleh
答案 1 :(得分:1)
我只是假设你必须在两个for循环中进行,所以我只是添加到循环中。
for (int i = 0; i <= word.length() - 1; i++){
for (int a = 0; a <= word.length() - 1; a++){
if(i > 0 && i < word.length() - 1 && a > 0 && a < word.length() - 1) {
System.out.print(" ");
} else if(i == word.length() - 1) {
System.out.print(word.charAt(word.length()-1-a));
} else if(a != word.length() - 1) {
System.out.print(word.charAt((a+i)%word.length()));
} else {
System.out.print(word.charAt((a-i)%word.length()));
}
}
System.out.println("");
}
这只是两个其他陈述中的相同内容
for (int i = 0; i <= word.length() - 1; i++){
for (int a = 0; a <= word.length() - 1; a++){
if(i > 0 && i < word.length() - 1 && a > 0 && a < word.length() - 1) {
System.out.print(" ");
} else if (a == 0 || i == 0) {
System.out.print(word.charAt((a+i)%word.length()));
} else {
System.out.print(word.charAt((2*(word.length()-1)-a-i)%word.length()));
}
}
System.out.println("");
}
答案 2 :(得分:0)
String word = "hello";
System.out.println(word);
for (int i = 1; i < word.length() - 1; i++) {
System.out.print(word.charAt(i));
for (int j = 0; j < word.length() - 2; j++) {
System.out.print(" ");
}
System.out.println(word.charAt(word.length()-i-1));
}
System.out.println(new StringBuilder(word).reverse().toString());
<强>输出:强>
hello
e l
l l
l e
olleh
答案 3 :(得分:-1)
试试这个
public void print(String word){
System.out.println(word);
for (int i=1;i<word.length()-1;i++){
System.out.print(word.charAt(i));
for (int j=0;j<word.length()-2;j++){
System.out.print(" ");
}
System.out.println(word.charAt(word.length()-i-1));
}
System.out.println(new StringBuilder(word).reverse().toString());
}
有趣的家庭作业问题btw
答案 4 :(得分:-1)
试试这个,更简单的逻辑
public class SchoolAssignment
{
// arguments are passed using the text field below this editor
public static void main(String[] args)
{
String input = "hello";
String middleSpaces = null;
//Determine amount of space in middle rows
StringBuilder middleSpace = new StringBuilder();
middleSpace.append("%");
middleSpace.append(String.valueOf(input.length()-2));
middleSpace.append("s");
middleSpaces = String.format(middleSpace.toString(), " ");
//print first line
System.out.println(input);
//print middle lines
for(int i = 1; i <= input.length() - 2; i++){
StringBuilder middleLine = new StringBuilder();
middleLine.append(input.charAt(i));
middleLine.append(middleSpaces);
middleLine.append(input.charAt(input.length() - 1 - i));
System.out.println(middleLine.toString());
}
//print last line
StringBuilder lastLine = new StringBuilder();
lastLine.append(input);
lastLine=lastLine.reverse();
System.out.println(lastLine);
}
}