1

关于如何在名为 Window 的类中编写 Java 程序的任何想法,该类会产生上图作为输出。我必须使用嵌套的 for 循环来打印图形的重复部分。我试过很多次,没有成功:(

在一个名为 Window 的类中编写一个 Java 程序,该类生成上图作为输出。使用嵌套的 for 循环打印图形的重复部分。一旦你让它工作,在你的程序中添加一个类常量,这样图形的大小就可以通过改变这个常量的值来改变。显示的示例输出是常数大小 3,但如果更改常数,数字应该会按比例变大和变宽。

+===+===+
|   |   |
|   |   |
|   |   |
+===+===+
|   |   |
|   |   |
|   |   |
+===+===+

好的,我知道了,但仍然需要摆脱 3 条底线——知道吗?

    for (int i = 1; i <= 3; i++) {
        for (int plus = 1; plus <= 2; plus++) {
            System.out.print("+");
        for (int shave = 1; shave <= 3; shave++) {
                System.out.print("=");
            }
            }
        System.out.print("+");
            System.out.println();
    for (int time = 1; time <= 3; time++) {
         for (int kav = 1; kav <= 3; kav++) {
                 System.out.print("|");
             for (int rev = 1; rev <= 3; rev++) {
                 System.out.print(" ");
             }
             }
         System.out.println();
        }
    }

}

4

7 回答 7

3

我认为这就是您正在寻找的:

        final int BLOCK_SIZE = 2;
        for(int i=0; i<1; i++){
            System.out.print("+===+");
            for(int j=0; j<1; j++){
                System.out.println("===+");
                for(int k=0; k<BLOCK_SIZE; k++){
                    System.out.println("|   |   |\n|   |   |\n|   |   |");
                    for(int l=0; l<1; l++){
                        System.out.println("+===+===+");
                    }
                } System.out.println();
            }
        }
于 2012-02-05T01:21:10.650 回答
1

我正在通过随机问题,我发现没有人通过更正您发布的代码来正确回答。所以在这里我用嵌套的 for 循环发布我的代码,它满足你的所有标准,给出正确的输出。

int BLOCK_SIZE = 4;
for (int i=1; i<BLOCK_SIZE; i++){
    for(int j=1; j<BLOCK_SIZE; j++){
        System.out.print("+===");
    }
    System.out.println("+");
    for(int k=0; k<3; k++){
        for(int j=1; j<BLOCK_SIZE; j++){
            System.out.print("|   ");
        }
        System.out.println("|");
    }
}
for(int j=1; j<BLOCK_SIZE; j++){
        System.out.print("+===");
    }
System.out.println("+");

使用一些 if 语句的另一种方法

  • 在这里,我利用了每 (4n+1) 行都有 '+' 替换 '|' 的事实 和 '=' 替换 'whitespace'

    int BLOCK_SIZE = 5;
    int length = BLOCK_SIZE*4-4;
    char one,two;
    
    for(int i=0; i<=length; i++){
        if(i%4!=0){one='|';two=' ';}
        else{one='+';two='=';}
        for(int j=0; j<=length; j++){
            if(j%4==0) {
            System.out.print(one);
            }
            else {
            System.out.print(two);
            }
        }
        System.out.println(); 
    }
    

诚然这是一个家庭作业问题,但即使它们没有任何实际用途,我们也可以很开心地破解它们。这就是编码可以得到的快乐!:)

于 2013-04-11T20:20:27.637 回答
1

花了一些时间来达到您的预期结果,看看它是否适合您?

public class Homework
{

    /**
     * @param args
     */
    public static void main(String[] args)
    {
            for (int line = 1; line <= 3; line ++)
            {

                NEWLINE:

                    for (int plus = 1; plus <= 3; plus++)
                    {
                        System.out.print("+");
                        if (plus == 3) 
                        {
                            for (int k = 1; k <= 3; k++)
                            {
                                if (line == 3)
                                {
                                    break NEWLINE;
                                }
                                System.out.println("");
                                System.out.print(" |         |          |");
                                if (k == 3)
                                {
                                    System.out.println();
                                    break NEWLINE;
                                }
                            }
                        }

                        for (int eq = 1; eq <= 6; eq++)
                        {
                            if (eq % 4 == 0)
                            {                           
                                break;
                            }
                            System.out.print("=");
                        }

                    }
            }
    }
}
于 2012-02-05T04:57:45.053 回答
0

这是一种方法

    int rows = 3;
    int cols = 4;
    String output1 = "";
    String output2 = "";
    for(int j = 0; j < cols; j++)
    {
        output1 += "|   ";
    }
    output1 += "|";
    for(int j = 0; j < cols; j++)
    {
        output2 += "+===";
    }
    output2 += "+";
    for(int i = 0; i < rows*4; i++)
    {
        System.out.println((i % 4 != 0)?output1:output2);
    }
    System.out.println(output2);
于 2012-02-04T23:02:03.827 回答
0

可以重构为更漂亮,但会起作用:

static String[][] array = { { "x", "="}, { "|", " "}};

public static void main(String[] args) {
    for(int y = 0; y < 9; y++) {
        for(int x = 0; x < 9; x++) {
            System.out.print(getSign(y, x));
        }
        System.out.print("\n");
    }
}

private static String getSign(int y, int x) {   
    int modY = y % 4;
    int modX = x % 4;
    return array[getPos(modY)][getPos(modX)];
}

private static int getPos(int mod) {
    return (mod & 1) | ((mod & 2) >> 1);
}

于 2012-02-04T23:19:33.050 回答
0

这是最简单的方法。

public class Window {
    public static final int size = 3;

    public static void main(String[] args) {
        for (int x=0; x<2; x++) {
            for (int y = 0; y<2; y++){
                System.out.print("+");
                for (int z=0;z<size; z++) {
                    System.out.print("=");
                }
            }
            System.out.println("+");
            for (int i = 0; i<size; i ++) {
                for (int j = 0; j<2; j++) {
                    System.out.print("|");
                    for (int k = 0; k<size; k++) {
                        System.out.print(" ");
                    }
                }
                System.out.println("|");
            }
        }
         for (int y = 0; y<2; y++){
                System.out.print("+");
                for (int z=0;z<size; z++) {
                    System.out.print("=");
                }
            }
        System.out.println("+");
    }
}
于 2016-08-30T18:05:25.040 回答
-1
public class Window{
    public static final int size=3;
    public static void main(String[] args){
       for (int p = 1; p <= 2; p++) {
            for (int i = 1; i <=2; i++) {
                System.out.print("+");
                for (int j = 1; j <= size; j++) {
                    System.out.print("=");
                }
            }
                System.out.print("+");
                System.out.println();
            for (int k = 1; k <= size; k++) {
                for (int i = 1; i <= 3; i++) {
                    System.out.print("|");
                    for (int j = 1; j <= size; j++) {
                        System.out.print(" ");
                    }
                }
                System.out.println();
            }
        }
        for (int i = 1; i <= 2; i++) {
            System.out.print("+");
            for (int j = 1; j <= size; j++) {
                System.out.print("=");
            }
        }
        System.out.print("+");
    }
}
于 2015-02-16T04:21:02.717 回答