首页 > 编程语言 > 详细

通过JAVA程序测试闰年

时间:2015-04-07 23:07:55      阅读:278      评论:0      收藏:0      [点我收藏+]

  首先简要介绍一下公历上规定的闰年:四年一闰,百年不闰,四百年再闰。

  针对这一规则,简要的设计部分测试用例:

    技术分享

  附(测试截图):

技术分享

技术分享

技术分享

以下为该程序代码段:

 

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.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;


public class Main extends Application{
TextField textField;
Label label;

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

public void start(Stage stage){
stage.setTitle("For Test");
AnchorPane root = new AnchorPane();
Scene scene = new Scene(root, 500, 300);

Text text = new Text();
text.setText("Enter a particular year for test:");
text.setFont(Font.font("Comic Sans MS", 18));
AnchorPane.setTopAnchor(text, 0.0);
AnchorPane.setLeftAnchor(text, 10.0);

Text text2 = new Text();
text2.setText("Result:");
text2.setFont(Font.font("Comic Sans MS", 18));
AnchorPane.setTopAnchor(text2, 50.0);
AnchorPane.setLeftAnchor(text2, 10.0);

label = new Label(" ");
label.setFont(Font.font ("Comic Sans MS", 16));
AnchorPane.setTopAnchor(label, 90.0);
AnchorPane.setLeftAnchor(label, 50.0);

Button button = new Button();
button.setText(" Sure ");
AnchorPane.setTopAnchor(button, 5.0);
AnchorPane.setLeftAnchor(button, 460.0);

textField = new TextField ();
textField.setPrefWidth(160);
textField.getText();
AnchorPane.setTopAnchor(textField, 5.0);
AnchorPane.setLeftAnchor(textField, 290.0);

button.setOnAction(new EventHandler<ActionEvent>() {

@Override
public void handle(ActionEvent e) {
String year = textField.getText().toString();
boolean inputlegal = false;
int Isleapyear = 2;
for (int i=0; i<year.length(); i++) {
if (year.indexOf(year.charAt(i)) == -1) {
inputlegal = false;
}
}

if(inputlegal == true){
if (Double.valueOf(year)%4!=0) {
Isleapyear = 0;
}
else if(Double.valueOf(year)%100==0 && Double.valueOf(year)%400==0){
Isleapyear = 1;
}
else{
Isleapyear = 0;
}
}

if(inputlegal == false){
label.setText("Input error");
}
else if(Isleapyear == 0){
label.setText(textField.getText() + " is not a leap year");
}
else if(Isleapyear == 1){
label.setText(textField.getText() + " is a leap year");
}

}
});

root.getChildren().addAll(text,text2,textField,button,label);
stage.setResizable(false);
stage.setScene(scene);
stage.show();
}
}

通过JAVA程序测试闰年

原文:http://www.cnblogs.com/CrystalN/p/4400908.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!