First, a brief introduction to the Gregorian calendar in the leap year: four Years a leap, a century does not leap, 400 years and then leap.
For this rule, a brief design part of the test case:
Attached (test):
The following is the code snippet for the program:
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, Max.);
text text = new text ();
Text.settext ("Enter a particular year for test:");
Text.setfont (Font.font ("Comic Sans MS",));
anchorpane.settopanchor (text, 0.0);
Anchorpane.setleftanchor (text, 10.0);
text Text2 = new text ();
Text2.settext ("Result:");
Text2.setfont (Font.font ("Comic Sans MS",));
Anchorpane.settopanchor (Text2, 50.0);
Anchorpane.setleftanchor (Text2, 10.0);
label = new label ("");
Label.setfont (Font.font ("Comic Sans MS", +));
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 ();
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 ();
}
}
Test a leap year with a Java program