英文:
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 < 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();
}
}
答案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 < 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
*******
* *
* *
* *
* *
* *
*******
答案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(" " + 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", "*"));
", "*"));
EDIT: Fixed typo, works now!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论