非常基本的Java问题,嵌套for循环。

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

Very basic java question, nested for loops

问题

我正在尝试制作一个日历的一周块,但当我尝试连接这些天的块时,它们以纵向而不是并排的方式打印出来。我知道我有println语句,但我认为它不应该引起问题,因为它在一个循环内部。

代码:
```java
public class MyCalendar {
    public static final int SIZE = 10;

    public static void main(String[] args) {
        drawRow();
    }

    public static void drawRow() {
        for (int week = 1; week <= 7; week++) {
            divide();
            
            // 行
            for (int col = 1; col <= SIZE / 2; col++) {
                System.out.print("|");
                
                for (int space = 1; space <= SIZE - 1; space++) {
                    System.out.print(" ");
                }
                System.out.println();
            }
        }
    }

    public static void divide() {
        for (int length = 1; length <= 1; length++) {
            for (int top = 1; top <= SIZE; top++) {
                System.out.print("=");
            }
        }
        System.out.println();
    }
}

输出:

==========
|         
|         
|         
|         
|         
==========
|         
|         
|         
|         
|         
==========
|         
|         
|         
|         
|         
==========
|         
|         
|         
|         
|         
==========
|         
|         
|         
|         
|         
==========
|         
|         
|         
|         
|         
==========
|         
|         
|         
|         
|         

期望输出:

======================================================================
|         |         |         |         |         |         |         
|         |         |         |         |         |         |         
|         |         |         |         |         |         |         
|         |         |         |         |         |         |         
|         |         |         |         |         |         |         

我知道这是基本的内容,但我真的不知道我做错了什么。这肯定是println语句的问题,但它嵌套在循环内部,所以我不确定为什么会引起问题。谢谢。


<details>
<summary>英文:</summary>

I&#39;m trying to make a week block of a calendar, when I try and connect the day blocks, they print out vertically not next to each other? I know I have the println statement but I don&#39;t think that should be causing the issue as it&#39;s inside one of the loops.

code:

public class MyCalendar {
public static final int SIZE = 10;

public static void main(String[] args) {
    drawRow();

}

public static void drawRow() {

    for (int week = 1; week &lt;= 7; week++) {
        divide();
        // lines
        for (int col = 1; col &lt;= SIZE / 2; col++) {

            System.out.print(&quot;|&quot;);
            for (int space = 1; space &lt;= SIZE - 1; space++) {
                System.out.print(&quot; &quot;);
            }
            System.out.println();

        }

    }

}

public static void divide() {
    for (int length = 1; length &lt;= 1; length++) {

        for (int top = 1; top &lt;= SIZE; top++) {
            System.out.print(&quot;=&quot;);
        }
    }
    System.out.println();
}

}

output:

==========
|
|
|
|
|

|
|
|
|
|

|
|
|
|
|

|
|
|
|
|

|
|
|
|
|

|
|
|
|
|

|
|
|
|
|

What I want:

======================================================================
| | | | | | |
| | | | | | |
| | | | | | |
| | | | | | |
| | | | | | |

I know this is something basic but i really dont know what im doing wrong.  Its gotta be an issue with the println statement but its nested inside the for loop so im not sure why that would be causing the issues. Thanks

</details>


# 答案1
**得分**: 1

这就够了

```java
public class MyCalendar {

    public static void main(String[] args) {
        for (int week = 1; week <= 7; week++) {
            System.out.print("=======");
        }
        System.out.println();
        for (int week = 1; week <= 7; week++) {
            System.out.println("|       |       |       |       |       |       |");
        }

    }
}

输出:

=================================================
|       |       |       |       |       |       |
|       |       |       |       |       |       |
|       |       |       |       |       |       |
|       |       |       |       |       |       |
|       |       |       |       |       |       |
|       |       |       |       |       |       |
|       |       |       |       |       |       |
英文:

this much is enough

public class MyCalendar {

    public static void main(String[] args) {
        for (int week = 1; week &lt;= 7; week++) {
            System.out.print(&quot;=======&quot;);
        }
        System.out.println();
        for (int week = 1; week &lt;= 7; week++) {
            System.out.println(&quot;|       |       |       |       |       |       |&quot;);
        }

    }
}

output

=================================================
|       |       |       |       |       |       |
|       |       |       |       |       |       |
|       |       |       |       |       |       |
|       |       |       |       |       |       |
|       |       |       |       |       |       |
|       |       |       |       |       |       |
|       |       |       |       |       |       |

答案2

得分: 1

我认为你应该尝试这样写:

public static void drawRow() {
    header(SIZE * 7);
    for (int week = 1; week <= 7; week++) {
        // lines
        for (int col = 1; col <= 7; col++) {
            System.out.print("|");
            for (int space = 1; space <= SIZE - 1; space++) {
                System.out.print(" ");
            }
        }
        System.out.println();
    }
}

public static void header(int sz) {
    for (int top = 1; top <= sz; top++) {
        System.out.print("=");
    }
    System.out.println();
}

当我运行这段代码时,我能够看到你在问题中提到的输出结果:

src: $ java MyCalendar 
======================================================================
|         |         |         |         |         |         |         
|         |         |         |         |         |         |         
|         |         |         |         |         |         |         
|         |         |         |         |         |         |         
|         |         |         |         |         |         |         
|         |         |         |         |         |         |         
|         |         |         |         |         |         |  
英文:

I think you should try this:

public static void drawRow() {
    header(SIZE * 7);
    for (int week = 1; week &lt;= 7; week++) {
        // lines
        for (int col = 1; col &lt;= 7; col++) {
            System.out.print(&quot;|&quot;);
            for (int space = 1; space &lt;= SIZE - 1; space++) {
                System.out.print(&quot; &quot;);
            }
        }
        System.out.println();
    }
}

public static void header(int sz) {
    for (int top = 1; top &lt;= sz; top++) {
        System.out.print(&quot;=&quot;);
    }
    System.out.println();
}

When I run this, I can see the output you have in your question:

src : $ java MyCalendar 
======================================================================
|         |         |         |         |         |         |         
|         |         |         |         |         |         |         
|         |         |         |         |         |         |         
|         |         |         |         |         |         |         
|         |         |         |         |         |         |         
|         |         |         |         |         |         |         
|         |         |         |         |         |         |  

huangapple
  • 本文由 发表于 2020年10月17日 00:09:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/64392729.html
匿名

发表评论

匿名网友

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

确定