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+"不是闰年";
          }
     
    }
  • 相关阅读:
    13.Convert BST to Greater Tree(将树转为更大树)
    13.调用数组顺序使奇数位于偶数前面
    12.数值的整数次方
    11.二进制中1的个数
    12.Hamming Distance(汉明距离)
    11.Find All Numbers Disappeared in an Array(找出数组中缺失的数)
    10.Find All Anagrams in a String(在一个字符串中发现所有的目标串排列)
    垃圾收集器与内存分配策略---垃圾收集器
    线程之间的协作
    1287. Mars Canals(DP)
  • 原文地址:https://www.cnblogs.com/voidsh/p/4396190.html
Copyright © 2011-2022 走看看