Questions and procedures about whether the test input is a leap year

Source: Internet
Author: User
Tags try catch

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.