2011-07-27 9 views
0

私はZellerの合同と呼ばれる曜日を計算するためのアルゴリズムが与えられている教科書からこのプログラミング演習を行っています。まあ、教科書のサンプルと同じ出力を得ることができると思いますか? 2002年、3月、26日になります。サンプルは火曜日に読み返しています。私はいくつかの改造と書き換えを行い、火曜日の近くにはどこにも行けません!曜日、java、およびZellerの合同!

それは誰のものであれば包括的な教科書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

私をビートしてください!また、私はwikiページの数学の構文を理解していませんが、整数の分割で十分ですか? – Bringer128

+0

私はnotiationは単なる除算だと思うが、絶対値がそれぞれの周りにラップされているので、かなり "整数除算"だ。 –

+1

部門の周りの線はfloor関数です(常に丸めます)。 Linが言ったように、それは整数除算です。頂点(bottonではなく)のコーナーが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

Gauravありがとうございました。乾杯。 –

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++のコードです(PHPの曜日の機能に対して、0年から1年に渡って毎日テストされています)。すべての変数はint型である:

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

お知らせ - 機能の終わり近くに「1」 - これは、それが使用する値を容易にするために7スルー6ではなく1を通して0から値を返すようになりたとえば、曜日名の文字列配列へのインデックスとして使用できます。

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

    } 
} 
関連する問題