Java 如何使用两个for循环制作空心盒
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19393842/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make hollow box using two for loops
提问by Claudia Gomes
Use nested for loops statements to draw hallow boxes of "*"s. The boxes have the same number of rows and columns and this number should be input from the user (valid range: 5 to 21). I'm having trouble coming up with a way to make the box hollow. this is what i have for the code and it comes as a complete square, but i need it to be hollow or just the border.
使用嵌套的 for 循环语句绘制“*”的中空框。这些框具有相同的行数和列数,该数字应由用户输入(有效范围:5 到 21)。我在想办法让盒子变空时遇到了麻烦。这就是我的代码,它是一个完整的正方形,但我需要它是空心的或只是边框。
System.out.println("How many rows/columns(5-21)?");
rows=input.nextInt();
while(rows<5||rows>21){
System.out.println("Out of range. Reenter: ");
rows=input.nextInt();
}
for(m=1;m<=rows;m++){
for(c=1;c<=rows;c++){
System.out.print("*");
}
System.out.println();
}
the output should look like this: How many rows/columns (5-21)? 25 Out of range. Reenter: 7
输出应如下所示:多少行/列 (5-21)?25 超出范围。再入:7
*******
* *
* *
* *
* *
* *
*******
回答by cxw
You need to print only some of the *
s, so add a test before the print("*")
. One option would be to explicitly test the four conditions (top, bottom, left, right) and OR them together logically:
您只需要打印*
s 中的一部分,因此在print("*")
. 一种选择是显式测试四个条件(顶部、底部、左侧、右侧)并将它们逻辑地 OR 在一起:
if( (m==1) || //top
(m==rows) || //bottom
(c==1) || //left
(c==rows) //right
) {
System.out.print("*");
} else {
System.out.print(" ");
}
Each m==
test or c==
test identifies one piece of the square. Since the four tests
are ORed together, the if() is true (and a * is printed) if any one of the four tests is true. If none of them are true, the else
runs and prints a space.
每个m==
测试或c==
测试标识一块正方形。由于四个测试是 OR 运算在一起的,如果四个测试中的任何一个为真,则 if() 为真(并打印 *)。如果它们都不为真,则else
运行并打印一个空格。
I also recommend renaming m
to rowIndex
and c
to colIndex
or something. When you come back to the code a week or two later, more descriptive names will make it easier to pick up where you left off. (Ask me how I know!)
我还建议重新命名m
,以rowIndex
和c
以colIndex
什么的。当您在一两周后返回代码时,更具描述性的名称将使您更容易从上次中断的地方继续学习。(问我怎么知道的!)
回答by oushnn
import java.util.Scanner;
public class icibos {
public static void main(String[] args) {
Scanner gir = new Scanner(System.in);
System.out.print("Karenin kenar?n? girin: ");
int kenar = gir.nextInt();
for (int i = 1; i <= kenar; i++) {
for (int j = 1; j <= kenar; j++) {
if (i == 1 || i == kenar || j == 1 || j == kenar)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
}
回答by KateChong
for (int i=1;i<=lgh;i++){
for (int a=1;a<=lgh;a++){
if(i>1 && i<lgh && a>1 && a<lgh)
System.out.print(" ");
else
System.out.print("*");
}
System.out.println("");
}
回答by RishiKesh Pathak
回答by RishiKesh Pathak
You can try this
你可以试试这个
Example is Here
例子在这里
String pattern;
int noOfTimes;
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the pattern to print : ");
pattern = scanner.nextLine();
System.out.print("Enter number of times it should get printed : ");
noOfTimes = scanner.nextInt();
for(int i=1; i<=noOfTimes; i++) {
System.out.println();
if(i==1 || i==noOfTimes) {
for(int j=1; j<=noOfTimes; j++){
System.out.print(pattern+" ");
}
}
else {
for(int k=1; k<=noOfTimes;k++) {
if(k==1 || k == noOfTimes) {
System.out.print(pattern + " ");
}
else {
System.out.print(" ");
}
}
}
}
回答by IMP
import java.util.Scanner;
public class holsqr{
public static void main(String args[]){
Scanner ma=new Scanner(System.in);
System.out.print("Enter the number:");
int max=ma.nextInt();
for(int i=1;i<=max;i++){
for(int j=1;j<=max;j++){
if((i==1)||(i==max)){
System.out.print("#");
}else{
if(j==1||j==max){
System.out.print("#");
}
else{
System.out.print(" ");
}
}
}
System.out.println();
}
}
}
回答by Hieu Nguyen
It pretty simple, using two while loop: public static void main(String[] args) {
它非常简单,使用两个 while 循环: public static void main(String[] args) {
int size = 0;
int r = 0;
String star = "*";
String space = " ";
System.out.print("Input size of side of square: ");
Scanner input = new Scanner(System.in);
size = input.nextInt();
while (r < size) {
int c = 0;
while (c < size) {
System.out.print(r > 0 && r < size - 1 && c > 0 && c < size - 1 ? space : star);
++c;
}
System.out.println();
++r;
}
}