java求閏年
發布時間: 2023-03-30 17:23:28
Ⅰ 用java程序判斷是否是閏年
以下為代碼:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class LeapyearTest extends Applet implements ActionListener{
Label lblResult;
Button btn;
TextField txt;
int year;
boolean leap;
public void init() {
lblResult=new Label("請輸入要判斷的年份");
txt=new TextField(5);
btn=new Button("判斷");
add(lblResult);
add(txt);
add(btn);
btn.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
year=Integer.parseInt(txt.getText());
if(year%4==0&;&;(year%100)!=0)
{leap=true;
}
else if(year%400==0){
leap=false;
}
if(leap==true)
lblResult.setText(year+"年是閏年");
else
lblResult.setText(year+"年是平年");
txt.setText("");
}
}
熱點內容