如何制作一个由单词构成的空心正方形?

huangapple go评论77阅读模式
英文:

How to make a hollow square with words?

问题

以下是翻译好的代码部分:

// JAVA空心矩形的代码
import java.io.*; 
import java.awt.*;
import java.util.*;

class BasicAssign1 { 

public static void main(String args[]) 
{ 
    Scanner kb = new Scanner(System.in);
    System.out.println("输入字符串:");
    String word = kb.nextLine();
    char abc[]={};
    int a=0;
    a=word.length();

    
    for(int i=0; i<=word.length()-1; i++)
    {
        System.out.print(word.charAt(i));


    }

    
    int rows = word.length();
    int columns = word.length(); 
    int i;
    int j; 
    for (i = 0; i <= rows; i++) 
    { 

        for (j = 0; j <= columns; j++) 
        { 


            if (i == 0 || i == rows || j == 0 || j == columns) 
            {

                System.out.print(word.charAt(i));  
            }
            if( i==0 && j==columns){
                String word3 = "four";
                StringBuilder sb = new StringBuilder(word3);
                sb.reverse();
                word3 = sb.toString();
                System.out.println(word3);

            }
            else   
                System.out.print(" ");             
        } 
        System.out.println(); 
    } 


} 
} 

注意:我已经翻译了你提供的代码,但是这段代码中存在一些逻辑错误,所以翻译后的代码也可能不会得到预期的输出。如果需要进一步的帮助,请告诉我您希望实现的功能,我会尽力提供指导。

英文:

So I am trying to create a program where I ask the user to input a string and I output a hollow square with the string. So an example would look like if the user input "four":

Output:

fourf
r   o
u   u
o   r
fruof

So far my code is this:

// JAVA code for hollow rectangle 
import java.io.*; 
import java.awt.*;
import java.util.*;
class BasicAssign1 { 
public static void main(String args[]) 
{ 
Scanner kb = new Scanner(System.in);
System.out.println(&quot;Enter line: &quot;);
String word = kb.nextLine();
char abc[]={};
int a=0;
a=word.length();
for(int i=0; i&lt;=word.length()-1; i++)
{
System.out.print(word.charAt(i));
}
int rows = word.length();
int columns = word.length(); 
int i;
int j; 
for (i = 0; i &lt;=rows; i++) 
{ 
for (j = 0; j &lt;=columns; j++) 
{ 
if (i == 0 || i == rows || j == 0 || j == columns) 
{
System.out.print(word.charAt(i));  
}
if( i==0 &amp;&amp; j==columns){
String word3 = &quot;four&quot;;
StringBuilder sb = new StringBuilder(word3);
sb.reverse();
word3 = sb.toString();
System.out.println(word3);
}
else   
System.out.print(&quot; &quot;);             
} 
System.out.println(); 
} 
} 
} 

Currently this outputs:

fourf f f f fruof
o    o 
u    u 
r    r 

Now I only got 1-2 rows properly done, but the 3rd row which is suppose to be in inverse is printed at the top and also isn't aligned to make a square. Any help would be appreciated thanks!

答案1

得分: 6

以下是翻译好的部分:

你可以通过以下方式简化你的任务:

  • 第一行 = 单词 + 单词的第一个字母
  • 第二行到倒数第二行 = 单词[length-行数],空格 * (长度-1),单词[行数]
  • 最后一行 = 单词的第一个字母 + 单词反向排列

并编写一个实现这个逻辑的函数,例如:

public static void square(String word) {
    int len = word.length();
    // 第一行:单词 + 单词的第一个字母
    System.out.println(word + word.charAt(0));
    // 第二行到倒数第二行:单词[length-行数],空格 * (长度-1),单词[行数]
    // 创建空白字符串
    String blanks = String.join(" ", Collections.nCopies(len - 1, " "));
    for (int i = 1; i < len; i++) {
        System.out.println(word.charAt(len - i) + blanks + word.charAt(i));
    }
    // 最后一行:单词的第一个字母 + 单词反向排列
    String reversed = new StringBuilder(word).reverse().toString();
    System.out.println(word.charAt(0) + reversed);
}

你可以调用这个函数,例如:

square("four");
square("eleven");

这将产生以下输出:

fourf
r   o
u   u
o   r
fruof
elevene
n     l
e     e
v     v
e     e
l     n
enevele
英文:

You can simplify your task by noting that:

  • first row = word + first letter of word
  • second to length row = word[length-row], blank * length-1, word[row]
  • last row = first letter of word + word backward

and writing a function to implement that e.g.

public static void square(String word) {
int len = word.length();
// first row: word + first letter of word
System.out.println(word + word.charAt(0));
// second to length row: word[length-row], blank * length-1, word[row]
// create a blanks string
String blanks = String.join(&quot;&quot;, Collections.nCopies(len - 1, &quot; &quot;));
for (int i = 1; i &lt; len; i++) {
System.out.println(word.charAt(len-i) + blanks + word.charAt(i));
}
// last row: first letter of word + word backward
String reversed = new StringBuilder(word).reverse().toString();
System.out.println(word.charAt(0) + reversed);
}

You can call this e.g.

square(&quot;four&quot;);
square(&quot;eleven&quot;);

Which will give the following output:

fourf
r   o
u   u
o   r
fruof
elevene
n     l
e     e
v     v
e     e
l     n
enevele

huangapple
  • 本文由 发表于 2020年9月22日 08:09:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/64001400.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定