开关盒使用Java后输出不打印

时间:2016-11-05 19:15:12

标签: java enums switch-statement

我正在使用enum / switch case以及Zeller的公式来说明具体日期的哪一天。我的代码是在我实现代码的enum / switch部分之前的正确日子打印的(下面)。在我输入enum / switch的情况下,当我在DrJava中运行它时,它会提示当天,月份和年份,但是一旦它通过开关盒就没有打印

 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;     
  }
}

1 个答案:

答案 0 :(得分:0)

  • 如果您想使用DetermineDay输出当天,则需要在完成计算后最后调用该方法,并将结果分配给值。
  • 这似乎有效但你的算法在使用日期4/11/2016尝试这个程序时有问题它确实发现它是星期五,但是当使用日期5/5/2016这是今天的输出是 - 看起来像那天不存在,所以是的,那就是。
  • 在DetermineDay结束时,你不需要返回一个结果,因为你已经在开关内输入了结果。

    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) {
      // 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);
      DetermineDay(value);
     }
    
     public static void 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;
      }
     }
    }