简体   繁体   English

Java中具有不同字符的空心正方形

[英]hollow square with different characters in java

So i have been tasked with creating a hollow square which basically needs to look like this. 所以我受命创建一个空心正方形,基本上需要看起来像这样。

*----*
|    |
|    |
|    |
*----*

(size varying on user input) The user is supposed to input width and length. (大小随用户输入而变化)应该由用户输入宽度和长度。 As for now i've been able to create a hollow square and a full square and everything. 到目前为止,我已经能够创建一个空心正方形和一个完整正方形以及所有内容。 Now i'm just realy stumped towards how i can create the square with the different characters.. 现在我真的很困惑如何创建具有不同角色的正方形。

import java.util.Scanner;
public class HulR{
  public static void main (String []args) {
 Scanner tastatur = new Scanner(System.in) ;

 int bredde;
 int lengde;


 System.out.print("bredde") ; 
 bredde = tastatur.nextInt();

 System.out.print("lengde"); 
 lengde = tastatur.nextInt();

 for (int j = 1; j<= bredde; j++) 
   for (int i = 1; i <= lengde; i++){
     if (i == 1 || i == lengde) 
       System.out.print("*");

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

This is how far i've come.. i'm 3 weeks into a beginners course for programming and i'm just lost when it comes to this task.. 这就是我要走的路。.我参加了编程的初学者课程3周,而我在完成这项任务时迷失了。

bredde = width and lengde = length btw 品种=宽度和长度=长度

Think about it: you are printing it line by line. 考虑一下:您正在逐行打印它。

The first and last line are different from the "middle" ones in that they are of the form --- , while the other are in the form | 第一行和最后一行与“中间”行的不同之处在于它们的格式为--- ,而另一行的格式为|。 |. |。

So we must differentiate between these two cases: 因此,我们必须区分这两种情况:

for (int i = 1; i<=height; i++) {
   for (int j = 1; j<=width; j++) {
     if (isFirstOrLastLine(i, height)) {
        //print like this: *--*
     }
     else {
        //print like this : |  |
     }
   }
}

Now, how do we know if we are on the first or last line: 现在,我们如何知道我们在第一行还是最后一行:

boolean isFirstOrLastLine(int line, int height) {
  return i == 1 || i == height;
}

Now we can fill in the logic to print the actual line! 现在我们可以填写逻辑以打印实际的行!

for (int i = 1; i<=height; i++) {
   for (int j = 1; j<=width; j++) {
     if (isFirstOrLastLine(i, height)) {
        //print like this: *--*
        if (isFirstOrLastColumn(j, width)) {
           System.out.print("*");
        }
        else {
           System.out.print("-");
        }
     }
     else {
        //print like this : |  |
        if (isFirstOrLastColumn(j, width)) {
           System.out.print("|");
        }
        else {
           System.out.print(" ");
        }
     }
   }
}

Can you guess the code for the "isFirstOrLastColumn" function yourself? 您可以自己猜出“ isFirstOrLastColumn”函数的代码吗?

Here is a pretty solution for the problem: 这是解决问题的一个不错的解决方案:

Code : 代码

import java.util.Arrays;
import java.util.Scanner;

public class Program {
    public static void main(String[] args) {
        // query parameters
        Scanner in = new Scanner(System.in);

        System.out.print("width=");
        int width = in.nextInt();

        System.out.print("height=");
        int height = in.nextInt();

        // setup model
        char[] headline = new char[width];
        Arrays.fill(headline, '-');
        headline[0] = '*';
        headline[width - 1] = '*';

        char[] midline = new char[width];
        Arrays.fill(midline, ' ');
        midline[0] = '|';
        midline[width - 1] = '|';

        // draw model
        System.out.println(headline);
        if (height >= 2) {
            for (int r = 1; r < height - 1; r++) {
                System.out.println(midline);
            }
            System.out.println(headline);
        }
    }
}

width and height are assumed to be greater than 0, feel free to add code to handle incorrect user input. widthheight假定大于0,请随时添加代码以处理错误的用户输入。

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

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