闰年检测问题

关于闰年测试的问题:

条件:1. 4 年一闰, 100 年不闰,400年又闰。

   2. 输入年份大于0。

   3. 输入只能为数字。

源代码:

public class leapyear extends Application {

public static void main(String[] args) {
leapyear.launch( args );
}

private TextField textfield = new TextField();

@Override
public void start(Stage arg0) throws Exception {
arg0.setTitle( "LeapYear" );

HBox hbox = new HBox( 8 );
textfield.setPrefColumnCount( 25 );
hbox.setAlignment( Pos.CENTER_LEFT );
Button btn = new Button();
btn.setText( "确定" );
btn.setOnAction( new Listener() );
hbox.getChildren().addAll( new Label( " 请输入年份: "), textfield, btn );

arg0.setScene( new Scene( hbox, 450, 80 ));
arg0.show();
}

public class Listener implements EventHandler<ActionEvent> {

@Override
public void handle(ActionEvent arg0) {
String str = textfield.getText();
String inf = "";
if( isLeapYear( Integer.parseInt( str ) ) ) {
inf = "闰年";
}
else {
inf = "非闰年";
}
JOptionPane.showMessageDialog( null, inf, "information",
JOptionPane.INFORMATION_MESSAGE );
}
}

private boolean isLeapYear( int year ) {
if( year % 4 != 0 ) {
return false;
}
else if( year % 100 != 0 ) {
return true;
}
else if( year % 400 != 0 ) {
return false;
}
else {
return true;
}
}
}

编号测试用例结果
11995非闰年
22000闰年
32012闰年
40000闰年
51900非闰年

 

 

 

 

 

测试结果:

1.

2.

3.

4.

5.

 

但以上出现了一个问题,当输入为空时,错误。还有当输入飞数字的时候也会显示错误。

 

转载于:https://www.cnblogs.com/HCS1995/p/4398634.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值