简体   繁体   中英

possible way to make easter calculator code more efficient?

This is my code for my Easter calculator. Works fine, wondering if there was a way to make it more efficient (the month selection portion especially). The if-statements are very long and wondering if I could use an array to select the month. For future references, if-statements like this would be very time consuming, thanks for the feedback.

import java.util.*;
import java.lang.Math;

class Main {
    public static void main(String[] args) {
        Scanner userInput = new Scanner(System.in);

        System.out.println("\nWelcome to the Easter Calculator. Please enter the current year below.");
        int y = userInput.nextInt();

        int p = y/100;

        int q = y - (19*(y/19));

        int r = (p-17)/25;

        int s = p - (p/4) - ((p-r)/3) + (19*q) + 15;

        s = s - (30*(s/30));

        s = s - ((s/28)*1-((s/28)*(29/(s+1))*((21-q)/11)));

        int t = y + (y/4) + s + 2 - p + (p/4);

        t = t - (7*(t/7));

        int u = s - t;

        int m = 3 + ((u+40)/44);

        int d = u + 28 - (31*(m/4));

        String month;

        if(m == 1){
            month = "January";
        }
        else if(m == 2){
            month = "February";
        }
        else if(m == 3){
            month = "March";
        }
        else if(m == 4){
            month = "April";
        }
        else if(m == 5){
            month = "May";
        }
        else if(m == 6){
            month = "June";
        }
        else if(m == 7){
            month = "July";
        }
        else if(m == 8){
            month = "August";
        }
        else if(m == 9){
            month = "September";
        }
        else if(m == 10){
            month = "October";
        }
        else if(m == 11){
            month = "November";
        }
        else{
            month = "December";
        }

        System.out.println("\nEaster will be on "+month+" "+d+", "+y+".");

    }
}

Probably the fastest decoding with least lines of code would be by an array of Strings, let's call it monthNames , containing the names of all months. It would be of length 12, and since array indexes are 0-based you'd have have to get the String eg for month 1 this way: String month = monthNames[m-1];

I suggest using a switch statement. Here is something to start you off:

switch(m) {
    case 1: month = "January";
        break;
    case 2: month = "February";
        break;
    case 3: month = "March";
        break;
    case 4: month = "April";
        break;
    ...
    case 11: month = "November";
        break;
    default: month = "December";
}

As well, I suggest indenting your code properly, it makes it much easier to read and debug if anything comes up. As well, I recommend giving your variables meaningful names. Single letter names don't mean much, and as such it can get very confusing quickly.

Someone else mentioned it, but I would go for an array as well

something like:

    String month = "";
    int m = 1; // january

    String[] months = {"January", "Febuary", "March", "April", "May", "June", "July", "August",
            "September", "October", "November", "December"};

    month = months[m-1];

Thank you for the feedback. The switch statements worked as well as the string Array. I found the array much easier to use!

//Array to hold each month of the year
String monthArray[] = {"January", "Febuary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};

//Final output statement stating the month, day, and year easter will be held.
System.out.println("\nEaster will be on "+monthArray[m-1]+" "+d+", "+y+".");

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