LeapYearExtended

mail@pastecode.io avatar
unknown
java
3 years ago
1.9 kB
5
Indexable
Never
import java.io.*;
import java.util.Scanner;

public class LeapYearExtended {

    public static void main (String [] args) throws IOException {
        
        Scanner scan = new Scanner(System.in);

        System.out.println();

        System.out.print("> Start year: ");
        int start_year = scan.nextInt();
        System.out.print("> End year:   ");
        int end_year = scan.nextInt();

        if (end_year < start_year){
            System.out.println("[!] Invalid value of end year");
            System.exit(1);
        }

        System.out.print("> Save information? (y/n): ");
        String choice = scan.next().trim().toLowerCase();

        char c = choice.charAt(0);

        boolean save_info = false;

        if (c == 'y'){
            save_info = true;
        }

        PrintWriter file_handler = new PrintWriter("leap-year.txt");

        int cy = start_year; // current year (cy)
        int line_break = 0;

        System.out.println();

        while (true){

            if ( (cy % 4 == 0 ) && (cy % 100 != 0) ){
                System.out.printf("%d ", cy);

                if (save_info == true){
                    file_handler.print(cy + " ");
                }

                if (line_break == 6){ // Prints 7 years / line
                    System.out.println();

                    if (save_info == true){
                        file_handler.println("");
                    }

                    line_break = 0;
                } else {
                    line_break++;
                }
            } 

            if (cy == end_year){
                break; // Making sure we break out of loop
                       // after the end year has been reached
            } else {
                cy++;
            }
        }
        
        System.out.println("\n");

        if (save_info == true){
            file_handler.close();
        }
    }
}