首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >初学者级Java - Zellers算法

初学者级Java - Zellers算法
EN

Stack Overflow用户
提问于 2017-03-20 02:05:26
回答 1查看 112关注 0票数 0

我有一个基于Zellers算法的Java (编程入门)任务,将输入写成整数,循环和计数器代码,我必须使用while循环来获取日期和年份,然后是月份,将Jan & Feb视为前一年。我认为我至少在大体范围内,但不能让我的代码工作。对如何改进有什么建议吗?

代码语言:javascript
运行
复制
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();
EN

回答 1

Stack Overflow用户

发布于 2017-03-20 05:18:17

请澄清什么是但不能让我的代码工作的意思。

不过,也有一些评论

  • int count, Month, Day, Year; --变量应该是小写的(这不会改变程序的行为,但这是Year程序的常见代码风格--大多数人都会将变量读作一个类名)。这同样适用于“全大写”,如C,它通常用于常量,
  • 使用表达含义的变量名。MKCD并不能帮助读者理解它们的意思。
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42890178

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档