英文:
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'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't think that should be causing the issue as it'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 <= 7; week++) {
divide();
// lines
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();
}
}
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 <= 7; week++) {
System.out.print("=======");
}
System.out.println();
for (int week = 1; week <= 7; week++) {
System.out.println("| | | | | | |");
}
}
}
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 <= 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();
}
When I run this, I can see the output you have in your question:
src : $ java MyCalendar
======================================================================
| | | | | | |
| | | | | | |
| | | | | | |
| | | | | | |
| | | | | | |
| | | | | | |
| | | | | | |
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论