【问题标题】:Output not printing after switch case utilized Java使用 Java 的 switch case 后输出不打印
【发布时间】: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


【解决方案1】:
  • 如果您想使用DetermineDay 输出日期,您需要在完成计算并将结果赋值给value 后最后调用该方法。
  • 这似乎可行,但在尝试使用日期为 2016 年 4 月 11 日的程序时,您的算法存在问题,它确实发现它是星期五,但是当使用日期 2016 年 5 月 5 日(即今天)时输出是“看起来那一天不存在”,所以是的。
  • DetermineDay 结束时,您不需要返回结果,因为您已经将结果保存在 switch 中。

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

【讨论】:

    猜你喜欢
    • 2020-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多