⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 zodiacsigns.java

📁 JAVA 2入门经典 练习答案
💻 JAVA
字号:
// Chapter 14 Exercise 3
// Determines the sign of the Zodiac for a given date.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.GregorianCalendar;
import java.text.DateFormat;

public class ZodiacSigns {
  public static void main(String[] args) {
    System.out.println(df.format(calendar.getTime()));
    int day = 0;
    int month = 0;
    int year = 0;
    while(true) {
      // Read in a date
       System.out.println("Enter the year: ");
      year = readInt();
      while(true){
        System.out.println("Enter the month number January is 1, December is 12: ");
        month = readInt();
        if(validMonth(--month))              // Change month to zero-based and validate
          break;
      }
      while(true) {
      System.out.println("Enter the day in the month: ");
      day = readInt();
        if(validDay(day, month, year))       // Verify date is valid for month and year
          break;
      }
      
      
      // determine the sign
      calendar.set(year, month,day);                     // Set the calendar to the date entered
      // Match the year for the sign start dates
      for(int i = 0 ; i < signStartDates.length ; i++)
        signStartDates[i].set(GregorianCalendar.YEAR, year);
        
      for(int i = 0 ; i < signStartDates.length ; i++) 
        if(calendar.after(signStartDates[i]) && 
                        calendar.before(signStartDates[(i+1)%signStartDates.length])) {
          System.out.println(df.format(calendar.getTime())+ " is in the sign of "+signs[i]);
          break;
        }
      
      // Try another date?
      System.out.println("Do you want to try another date(Enter Y or N)?");
      if(!yes())
      break;
    }
  }

  // Validate the month value
  private static boolean validMonth(int month) {
    if(month >= 0 && month <= 11)
      return true;
    else
      System.out.println("The month value must be from 1 to 12. Try again.");
    return false;
  }

  // Validate the day value for the month and year
  private static boolean validDay(int day, int month, int year) {
    /* A valid day must be:
                 - between 1 and 31
                 - less than 31 when the month is April, June, September, or November
                 - less than 29 when the month is February and it is not a leap year
                 - less than 30 when the month is February and it is a leap year
    */
    if (day < 0 || day > 31) {
      System.out.println("Day values must be between 1 and 31. Try again.");
      return false;
    } 
 
    if(day >30 && (month == 3 || month == 5 || month == 8 || month == 10)) {
      System.out.println("Day values must be less than 31 when the month"
      +" is "+MONTH_NAMES[month]+". Try again.");
      return false;      
    } 

    if(day > 28 && month == 1 && !calendar.isLeapYear(year)) {
      System.out.println(year + " is not a leap year so day values must be less than 29. Try again.");
      return false;
    }
    if(day > 29 && month == 1 && calendar.isLeapYear(year)) {
      return false;
    }
    
    return true;
      
  }
    
  // Reads an integer from the keyboard
  private static int readInt() {
    int value = 0;
    while(true) {
      try {      
        value = Integer.parseInt(in.readLine().trim());
        return value;
      }catch(NumberFormatException e) {
        System.out.println("Invalid input. Try again.");
      } catch(IOException e){
          System.out.println("Error reading for the keyboard." + e.getMessage());
      }
    }
  }

  private static boolean yes() {
    String str = null;
    while(true) {
      try {
        str = in.readLine().trim();
      } catch(IOException e) {
          System.out.println("Error reading for the keyboard." + e.getMessage());
      }

      if(str.equalsIgnoreCase("Y"))
        return true;
      else if(str.equalsIgnoreCase("N"))
        break;
      else
        System.out.print("Invalid input. Try again. Enter Y or N: ");
    }
    return false;
  }

  private static BufferedReader in = new BufferedReader(
                        new InputStreamReader(System.in));      // Keyboard input

  // Names for Zodiac signs and start dates. Remember - months start at zero.
  private static String[] signs = {"Aquarius", "Pisces", "Aries"      , "Taurus",
                                   "Gemini"  , "Cancer", "Leo"        , "Virgo",
                                   "Libra"   ,"Scorpio", "Sagittarius", "Capricorn" };
  private static GregorianCalendar[] signStartDates = {
           new GregorianCalendar(2002,  0, 20),    // Aquarius start date
           new GregorianCalendar(2002,  1, 19),    // Pisces start date
           new GregorianCalendar(2002,  2, 21),    // Aries start date
           new GregorianCalendar(2002,  3, 20),    // Taurus start date
           new GregorianCalendar(2002,  4, 21),    // Gemini start date
           new GregorianCalendar(2002,  5, 21),    // Cancer start date
           new GregorianCalendar(2002,  6, 23),    // Leo start date
           new GregorianCalendar(2002,  7, 23),    // Virgo start date
           new GregorianCalendar(2002,  8, 23),    // Libra start date
           new GregorianCalendar(2002,  9, 23),    // Scorpio start date
           new GregorianCalendar(2002, 10, 22),    // Sagittarius start date
           new GregorianCalendar(2002, 11, 22),    // Capricorn start date
    };
  private static GregorianCalendar calendar = new GregorianCalendar();
  
  // Array defining the names of the months.
  // Note that DateFormatSymbols class defines an instance method, getMonths(),
  // that will return exactly this array of String objects.
  private static final String[] MONTH_NAMES = {
       "January"  , "February", "March"   , "April",
       "May"      , "June"    , "July"    , "August", 
       "September", "October" , "November", "December" };

  // Date formatter for displaying dates
  private static DateFormat df = DateFormat.getDateInstance(DateFormat.LONG);

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -