/ /曜日、Java、およびZellerの一致! -Java

曜日、Java、およびZellerの合同! - java

私はこのプログラミング演習をゼラーの合同と呼ばれる曜日を計算するためのアルゴリズムが与えられている教科書。まあ、教科書のサンプルの実行と同じ出力を得ることができると思います! 26.サンプルは火曜日を読み返します。「数時間のmodと書き換えを行ったが、火曜日の近くには何もできません!」

Java Comprehensive textbook 8eの133ページをお持ちの方は...初心者ですので、建設的なフィードバックを歓迎します。

ツェラーの合同

あなたのアドバイスをいただければ幸いです:

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

}
}

回答:

回答№1は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;

回答№2については2

Zeller "s CongruenceではINTEGER数学が必要です正しく機能するために。 C / C ++のコードは次のとおりです(これは、0年目から10,000年目までの毎日のPHP曜日関数に対してテストされました)。すべての変数はint型です。

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

関数の終わり近くの「-1」に注意してください-これにより、たとえば、曜日名の文字列配列へのインデックスとして値を使いやすくするために、1〜7ではなく0〜6の値が返されます。


回答№3の場合は1
 double h = (month + ((26*(month + 1)) / 10) + k + (k / 4) +
(j / 4) + (5 * j)) % 7;

間違っている 。現在の実装では日数を使用しないでください。


回答№4の場合は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] + ".");
}
}

回答№5の場合は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] + ".");
}
}

答え№6の場合は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:ttt");
dayOfMonth = scan.nextInt();


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


System.out.print("Enter year:ttt");
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();

}
}