gpt4 book ai didi

java - 我的代码有什么问题吗?泽勒同余算法代码

转载 作者:行者123 更新时间:2023-12-01 23:59:32 25 4
gpt4 key购买 nike

我的代码有什么问题吗?出于某种原因,我总是把休息日推迟一天?例如,今天是 2013 年 26 号,是星期二,但程序告诉我今天是星期三。我正在使用 Zeller 的同余算法。

import javax.swing.JOptionPane;
public class zeller {
public static void main(String[] args) {
String yearString = JOptionPane.showInputDialog("Enter the year:");

int year = Integer.parseInt(yearString);

String monthString = JOptionPane.showInputDialog("Enter the month (3-12)(January and Feburary are 13 and 14):");
int month = Integer.parseInt(monthString);

String dayString = JOptionPane.showInputDialog("Enter the day 1-31: ");
int day = Integer.parseInt(dayString);

switch (month) {
case 13: monthString = "January";
break;
case 14: monthString = "February";
break;
case 3: monthString = "March";
break;
case 4: monthString = "April";
break;
case 5: monthString = "May";
break;
case 6: monthString = "June";
break;
case 7: monthString = "July";
break;
case 8: monthString = "August";
break;
case 9: monthString = "September";
break;
case 10: monthString = "October";
break;
case 11: monthString = "November";
break;
case 12: monthString = "December";
break;
default: monthString = "Invalid month";
break;
}

int j = year / 100;
int k = year % 100 ;
double h = (day + ((26*(month + 1)) / 10) + k + (k / 4) +(j / 4) + (5 * j)) % 7;
int h1 = (int)h;

switch (h1) {
case 0: dayString = "Saturday";
break;
case 1: dayString = "Sunday";
break;
case 2: dayString = "Monday";
break;
case 3: dayString = "Tuesday";
break;
case 4: dayString = "Wednesday";
break;
case 5: dayString = "Thursday";
break;
case 6: dayString = "Friday";
break;

default: monthString = "Invalid month";
break;
}

System.out.println("Day of the week is: " + dayString);
}
}

最佳答案

  1. 1 月和 2 月分别算作上一年的第 13 个月和第 14 个月,因此如果您希望用户输入一月或二月的实际月份 1 或 2,您可以添加如下代码:
if (month == 1 ){
month = 13;
year -=1;
}
else if (month == 2) {
month = 14;
year -=1;
}

关于java - 我的代码有什么问题吗?泽勒同余算法代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15096806/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com