星期几,java和Zeller的一致!

时间:2011-07-27 06:05:15

标签: java

我正在从一本教科书中进行这项编程练习,在那里我们得到一个算法,用于计算一周中称为Zeller同余的星期几。那么你认为我可以获得与教科书中的样本相同的输出!它们与2002年,第3个月和第26个月的日期相符。样本在周二回读。我已经制作了几个小时的mod和重写,并且在星期二附近无法到达!

这是Java综合教科书8e的第133页,如果有人有...我是初学者如此建设性的反馈,非常欢迎!

Zeller's Congruence

您的建议将不胜感激:

import java.util.Scanner;

public class DayOfTheWeek {

   public static void main(String[] args) {

   // Set up the scanner...
   Scanner input = new Scanner(System.in);

   // Set up the day's of the week with 0 being Sat as per algorithm.           
   final String[] DAY_OF_WEEK = {"Saturday", "Sunday", "Monday", 
       "Tuesday", "Wednesday", "Thursday", "Friday"};

   // Get the year       
   System.out.print("Enter the year (e.g., 2002): ");             
   int year = input.nextInt();

   // Get the month
   System.out.print("Enter the month 1-12: ");  
   int month = input.nextInt();

   // Get the day
   System.out.print("Enter the day 1-31: ");  
   int day = input.nextInt();

   // According to the algorithm Jan is 13 & Feb 14...
   if (month == 1) month = 13;
   else if (month == 2) month = 14;

   // j Is the century.
   int j = year / 100;

   // k Is the year of the century.
   int k = year % 100 ;

   // Calculate
   double h = (month + ((26*(month + 1)) / 10) + k + (k / 4) +
           (j / 4) + (5 * j)) % 7;

   // Cast answer back to integer to get result from array
   int ans = (int)h;

   // Print result
   System.out.println("Day of the week is: " + DAY_OF_WEEK[ans]);

   }
}

6 个答案:

答案 0 :(得分:4)

看起来这行代码是错误的:

double h = (month + ((26*(month + 1)) / 10) + k + (k / 4) +
(j / 4) + (5 * j)) % 7;

公式将添加到第一个表达式,而不是。所以看起来应该是这样的:

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

答案 1 :(得分:2)

Zeller的同余需要INTEGER数学才能正常运行。下面是C / C ++中的代码(针对每周从PHP 0到10,000年的PHP日函数进行测试)。所有变量的类型都是int:

ss

注意" - 1"在函数末尾附近 - 这会导致它返回0到6而不是1到7的值,以使值更容易使用,例如,作为日期名称的字符串数组的索引。

答案 2 :(得分:1)

 double h = (month + ((26*(month + 1)) / 10) + k + (k / 4) +
       (j / 4) + (5 * j)) % 7;

错了。请注意,在当前的实现中不要使用days。

答案 3 :(得分:1)

这可能有用,我对你可能尝试这段代码的那本书一无所知

   import java.util.*;


public class Zeller {
    /**
     *
     * @param args (Not used)
     */
    final static String[] DAYS_OF_WEEK = {
            "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",
            "Friday"
        };

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter the date in dd/mm/yyyy form: ");

        String[] atoms = input.nextLine().split("/");
        int q = Integer.parseInt(atoms[0]);
        int m = Integer.parseInt(atoms[1]);
        int y = Integer.parseInt(atoms[2]);

        if (m < 3) {
            m += 12;
            y -= 1;
        }

        int k = y % 100;
        int j = y / 100;

        int day = ((q + (((m + 1) * 26) / 10) + k + (k / 4) + (j / 4)) +
            (5 * j)) % 7;

        System.out.println("That date was a " + DAYS_OF_WEEK[day] + ".");
    }
}

答案 4 :(得分:1)

//just the modified version of the above code.

import java.util.*;

public class Zeller {

final static String[] DAYS_OF_WEEK = {
        "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",
        "Friday"
    };

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.print("Enter the date in dd/mm/yyyy form: ");

    String[] atoms = input.nextLine().split("/");
    int q = Integer.parseInt(atoms[0]);
    int m = Integer.parseInt(atoms[1]);
    int y = Integer.parseInt(atoms[2]);

    int dd,mm,yy;



    dd = q; mm =m; yy=y;

    if (m < 3) {
        m += 12;
        y -= 1;
    }

    int k = y % 100;
    int j = y / 100;

    int day = ((q + (((m + 1) * 26) / 10) + k + (k / 4) + (j / 4)) +
        (5 * j)) % 7;

    Calendar now = Calendar.getInstance();

    int nd,nm,ny;

    nm =  now.get(Calendar.MONTH) + 1;
    nd = (now.get(Calendar.DATE));
    ny = now.get(Calendar.YEAR);

    if(dd == nd && mm == nm && yy == ny)
        present(day);
    else if(yy<ny)
        past(day);
    else if(yy == ny)
            if(mm == nm)
                if(dd>nd)
                    future(day);
                else
                    past(day);
            else if(mm>nd)
                future(day);
            else 
                past(day);
    else 
        future(day);
}

public static void past(int day)
{
    System.out.println("That date was " + DAYS_OF_WEEK[day] + ".");
}

public static void future(int day)
{
    System.out.println("That date will be " + DAYS_OF_WEEK[day] + ".");
}

public static void present(int day)
{
    System.out.println("Toady is " + DAYS_OF_WEEK[day] + ".");
}
}

答案 5 :(得分:0)

import java.util.Scanner;

public class zelleralgorithm {

    public static void main (String[] args) {

        int month, dayOfMonth, year, cenNumber, yearNumber, weekday, counter = 0;
        String dayname = null;

        Scanner scan = new Scanner(System.in);

        System.out.println("\tZeller's Algorithm");
        System.out.println("**************************************");
        System.out.print("Enter month ( or 0 to exit):\t");
        month = scan.nextInt();


        //handling exception
        while(month > 12){
            System.out.println("Please enter a valid month!\n");
            System.out.print("Enter month ( or 0 to exit):\t");
            month = scan.nextInt();
        }


        while(month != 0) {
            System.out.print("Enter day:\t\t\t");
            dayOfMonth = scan.nextInt();


            //handling exception
            while(dayOfMonth > 32){
                System.out.println("Please enter a valid date!\n");
                System.out.print("Enter day:\t\t\t");
                dayOfMonth = scan.nextInt();
            }


            System.out.print("Enter year:\t\t\t");
            year = scan.nextInt();

            if(month == 1 || month == 2){
                month = 11;
                --year;
            }
            else{
                month = month -2;
            }

            cenNumber = year / 100;
            yearNumber = year % 100;

            weekday = (( (int) (2.6*month-.2) + dayOfMonth + yearNumber + (yearNumber/4) + (cenNumber/4) - (2*cenNumber)) % 7);

            if(weekday < 0){
                weekday = weekday + 7;
            }

            switch(weekday){
            case 0:
                dayname = "Sunday";
                break;
            case 1:
                dayname = "Monday";
                break;
            case 2:
                dayname = "Tuesday";
                break;
            case 3:
                dayname = "Wednesday";
                break;
            case 4:
                dayname = "Thursday";
                break;
            case 5:
                dayname = "Friday";
                break;
            case 6:
                dayname = "Saturday";
                break;
            default:
                dayname = "Exceptional Case error!!";
            }

            System.out.println("\n**************************************");
            System.out.println("\tThe day is "+ dayname);
            System.out.println("**************************************");

            System.out.print("\nEnter month ( or 0 to exit):\t");
            month = scan.nextInt();

            ++counter;
        }
        //while loop end

        //counter
        System.out.println("Number of entries = " + counter);

        scan.close();

    }
}