zoukankan      html  css  js  c++  java
  • 【Java】实现一个根据日期判断星座程序的编写

    思路

    直接根据月份做索引,然后根据日期边界判断是本月的星座还是上月的。

    算法

        private static String getAstro(int month, int day) {
        	String[] starArr = {"魔羯座","水瓶座", "双鱼座", "牡羊座", 
        		"金牛座", "双子座", "巨蟹座", "狮子座", "处女座", "天秤座", "天蝎座", "射手座" };   
        	int[] DayArr = {22, 20, 19, 21, 21, 21, 22, 23, 23, 23, 23, 22};  // 两个星座分割日
            int index = month;
            // 所查询日期在分割日之前,索引-1,否则不变
            if (day < DayArr[month - 1]) {
                    index = index - 1;
            }
            // 返回索引指向的星座string
            return starArr[index];
        }

    最终的效果图

    初始图:

    选择图:

    判断星座


    Java代码

    package com.test0803;
    
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.*;
    /*
     *  
     * */
    class MyFrameee extends JFrame implements ItemListener {
    
    	private JComboBox day = new JComboBox(), month = new JComboBox();
    	private JLabel tip = new JLabel("请选择日期");
    	public static final String[] starArr = {"魔羯座","水瓶座", "双鱼座", "牡羊座", 
    		"金牛座", "双子座", "巨蟹座", "狮子座", "处女座", "天秤座", "天蝎座", "射手座" };   
    	public static final int[] DayArr = {22, 20, 19, 21, 21, 21, 22, 23, 23, 23, 23, 22};   
    
    	MyFrameee() {
    		super("星座计算");
    		for (int i = 1; i <= 12; i++) {
    			//void addItem(Object anObject)  为项列表添加项。
    			month.addItem(String.valueOf(i));
    		}
    		for (int i = 1; i <= 31; i++) {
    			day.addItem(String.valueOf(i));
    		}
    		//setForeground:设置此组件的前景色。
    		tip.setForeground(Color.blue);
    
    		Container con = getContentPane();
    		con.setLayout(new FlowLayout());
    		con.add(month);
    		con.add(new JLabel("月-"));
    		con.add(day);
    		con.add(new JLabel("日"));
    		con.add(tip);
    		day.addItemListener(this);
    		month.addItemListener(this);
    		setSize(280, 100);
    		setLocation(400, 300);
    		setVisible(true);
    		setResizable(false);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	}
    
    	public static String getConstellation(Calendar time) {   
    	    int month = time.get(Calendar.MONTH);   
    	    int day = time.get(Calendar.DAY_OF_MONTH);   
    	    if (day < DayArr[month]) {   
    	        month = month - 1;   
    	    }   
    	    if (month >= 0) {   
    	        return starArr[month];   
    	    }   
    	    return starArr[0];   
    	}  
    
    	public void itemStateChanged(ItemEvent e) {
    		Calendar date = Calendar.getInstance();
    		date.set(2010, Integer.parseInt((String) month.getSelectedItem()),
    				Integer.parseInt((String) day.getSelectedItem()));
    		tip.setText(getConstellation(date));
    	}
    }
    
    public class Test_Star2 {
    	public static void main(String[] args) {
    		new MyFrameee();
    	}
    }

    不足之处

    为了方便,没有确定特殊月份的日期数量,都是按照31天处理的。
  • 相关阅读:
    python错误信息 object is not subscriptable 的原因
    python join函数
    string的部分总结
    第k个非立方数(忘记哪里的题了)
    pandas 学习
    数学建模中 时间序列典型分解模型 matlab实现
    matlab三维画图学习 三次插值
    原型
    JavaScript
    js数组去重(多种写法)
  • 原文地址:https://www.cnblogs.com/jiangu66/p/3241102.html
Copyright © 2011-2022 走看看