if(Integer.parseInt(year.getSelectedItem()%4==0) if(Integer.parseInt(year.getSelectedItem())%4==0)少了一个括号,把一个字符串跟4进行了运算,所以报错。

解决方案 »

  1.   

    还有问题:以下代码为什么不能实现判断2月份是29天还是28天?
    谢谢import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import javax.swing.event.*;
    import java.applet.*;public class a extends Applet 
    implements ActionListener,ItemListener
    {
    Panel p1,p2,p3,p4,p5,p6;
    Label l1,l2,l3,l4,l5,l6,l7,l8,l9,l10,l11,l12,l13,l14,l15,l16,l17,l18,l19,l20,l21,l22;;
    Choice c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12;
    TextField t1,t2,t3;
    TextArea ta1;
    Button b1;

    public void init()
    {
    setLayout(new GridLayout(6,1));
    p1=new Panel();
    p2=new Panel();
    p3=new Panel();
    p4=new Panel();
    p5=new Panel();
    p6=new Panel();

    //电费
    l1=new Label("电费每月交:");
    t1=new TextField("1",2);
    l2=new Label("元钱");
    l3=new Label("从");
    c1=new Choice();
    l4=new Label("年");
    c2=new Choice();
    l5=new Label("月");
    c3=new Choice();
    l6=new Label("日算起");





    //水费
    l7=new Label("水费每月交:");
    l8=new Label("元钱");
    l9=new Label("从");
    l10=new Label("年");
    l11=new Label("月");
    l12=new Label("日算起");
    t2=new TextField("1",2);
    c4=new Choice();
    c5=new Choice();
    c6=new Choice();

    //房费
    l13=new Label("房租每月交:");
    l14=new Label("元钱");
    l15=new Label("从");
    l16=new Label("年");
    l17=new Label("月");
    l18=new Label("日算起");
    t3=new TextField("16.67",3);
    c7=new Choice();
    c8=new Choice();
    c9=new Choice();

    //计算
    l19=new Label("到");
    l20=new Label("年");
    l21=new Label("月");
    l22=new Label("日");
    c10=new Choice();
    c11=new Choice();
    c12=new Choice();

    b1=new Button("查询"); ta1=new TextArea("",3,0);

    p1.add(l1);
    p1.add(t1);
    p1.add(l2);
    p1.add(l3);
    p1.add(c1);
    p1.add(l4);
    p1.add(c2);
    p1.add(l5);
    p1.add(c3);
    p1.add(l6); p2.add(l7);
    p2.add(t2);
    p2.add(l8);
    p2.add(l9);
    p2.add(c4);
    p2.add(l10);
    p2.add(c5);
    p2.add(l11);
    p2.add(c6);
    p2.add(l12);


    p3.add(l13);
    p3.add(t3);
    p3.add(l14);
    p3.add(l15);
    p3.add(c7);
    p3.add(l16);
    p3.add(c8);
    p3.add(l17);
    p3.add(c9);
    p3.add(l18);


    p4.add(l19);
    p4.add(c10);
    p4.add(l20);
    p4.add(c11);
    p4.add(l21);
    p4.add(c12);
    p4.add(l22);


    p5.add(b1);
    p6.add(ta1);

    add(p1);
    add(p2);
    add(p3);
    add(p4);
    add(p5);
    add(p6);

    //Choice加入年、月、日
    addChoice(c1,c2,c3);
    addChoice(c4,c5,c6);
    addChoice(c7,c8,c9);
    addChoice(c10,c11,c12);


    b1.addActionListener(this);

    c1.addItemListener(this);
    c2.addItemListener(this);
    c3.addItemListener(this);
    c4.addItemListener(this);
    c5.addItemListener(this);
    c6.addItemListener(this);
    c7.addItemListener(this);
    c8.addItemListener(this);
    c9.addItemListener(this);
    c10.addItemListener(this);
    c11.addItemListener(this);
    c12.addItemListener(this);


    } //Choice加入年、月、日
    void addChoice(Choice year,Choice month,Choice day)
    {
    for(int i=2000;i<=2010;i++)
    year.addItem(""+i);
    for(int i=1;i<=12;i++)
    month.addItem(""+i);
    for(int i=1;i<=31;i++)
    day.addItem(""+i);
    }

    public void actionPerformed(ActionEvent ae)
    { }

    public void itemStateChanged(ItemEvent ie)
    {
    judgemoth(c1,c2,c3);
    judgemoth(c4,c5,c6);
    judgemoth(c7,c8,c9);
    judgemoth(c10,c11,c12);

    repaint();
    }

    //判断
    void judgemoth(Choice year0,Choice month0,Choice day0)
    {
    Choice year;
    Choice month;
    Choice day;
    year=year0;
    month=month0;
    day=day0;


    if((Integer.parseInt(month.getSelectedItem()))==2)
    { //闰年2月29天
    if((Integer.parseInt(year.getSelectedItem()))%4==0
    && (Integer.parseInt(year.getSelectedItem()))%100!=0 
    || (Integer.parseInt(year.getSelectedItem()))%400==0)

    {
    if(day.getItemCount()==31)
    {
    day.remove("31");
    day.remove("30");
    }
    if(day.getItemCount()==30)
    day.remove("30");
    if(day.getItemCount()==28)
    day.addItem("29");
    }
    else //平年28天
    {
    if(day.getItemCount()==29)
    day.remove("29");
    if(day.getItemCount()==30)
    {
    day.remove("30");
    day.remove("29");
    }
    if(day.getItemCount()==31)
    {
    day.remove("31");
    day.remove("30");
    day.remove("29");
    }
    }
    }
    }
    }