简体   繁体   中英

Simple Calendar display Java program. Having issue with display pattern condition in for loop

Problem:

Write a JAVA program that produces a calendar. Your program should output a calendar for a single month, given parameters to specify how many days in the month and the day of the first Sunday.

I have written this program and need help in fixing the loop condition

Expected results在此处输入图片说明

Actual Result:在此处输入图片说明

My code has running | spaces and is unable to add floating spaces with pipes when the calendar has no value in the day field. I believe the issue is with this loop, I am hardcoding 35 which needs to be replaced with some condition that allows to print padded pipes.

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

My code:

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;
    }
}

I think this solves your biggest problem (apart from the formatting issues). The problem is that you're hardcoding the value that prints blank spaces. U can simply use the remainder of the number of days divided by 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("|");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM