Input
Enter a paragraph of characters (the correct number may appear other letters, symbols and other illegal characters)
Output
Indicates whether the input is a leap year, and if the input is not a number, the input is illegal.
Definition of Leap Year
Can be divisible by 4 but not divisible by 100, or divisible by 400.
Test Cases
Number |
Input |
Expected output |
1 |
400 |
Is |
2 |
1000 |
Whether |
3 |
10/*0 |
Illegal |
Test:
Code:
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 Run_nian Extends Application {/** * @param args */public static void main (string[] args) {//TODO auto-generated method Stubrun_nia N.launch (args);} @Overridepublic void Start (Stage stage) throws Exception {//TODO auto-generated method Stubanchorpane root = new Anchorpa NE (); Text Nian = new text ("Year:"); Anchorpane.setleftanchor (Nian, 10.0); Anchorpane.settopanchor (Nian, 10.0); final TextField field = new TextField (); Anchorpane.settopanchor (field,10.0); Anchorpane.setleftanchor (field, 50.0); button but = New button ("OK"); Anchorpane.settopanchor (but, 50.0); Anchorpane.setleftanchor (But, 100.0); But.setonaction (new eventhandler<actionevent> () {public void handle ( ActionEvent event) {String out = new String ("Input is not a leap year"), Try{int year = Integer.parseint (Field.gettext ()); if (year%400==0| | (year%4==0&&year%100!=0)) {out = "input is a leap year";}} catch (Exception e) {out = "input not valid";} System.out.println (out);}}); Root.getchildren (). AddAll (nian,field,but); Stage.setscene (new Scene (root,250,100)); Stage.show ();}}
Problem with Integer.parseint (String) error
If you do not apply the exception resolution method for try catch, the direct use of integer.parseint () will be an error, as follows
This error is due to the fact that this function parses a string parameter as a signed decimal integer. The ‘-‘
character in the string must be a decimal number, except for the negative numbers that the first character is a minus sign in the ASCII character.
Questions and procedures about whether the test input is a leap year