软件测试——闰年测试

  只要是对于需要记录年份的程序,闰年检测都是一个很重要的环节。公历的闰年规则如下:

    1、普通年能被4整除且不能被100整除的为闰年。(如2004年就是闰年,1900年不是闰年)

    2、世纪年能被400整除的是闰年。(如2000年是闰年,1900年不是闰年)
  然而对于不正确的输入,程序必须对持做出反应,不能使异常情况阻碍程序的运行,下面是闰年检测的程序试例:
  
输入年份结果
2000闰年
1900非闰年
-1600非法输入
1864闰年
saddsa非法输入

  

代码实现:

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.scene.text.Text;
import javafx.stage.Stage;


public class test2 extends Application {

    public static void main(String[ ] args) {
        test2.launch( args );
    }
    public void start( Stage primaryStage ) {
        primaryStage.setTitle( "input testing" );
        AnchorPane root = new AnchorPane();
        
        Text text = new Text("请输入测试年份");
        final Text textans1 = new Text("");
        
        final TextField intext1 = new TextField("");
        intext1.setMaxSize(140, 20);    
        AnchorPane.setTopAnchor(text, 40.0);
        AnchorPane.setLeftAnchor(text, 100.0);       
        
        AnchorPane.setTopAnchor(textans1, 120.0);
        AnchorPane.setLeftAnchor(textans1, 70.0);
        
        AnchorPane.setTopAnchor(intext1, 60.0);
        AnchorPane.setLeftAnchor(intext1, 70.0);        
        
        root.getChildren().addAll(text,intext1);  
        root.getChildren().addAll(textans1);
        
        Button btn = new Button("提交");        
        root.getChildren().addAll(btn);
        AnchorPane.setTopAnchor(btn, 90.0);
        AnchorPane.setLeftAnchor(btn, 120.0);
       
        btn.setOnAction(new EventHandler<ActionEvent>() {
            public void handle(ActionEvent arg0) {
                textans1.setText(check(intext1.getText().toString()));     
            }
        });
      
        primaryStage.setScene(new Scene(root,300,200));
        primaryStage.show( );
        }
        
    public String check(String str){
        try{
            int year = Integer.parseInt(str);
            if(year <0)
                return "请输入正确年份"; 
            if(year % 100 ==0){
                if(year % 400 == 0)
                    return str+"是闰年"; 
            }
            else{
                if(year % 4 == 0)
                    return str+"是闰年";
            }    
        }catch(Exception e){
            return "非法输入";
        }

            return str+"不是闰年";
      }
 
}

 

 

 

转载于:https://www.cnblogs.com/voidsh/p/4396190.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值