这是我的复活节计算器的代码。工作正常,想知道是否有一种方法可以提高效率(尤其是月份选择部分)。if 语句非常长,想知道我是否可以使用数组来选择月份。对于将来的参考,这样的 if 语句将非常耗时,感谢您的反馈。
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+".");
}
}
喵喔喔
皈依舞
狐的传说
守着一只汪
相关分类