简单的日历显示Java程序。for循环中的显示模式条件存在问题

简单的日历显示Java程序。for循环中的显示模式条件存在问题,java,Java,问题: import java.util.Scanner; public class Calendart { public static void main(String[] args) { Scanner console = new Scanner(System.in); System.out.println(); System.out.print("Please enter the number of days in the

问题:

import java.util.Scanner;

public class Calendart {
    public static void main(String[] args) {
        Scanner console = new Scanner(System.in);
        System.out.println();
        System.out.print("Please enter the number of days in the month: ");
        int days = console.nextInt();
        System.out.print("Please enter the number of the day of the first Sunday: ");
        int sunday = console.nextInt();
        System.out.println();

        calendarHeader();
        borderTopBottom();
        calendarBody(sunday, days);
        borderTopBottom();
    }

    static double getInt(Scanner sc, String s) {
        int d = 0;
        System.out.print(s);
        d = sc.nextInt();

        return d;
    }

    static void calendarBody(int sunday, int days) {
        int temp = 8 - sunday;
        if (sunday == 1) {
            System.out.printf(""); // if sunday is on 1st day, then do not print any spaces
        } else
            for (int space = 1; space <= temp; space++) { // Prints spaces to the first row if no days are listed
                System.out.print("|");
                System.out.printf("%6s", " ");
            }

        for (int i = 1; i <= days; i++) { // Printing the days
            temp++;
            if (temp % 7 == 0)
                System.out.println("|" + padded(i, 4) + " " + "|");
            else
                System.out.print("|" + padded(i, 4) + " ");

        }

        for (int j = days; j < 35; j++) {
            System.out.print("|");
            System.out.printf("%6s", " ");
        }

        sunday = (sunday + days) % 7;
    }

    static void calendarHeader() {
        System.out.print(" Sun ");
        System.out.print("Mon ");
        System.out.print("Tue ");
        System.out.print("Wed ");
        System.out.print("Thu ");
        System.out.print("Fri ");
        System.out.print("Sat");
    }

    static void borderTopBottom() {
        System.out.println();
        for (int i = 1; i <= 7; i++) {
            System.out.print("+");
            for (int j = 1; j <= 6; j++) {
                System.out.print("-");
            }
        }

        System.out.print("+");
        System.out.println();
    }

    public static String padded(int n, int width) {
        String s = "" + n;
        for (int i = s.length(); i < width; i++) {
            s = " " + s;
        }

        return s;
    }
}
编写一个生成日历的JAVA程序。您的程序应该输出一个月的日历,给定参数指定该月的天数和第一个星期日的日期

我已经写了这个程序,需要帮助修复循环条件

预期结果

实际结果:

我的代码有运行|空格,当日历在day字段中没有值时,无法使用管道添加浮动空格。我相信问题是这个循环,我硬编码35,需要更换一些条件,允许打印填充管道

for (int j = days; j < 35; j++) {
    System.out.print("|");
    System.out.printf("%6s", " ");
}
for(int j=days;j<35;j++){
系统输出打印(“|”);
System.out.printf(“%6s”和“);
}
我的代码:

import java.util.Scanner;

public class Calendart {
    public static void main(String[] args) {
        Scanner console = new Scanner(System.in);
        System.out.println();
        System.out.print("Please enter the number of days in the month: ");
        int days = console.nextInt();
        System.out.print("Please enter the number of the day of the first Sunday: ");
        int sunday = console.nextInt();
        System.out.println();

        calendarHeader();
        borderTopBottom();
        calendarBody(sunday, days);
        borderTopBottom();
    }

    static double getInt(Scanner sc, String s) {
        int d = 0;
        System.out.print(s);
        d = sc.nextInt();

        return d;
    }

    static void calendarBody(int sunday, int days) {
        int temp = 8 - sunday;
        if (sunday == 1) {
            System.out.printf(""); // if sunday is on 1st day, then do not print any spaces
        } else
            for (int space = 1; space <= temp; space++) { // Prints spaces to the first row if no days are listed
                System.out.print("|");
                System.out.printf("%6s", " ");
            }

        for (int i = 1; i <= days; i++) { // Printing the days
            temp++;
            if (temp % 7 == 0)
                System.out.println("|" + padded(i, 4) + " " + "|");
            else
                System.out.print("|" + padded(i, 4) + " ");

        }

        for (int j = days; j < 35; j++) {
            System.out.print("|");
            System.out.printf("%6s", " ");
        }

        sunday = (sunday + days) % 7;
    }

    static void calendarHeader() {
        System.out.print(" Sun ");
        System.out.print("Mon ");
        System.out.print("Tue ");
        System.out.print("Wed ");
        System.out.print("Thu ");
        System.out.print("Fri ");
        System.out.print("Sat");
    }

    static void borderTopBottom() {
        System.out.println();
        for (int i = 1; i <= 7; i++) {
            System.out.print("+");
            for (int j = 1; j <= 6; j++) {
                System.out.print("-");
            }
        }

        System.out.print("+");
        System.out.println();
    }

    public static String padded(int n, int width) {
        String s = "" + n;
        for (int i = s.length(); i < width; i++) {
            s = " " + s;
        }

        return s;
    }
}
import java.util.Scanner;
公共类日历{
公共静态void main(字符串[]args){
扫描仪控制台=新扫描仪(System.in);
System.out.println();
System.out.print(“请输入当月天数:”);
int days=console.nextInt();
System.out.print(“请输入第一个星期日的日期:”);
int sunday=console.nextInt();
System.out.println();
日历头();
borderTopBottom();
日历正文(星期日,天);
borderTopBottom();
}
静态双GEINT(扫描程序sc、字符串s){
int d=0;
系统输出打印;
d=sc.nextInt();
返回d;
}
静态无效日历正文(整数周日、整数天){
int temp=8-周日;
如果(星期日==1){
System.out.printf(“”;//如果星期天是第一天,那么不要打印任何空格
}否则

对于(int space=1;space我认为这解决了您最大的问题(除了格式问题)。问题是您正在硬编码打印空格的值。您只需使用天数的剩余部分除以7即可

int remainingDays = days % 7;

for (int j = 0; j < remainingDays; j++) {
    System.out.print("|");
    System.out.printf("%6s", " ");
}
if(remainingDays > 0)
    System.out.println("|");
int remainingDays=days%7;
对于(int j=0;j0)
System.out.println(“|”);

请缩进您的代码,以及您在for循环中需要帮助的具体内容。我运行了您的代码,它似乎运行得很好,只是格式有问题。而且我在(int j=days;j<35;j++)中看不到这一点我想这是用来填补空白的吗?是的,代码运行查找,我可以使它工作,但是如果你参考屏幕截图的预期和实际结果。我的实际已经在管道/空间上运行。我认为这是因为我在这个for循环条件下硬编码35:(int j=days;jI发布了一个答案,我认为它解决了你的主要问题。如果它对你有帮助,不要忘记接受它作为一个答案:)感谢您的回复。我对编程非常陌生,主要是通过尝试来解决问题。这确实有帮助。但问题是剩余天数并不总是第%7天。最后一行结果将随第一个星期日的日期而变化。因此仍然有未填充的空格。最后一行空格格式是我无法正确设置的。