Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
初级Java-Zellers算法_Java_Loops_While Loop_Counter - Fatal编程技术网

初级Java-Zellers算法

初级Java-Zellers算法,java,loops,while-loop,counter,Java,Loops,While Loop,Counter,我有一个基于Zellers算法的Java作业(编程入门),以整数、循环和计数器代码的形式编写输入,我必须先使用while循环获取日期和年份,然后是月份,将一月和二月视为前一年。我以为我至少在大概范围内,但无法让我的代码工作。有什么改进的建议吗 import java.util.Scanner; import java.text.NumberFormat; public class ZellersAlgorithm { public static void main(String[] args

我有一个基于Zellers算法的Java作业(编程入门),以整数、循环和计数器代码的形式编写输入,我必须先使用while循环获取日期和年份,然后是月份,将一月和二月视为前一年。我以为我至少在大概范围内,但无法让我的代码工作。有什么改进的建议吗

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();
请澄清“但无法使我的代码正常工作”的含义

不过有一些评论

  • int计数、月、日、年--变量应为小写(这不会改变程序的行为,但却是Java程序的常见代码样式-大多数人会将
    Year
    读作类名)。这同样适用于像
    C
    这样的“全大写”,它通常用于常量
  • 使用表示含义的变量名
    M
    K
    C
    D
    不能帮助读者理解它们的意思