初级Java - Zellers algotrithm

时间:2017-03-19 18:05:26

标签: java loops while-loop counter

我有基于Zellers算法的Java编程(编程介绍),将输入写为整数,循环和计数器代码,我必须使用while循环来获取日期和年份,然后处理Jan和amp ; 2月为前一年。我以为我至少在球场,但无法让我的代码工作。关于如何改进的任何建议?

import java.util.Scanner;
import java.text.NumberFormat;

public class ZellersAlgorithm {

public static void main(String[] args) {
    //declarations go here

int count, Month, Day, Year;
int C = (int)(Year / 100.0); // the century 
int D = (int) (Year % 100); // The year of the century
int K = Day;
int M = Month;
int G = (K + (int)((26 * (M + 1)) / 10.0) + C + (int)(C / 4.0) 
        + (int)(D / 4.0) + (5 * D)) % 7;

Month = (M);
Day = (K);
Year = (C + D);


Scanner scan = new Scanner(System.in);

//Title
System.out.println("Project 2 - Zellers Algorithm");


while(M != 0){


    System.out.print("Enter a numberic day: ");
    K = scan.nextInt();
        if (K == 0) 
            System.out.print("Saturday");
        else if (K == 1)
            System.out.print("Sunday");
        else if (K == 2)
            System.out.print("Monday");
        else if (K == 3)
            System.out.print("Tuesday");
        else if (K == 4)
            System.out.print("Wednesday");
        else if (K == 5)
            System.out.print("Thursday");
        else
            System.out.print("Friday");

    System.out.print("Enter a nummeric year: ");
    ///Need to convert 4 digit year to C & D variable
    // Add counter, possibly include length counter 1st 2 digits are century/C last 2 are year
    Year = scan.nextInt();

    System.out.print("Enter a numeric month: ");
    M = scan.nextInt();

    switch (M) {
    case 1:
        System.out.println("March");
        break;
    case 2:
        System.out.println("April");
        break;
    case 3:
        System.out.println("May");
        break;
    case 4:
        System.out.println("June");
        break;
    case 5:
        System.out.println("July");
        break;
    case 6:
        System.out.println("August");
        break;
    case 7:
        System.out.println("September");
        break;
    case 8:
        System.out.println("October");
        break;
    case 9:
        System.out.println("November");
        break;
    case 10:
        System.out.println("December");
        break;
    case 11:
        System.out.println("January" + D + count--);
        break;
    case 12:
        System.out.println("February" + D + count--);
        break;
    default:
        System.out.println("Invalid month.");
        break;

 }

}

//formula one
    //G =([2.6M-.2]+K+D+[D/4]+[C/4]-2C mod 7;
    //display as integer
String result = "Day of the week is ";
    System.out.println();

1 个答案:

答案 0 :(得分:0)

请澄清但无法让我的代码工作意味着。

虽然有些评论

  • int count, Month, Day, Year; - 变量应该是小写的(这不会改变程序的行为,但是Java程序的常见代码样式 - 大多数人会将Year读作类名)。同样适用于"全部大写"像C,通常用于常量
  • 使用表达含义的变量名称。 MKCD无法帮助读者理解他们的意思。