简体   繁体   English

如何用文字做一个空心的正方形?

[英]How to make a hollow square with words?

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.所以我试图创建一个程序,我要求用户输入一个字符串,然后我 output 是一个带有字符串的空心方块。 So an example would look like if the user input "four":因此,如果用户输入“四”,则示例如下:

Output: 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("Enter line: ");
    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(); 
    } 
    
    
     
} 
} 

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.现在我只正确完成了 1-2 行,但是应该是反的第 3 行打印在顶部并且也没有对齐以形成一个正方形。 Any help would be appreciated thanks!任何帮助将不胜感激谢谢!

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]第二个长度 row = word[length-row], blank * length-1, word[row]
  • last row = first letter of word + word backward最后一行=单词的第一个字母+向后的单词

and writing a function to implement that eg并编写一个 function 来实现它,例如

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("", Collections.nCopies(len - 1, " "));
    for (int i = 1; i < 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 eg你可以称之为例如

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

Which will give the following output:这将给出以下 output:

fourf
r   o
u   u
o   r
fruof

elevene
n     l
e     e
v     v
e     e
l     n
enevele

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM