Leap year detection is an important step for a program that needs to record years. The leap year rules for the Gregorian calendar are as follows:
1, the ordinary year can be divisible by 4 and not divisible by 100 for leap years. (For example, 2004 is a leap year, 1900 is not a leap)
2, Century can be divisible by 400 is a leap year. (as 2000 is a leap year, 1900 is not a leap year)however, for incorrect input, the program must respond to the hold, not to prevent the abnormal situation to hinder the operation of the program, the following is a leap year test program example:
Enter year |
Results |
2000 |
Leap |
1900 |
Non-leap year |
-1600 |
Illegal input |
1864 |
Leap |
Saddsa |
Illegal input |
Code implementation:
Importjavafx.application.Application;Importjavafx.event.ActionEvent;ImportJavafx.event.EventHandler;ImportJavafx.scene.Scene;ImportJavafx.scene.control.Button;ImportJavafx.scene.control.TextField;ImportJavafx.scene.layout.AnchorPane;ImportJavafx.scene.text.Text;ImportJavafx.stage.Stage; Public classTest2extendsApplication { Public Static voidMain (string[] args) {test2.launch (args); } Public voidStart (Stage primarystage) {primarystage.settitle ("Input Testing" ); Anchorpane Root=NewAnchorpane (); Text text=NewText ("Please enter Test year"); FinalText textans1 =NewText (""); FinalTextField Intext1 =NewTextField (""); 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=NewButton ("Submit"); Root.getchildren (). AddAll (BTN); Anchorpane.settopanchor (BTN,90.0); Anchorpane.setleftanchor (BTN,120.0); Btn.setonaction (NewEventhandler<actionevent>() { Public voidhandle (ActionEvent arg0) {textans1.settext (check (Intext1.gettext (). toString ())); } }); Primarystage.setscene (NewScene (root,300,200)); Primarystage.show (); } Publicstring Check (String str) {Try{ intYear =integer.parseint (str); if(Year <0) return"Please enter the correct year"; if(Year% 100 ==0){ if(Year% 400 = = 0) returnStr+ "is a leap year"; } Else{ if(year% 4 = = 0) returnStr+ "is a leap year"; } }Catch(Exception e) {return"Illegal input"; } returnstr+ "Not a leap year"; } }
Software testing-Leap year testing