2011-07-27 70 views
0

我正在從一本教科書中進行編程練習,在這本教科書中我們給出了一個計算一週稱爲Zeller一致性的算法。那麼你認爲我可以得到與教科書中樣本運行相同的輸出!他們在2002年,第3個月和第26個月的日子。這個樣本回讀星期二。我已經做了幾個小時的MOD和重寫,並且無法在星期二附近獲得!星期幾,java和澤勒的一致!

它是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]); 

    } 
} 

回答

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; 
+0

打我吧!另外,我不明白維基頁面上的數學語法,但是整數除法足夠嗎? – Bringer128

+0

我認爲這個符號只是除法,但是每個符號都有絕對值,所以它幾乎是「整數除法」。 –

+1

圍繞分部的線條是落地功能(總是往下舍入)。正如@Jon Lin所說,它是整數部門。如果頂部(而不是底部)的角落是ceil函數(總是舍入)。 – helios

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

是錯誤的。請注意,您不會在當前實施中使用天數。

+0

謝謝阿瑪。如果我有一些重大的邏輯問題,我覺得這很愚蠢! –

+0

如果你使用過一個IDE,它會非常明顯:)。 – amal

1

這工作我沒有關於這本書的任何想法U可以試試這個代碼

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] + "."); 
    } 
} 
+0

我對這本書很熟悉,你告訴我..但檢查這個代碼,這可能會幫助你 –

+0

謝謝高拉夫,我還沒有看到過這種方式,所以我學到了一些新的東西。乾杯。 –

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] + "."); 
} 
} 
2

蔡勒公式需要整數運算才能正常工作。這裏是C/C++中的代碼(從0年到10000年的每一天,它都針對PHP星期幾函數進行了測試)。所有變量都是int類型:

day_of_week = (((day + (((month + 1) * 26)/10) + year + (year/4) + (6 * (year/100)) + (year/400)) - 1) % 7); 

注意「 - 1」附近函數的末尾 - 這導致其從0返回一個值通6,而不是1到7,使值更容易使用例如,作爲日期名稱的字符串數組的索引。

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(); 

    } 
}