如何制作一个字符矩形?

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

How to make a rectangle of characters?

问题

int a = requestNumber();
int b = requestNumber();
for (int i = 0; i < a; i++) {
    for (int j = 0; j < b; j++) {
        if (j == 0 || j < (b - 1))
            System.out.print("*");
        else {
            System.out.print(" ");
        }
    }
    System.out.println();
}

请注意,我已经根据您的要求,将代码部分保持不变,只翻译了注释内容。如果您有任何其他需要,请随时告诉我。

英文:

How to make a rectangle of characters?
I need to make such a picture appear in the console

 **********
*          *
*          *
*          *
 **********

But I get this:

********* 
********* 
********* 
********* 
********* 

Sides a and b are entered from the console.

    int a = requestNumber();
    int b = requestNumber();
    for (int i = 0; i &lt; a; i++) {
        for (int j = 0; j &lt; b; j++) {
            if (j == 0 || j &lt; (b - 1))
            System.out.print(&quot;*&quot;);
            else {
                System.out.print(&quot; &quot;);
            }
        }
        System.out.println();
    }
}

答案1

得分: 1

public class Rectangle {
    public static void main(String[] args) {
        Rectangle rectangle = new Rectangle();
        rectangle.printRectangle(7, 9);
    }

    private void printRectangle(int row, int col) {
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                if ((isFirstOrLastRow(i, row) && isFirstOrLastCol(j, col))
                        || !(isFirstOrLastRow(i, row) || isFirstOrLastCol(j, col))) {
                    System.out.print(" ");
                } else {
                    System.out.print("*");
                }
            }
            System.out.println();
        }
    }

    private boolean isFirstOrLastRow(int currentRow, int row) {
        return currentRow == 0 || currentRow == row - 1;
    }

    private boolean isFirstOrLastCol(int currentCol, int col) {
        return currentCol == 0 || currentCol == col - 1;
    }
}

The four corners and the middle position should output spaces, so we should judge whether it is the four corners or the middle position. I use isFirstOrLastRow and isFirstOrLastCol to help judge.

Here is the result for input 7,9:

 ******* 
*       *
*       *
*       *
*       *
*       *
 ******* 
英文:
public class Rectangle {
    public static void main(String[] args) {
        Rectangle rectangle = new Rectangle();
        rectangle.printRectangle(7,9);
    }

    private void printRectangle(int row, int col) {
        for (int i = 0; i &lt; row; i++) {
            for (int j = 0; j &lt; col; j++) {
                if ((isFirstOrLastRow(i, row) &amp;&amp; isFirstOrLastCol(j, col))
                        || !(isFirstOrLastRow(i, row) || isFirstOrLastCol(j, col))) {
                    System.out.print(&quot; &quot;);
                }
                else {
                    System.out.print(&quot;*&quot;);
                }
            }
            System.out.println();
        }
    }

    private boolean isFirstOrLastRow(int currentRow, int row) {
        return currentRow == 0 || currentRow == row - 1;
    }

    private boolean isFirstOrLastCol(int currentCol, int col) {
        return currentCol == 0 || currentCol == col - 1;
    }
}

The four corners and the middle position should output spaces, so we should judge whether it is the four corners or the middle position,I use isFirstOrLastRow and isFirstOrLastCol to help judge.

here is the result for input 7,9

 ******* 
*       *
*       *
*       *
*       *
*       *
 ******* 

答案2

得分: 1

一个相当简洁的方法只需几行代码:

int a = requestNumber();
int b = requestNumber();
	    
System.out.println(" " + new String(new char[a]).replace("
int a = requestNumber();
int b = requestNumber();
	    
System.out.println(" " + new String(new char[a]).replace("\0", "*"));
for (int i = 0; i < b; i++) {	        
    System.out.println("*" + new String(new char[a]).replace("\0", " ") + "*");
}
System.out.println(" " + new String(new char[a]).replace("\0", "*"));
"
, "*"));
for (int i = 0; i < b; i++) { System.out.println("*" + new String(new char[a]).replace("
int a = requestNumber();
int b = requestNumber();
	    
System.out.println(" " + new String(new char[a]).replace("\0", "*"));
for (int i = 0; i < b; i++) {	        
    System.out.println("*" + new String(new char[a]).replace("\0", " ") + "*");
}
System.out.println(" " + new String(new char[a]).replace("\0", "*"));
"
, " ") + "*");
} System.out.println(" " + new String(new char[a]).replace("
int a = requestNumber();
int b = requestNumber();
	    
System.out.println(" " + new String(new char[a]).replace("\0", "*"));
for (int i = 0; i < b; i++) {	        
    System.out.println("*" + new String(new char[a]).replace("\0", " ") + "*");
}
System.out.println(" " + new String(new char[a]).replace("\0", "*"));
"
, "*"));

编辑:修复了拼写错误,现在可以正常运行!

英文:

A pretty concise way to do it just a few lines:

int a = requestNumber();
int b = requestNumber();
	    
System.out.println(&quot; &quot; + new String(new char[a]).replace(&quot;
int a = requestNumber();
int b = requestNumber();
System.out.println(&quot; &quot; + new String(new char[a]).replace(&quot;\0&quot;, &quot;*&quot;));
for (int i = 0; i &lt; b; i++) {	        
System.out.println(&quot;*&quot; + new String(new char[a]).replace(&quot;\0&quot;, &quot; &quot;) + &quot;*&quot;);
}
System.out.println(&quot; &quot; + new String(new char[a]).replace(&quot;\0&quot;, &quot;*&quot;));
&quot;, &quot;*&quot;)); for (int i = 0; i &lt; b; i++) { System.out.println(&quot;*&quot; + new String(new char[a]).replace(&quot;
int a = requestNumber();
int b = requestNumber();
System.out.println(&quot; &quot; + new String(new char[a]).replace(&quot;\0&quot;, &quot;*&quot;));
for (int i = 0; i &lt; b; i++) {	        
System.out.println(&quot;*&quot; + new String(new char[a]).replace(&quot;\0&quot;, &quot; &quot;) + &quot;*&quot;);
}
System.out.println(&quot; &quot; + new String(new char[a]).replace(&quot;\0&quot;, &quot;*&quot;));
&quot;, &quot; &quot;) + &quot;*&quot;); } System.out.println(&quot; &quot; + new String(new char[a]).replace(&quot;
int a = requestNumber();
int b = requestNumber();
System.out.println(&quot; &quot; + new String(new char[a]).replace(&quot;\0&quot;, &quot;*&quot;));
for (int i = 0; i &lt; b; i++) {	        
System.out.println(&quot;*&quot; + new String(new char[a]).replace(&quot;\0&quot;, &quot; &quot;) + &quot;*&quot;);
}
System.out.println(&quot; &quot; + new String(new char[a]).replace(&quot;\0&quot;, &quot;*&quot;));
&quot;, &quot;*&quot;));

EDIT: Fixed typo, works now!

huangapple
  • 本文由 发表于 2020年5月29日 10:59:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/62077973.html
匿名

发表评论

匿名网友

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

确定