zoukankan      html  css  js  c++  java
  • 软件测试——闰年测试

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

        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+"不是闰年";
          }
     
    }
  • 相关阅读:
    firefox上网问题解决
    ubuntu内核的编译安装
    ubuntu常用命令
    source insight 添加文件类型
    ubuntu版本查看命令
    百年孤独与拉丁美洲历史--转载
    Guess Number Higher or Lower II--困惑
    Symmetric Tree
    一棵开花的树
    yii2 mysql数据库读写分离配置
  • 原文地址:https://www.cnblogs.com/voidsh/p/4396190.html
Copyright © 2011-2022 走看看