2011-07-27 4 views
0

Je fais cet exercice de programmation à partir d'un manuel où l'on nous donne un algorithme pour calculer le jour de la semaine appelé la congruence de Zeller. Pensez-vous que je peux obtenir la même sortie que l'échantillon exécuté dans le manuel? Ils vont avec l'année 2002, le mois 3 et le jour du mois 26. L'échantillon se lit mardi. J'ai fait plusieurs heures de mods et de réécritures et je n'arrive pas à me rapprocher de mardi!Jour de la semaine, java, et la congruence de Zeller!

C'est la page 133 de Java manuel complet 8e si quelqu'un l'a ... Je suis un débutant commentaires si constructifs les bienvenus!

Zeller's Congruence

Votre conseil serait apprécié:

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

    } 
} 

Répondre

4

Il ressemble à cette ligne de code est erroné:

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

La formule a le jours ajouté au premier expression, pas le mois. Donc, il devrait ressembler à ceci:

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

Battez-moi! De plus, je ne comprends pas la syntaxe mathématique de la page wiki, mais la division entière suffit-elle? – Bringer128

+0

Je pense que la notation est juste une division, mais il y a des valeurs absolues enroulées autour de chacune d'elles, donc c'est à peu près "division entière". –

+1

Les lignes autour des divisions sont fonction du sol (arrondir toujours). Comme l'a dit @Jon Lin, c'est une division entière. Si les coins étaient en haut (pas le botton) ce serait fonction de plafond (arrondir toujours). – helios

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

est faux. Notez que vous n'utilisez pas les jours dans votre implémentation actuelle.

+0

Merci amal. Je me sens stupide à ce sujet alors si j'avais un problème majeur de logique! –

+0

Si vous aviez utilisé un IDE, cela aurait été vraiment évident :). – amal

1

Cela peut fonctionner je n'ai aucune idée de ce livre u peut essayer ce code

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

Je suis familier avec le livre que vous m'avez dit .. mais vérifiez ce code cela peut vous aider –

+0

Merci Gaurav, je n'ai pas vu cette façon de prendre la date tout en un, alors j'ai appris quelque chose de nouveau. À votre santé. –

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

de congruence Zeller exige ENTIER mathématiques afin de fonctionner correctement. Voici le code en C/C++ (qui a été testé par rapport à la fonction PHP du jour de la semaine pour CHAQUE jour de l'année 0 à l'année 10 000). Toutes les variables sont de type int:

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

Notez que le « - 1 » à la fin de la fonction - ce qui provoque à retourner une valeur 0-6 plutôt que de 1 à 7 pour rendre la valeur plus facile à utiliser , par exemple, en tant qu'index dans un tableau de chaînes de noms de jour.

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

    } 
} 
Questions connexes