【发布时间】:2016-11-05 19:15:11
【问题描述】:
我正在努力使用枚举/switch case 以及 Zeller 的公式来说明特定日期是一年中的哪一天。在我实现代码的枚举/开关部分之前,我的代码正在打印正确的日子(如下)。在我放入 enum/ switch case 后,当我在 DrJava 中运行它时,它会提示日、月和年,但一旦通过 switch case 就不会打印任何内容
import java.util.*;
public class Zeller {
public enum DaysOftheWeek {
SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY;
}
private static int value;
public Zeller (int value){
this.value = value;
}
public int getValue(){
return this.value;
}
public static void main(String[] args) {
DetermineDay(value); // Create a Scanner
Scanner input = new Scanner(System.in);
// Prompt the user to enter a year, month and a day
System.out.print("Enter month: 1-12: ");
int month = input.nextInt();
System.out.print("Enter the day of the month: 1-31: ");
int day = input.nextInt();
System.out.print("Enter year (e.g., 2008): ");
int year = input.nextInt();
// Check if the month is January or February
// If the month is January and February, convert to 13, and 14,
// and year has to -1. (Go to previous year).
if (month == 1 || month == 2) {
month += 12;
year--;
}
// Compute the answer
int k = year % 100; // The year of the century
int j = (int)(year / 100.0); // the century
int q = day;
int m = month;
int h = (q + (int)((13 * (m + 1)) / 5.0) + k + (int)(k / 4.0)
+ (int)(j / 4.0) + (5 * j)) % 7;
value = h;
System.out.println(value);
}
public static String DetermineDay(int value){
String result = "Day of the week is ";
switch (value){
case 1 :
System.out.println(result + "Sunday");
break;
case 2 :
System.out.println(result + "Monday");
break;
case 3:
System.out.println(result + "Tuesday");
break;
case 4:
System.out.println(result + "Wednesday");
break;
case 5:
System.out.println(result + "Thursday");
break;
case 6:
System.out.println(result + "Friday");
break;
case 7 :
System.out.println( result + "Saturday");
break;
default :
System.out.println ("Looks like that day doesn't exist");
break;
}
return result;
}
}
【问题讨论】:
-
你在哪里打电话
DetermineDay? -
由于你没有分享你如何打电话给
DetermineDay,所以我们无法知道那个电话之后会发生什么。 -
很确定你忘记调用你的方法了。您可能还想为您的方法提供
value的参数。 -
哦,我没有意识到我必须调用它。我刚刚在我的代码中更新了它以在 main 方法中调用它,我重新运行它,但我仍然没有得到当天的打印输出
-
@JohnG 我刚刚测试了使其等于 h 后的值是多少,它是正确的值。我还在每个 switch 案例之后插入了 break 语句,但我仍然没有得到对应日期的打印字符串值
标签: java enums switch-statement