软件测试——闰年测试

一、程序要求 

  闰年的判定方法:

  1、普通年能被4整除且不能被100整除的为闰年。(如2004年就是闰年,1900年不是闰年)
  2、世纪年能被400整除的是闰年。(如2000年是闰年,1900年不是闰年)
  正常情况下在文本框内输入一个年份,点击OK按钮,程序会判定出该年份是否问闰年,但是如果输入的是一个字符串或者是一个负数,程序就会产生异常,而本篇博客就是为了解决这一问题。
 
二、测试分析
编号 输入 输出
1 2000 2000 is a leap year.
2 1900 1900 is not a leap year.
3 sdafsd Input Invalid.
4 -100 Please input a positive number.
三、代码实现
 
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 calculate extends Application{
    
    
    public static void main(String arg0[]){
        calculate.launch(arg0);
    }

    public void start(Stage stage) throws Exception {
        stage.setTitle("Calculate Year Program");
        AnchorPane root = new AnchorPane();
        
        Text text = new Text();
        text.setText("Please input the number of the year: ");
        root.getChildren().add(text);
        AnchorPane.setTopAnchor(text, 30.0);
        AnchorPane.setLeftAnchor(text, 90.0);
        
        final TextField input = new TextField();
        root.getChildren().add(input);
        AnchorPane.setTopAnchor(input, 50.0);
        AnchorPane.setLeftAnchor(input, 90.0);
        
        Button submit = new Button();
        submit.setText("submit");
        root.getChildren().add(submit);
        AnchorPane.setTopAnchor(submit, 50.0);
        AnchorPane.setLeftAnchor(submit, 240.0);
        
        submit.setOnAction(new EventHandler<ActionEvent>() {
            public void handle(ActionEvent arg0) {
                final int year = Integer.parseInt(input.getText().toString());
                
                Stage newStage = new Stage();
                AnchorPane newRoot = new AnchorPane();
                Text result = new Text();                
                
                try{
                    if((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)){
                        result.setText(year + " is a leap year.");
                    }
                    else{
                        result.setText(year + " is not a leap year.");
                    }
                } catch(Exception e){
                    result.setText("Input Invalid");
                }
                
                if(year < 0){
                    result.setText("Please input a positive number.");
                }
                
                newRoot.getChildren().add(result);
                AnchorPane.setTopAnchor(result, 20.0);
                AnchorPane.setLeftAnchor(result, 30.0);
                 
                newStage.setScene(new Scene(newRoot, 250, 50));
                newStage.show();
            }
        });
        
        stage.setScene(new Scene(root, 400, 100));
        stage.show();
    }
}

四、程序结果

posted on 2015-04-06 11:29  四班&吴迪  阅读(410)  评论(0编辑  收藏  举报