This week, in the software testing class at Zhang Yuan, the teacher asked the program to determine if the year the user entered was a leap years. I have given the following code to make the judging condition:
If (year% 400 = = 0)
{
Text2.settext ("Leap Year");
Return
}
if (year%!! = 0 && Year% 4 = = 0)
{
Text2.settext ("Leap Year");
Return
}
Text2.settext ("Not a leap year");
}
The program will throw an exception when the user enters illegally.
^ (* ̄ (oo)  ̄*) ^ Note:
Illegal input: The English alphabet, does not satisfy the actual number string, controls the character, the punctuation symbol and so on.
Solution:
1. Code implementation:
Import Java.util.Random;
Import javafx.application.Application;
Import Javafx.event.EventHandler;
Import Javafx.scene.Scene;
Import Javafx.scene.control.Button;
Import Javafx.scene.control.TextField;
Import javafx.scene.input.MouseEvent;
Import Javafx.scene.layout.AnchorPane;
Import Javafx.scene.text.Text;
Import Javafx.stage.Stage;
public class Softwaretest extends application{
/**
* @param args
*/
public static void Main (string[] args) {
Softwaretest.launch (args);
}
@Override
public void start (stage stage) throws Exception {
Anchorpane root = new Anchorpane ();
/*final String str1 = "valid";
Final String str2 = "illegal";
User name
Final TextField textfiled1 = new TextField ();
Root.getchildren (). AddAll (textfiled1);
Anchorpane.settopanchor (textfiled1,100.0);
Anchorpane.setleftanchor (textfiled1,100.0);
Text Text1 = new text ();
Text1.settext ("User name");
Root.getchildren (). AddAll (Text1);
Anchorpane.settopanchor (text1,100.0);
Anchorpane.setleftanchor (text1,50.0);
Password
Final TextField textfiled2 = new TextField ();
Root.getchildren (). AddAll (TEXTFILED2);
Anchorpane.settopanchor (textfiled2,150.0);
Anchorpane.setleftanchor (textfiled2,100.0);
Text Text2 = new text ();
Text2.settext ("password");
Root.getchildren (). AddAll (Text2);
Anchorpane.settopanchor (text2,150.0);
Anchorpane.setleftanchor (text2,50.0);
Verification Code
Final TextField textfiled3 = new TextField ();
Root.getchildren (). AddAll (TEXTFILED3);
Anchorpane.settopanchor (textfiled3,200.0);
Anchorpane.setleftanchor (textfiled3,100.0);
Text Text3 = new text ();
Text3.settext ("Verification Code");
Root.getchildren (). AddAll (TEXT3);
Anchorpane.settopanchor (text3,200.0);
Anchorpane.setleftanchor (text3,50.0);
Final text Text4 = new text ();
Root.getchildren (). AddAll (TEXT4);
Anchorpane.settopanchor (text4,200.0);
Anchorpane.setleftanchor (text4,300.0);
Random ran = new random ();
final int a = Ran.nextint (9000) +1000;
Text4.settext (A + "");
Final text text = new text ();
Root.getchildren (). AddAll (text);
Anchorpane.settopanchor (text,100.0);
Anchorpane.setleftanchor (text,300.0);
Button btn = New button ("OK");
Root.getchildren (). AddAll (BTN);
Anchorpane.settopanchor (btn,250.0);
Anchorpane.setleftanchor (btn,100.0);
btn.setonmouseclicked (New eventhandler<mouseevent> () {
public void handle (MouseEvent arg0) {
Boolean judge1 = true;
Boolean judge2 = true;
String s = textfiled1.gettext ();
if ((S.length () > 6) | | (S.length () < 1))
{
Text.settext (STR2);
Return
}
for (int i = 0; i < s.length (); i++)
{
char C = S.charat (i);
if (! ( (c >= ' A ' && C <= ' Z ') | | (c >= ' a ' && c <= ' z ') | | (c >= ' 0 ' && C <= ' 9 ')))
{
Judge1 = false;
Break
}
}
if (!judge1)
{
Text.settext (STR2);
Return
}
s = Textfiled2.gettext ();
if ((S.length () > 6) | | (S.length () < 1))
{
Text.settext (STR2);
Return
}
for (int i = 0; i < s.length (); i++)
{
char C = S.charat (i);
if (! ( (c >= ' A ' && C <= ' Z ') | | (c >= ' a ' && c <= ' z ') | | (c >= ' 0 ' && C <= ' 9 ')))
{
Judge2 = false;
Break
}
}
if (!judge2)
{
Text.settext (STR2);
Return
}
s = Textfiled3.gettext ();
int I=integer.parseint (s);
if (i! = a)
{
Text.settext (STR2);
}
Else
{
Text.settext (STR1);
}
}
});*/
Text Text1 = new text ();
Text1.settext ("Please enter the Year");
Anchorpane.settopanchor (text1,70.0);
Anchorpane.setleftanchor (text1,100.0);
Root.getchildren (). AddAll (Text1);
Final text Text2 = new text ();
Anchorpane.settopanchor (text2,100.0);
Anchorpane.setleftanchor (text2,300.0);
Root.getchildren (). AddAll (Text2);
Final TextField textfiled1 = new TextField ();
Root.getchildren (). AddAll (textfiled1);
Anchorpane.settopanchor (textfiled1,100.0);
Anchorpane.setleftanchor (textfiled1,100.0);
Button btn = New button ("OK");
Root.getchildren (). AddAll (BTN);
Anchorpane.settopanchor (btn,130.0);
Anchorpane.setleftanchor (btn,100.0);
btn.setonmouseclicked (New eventhandler<mouseevent> () {
public void handle (MouseEvent arg0) {
String s = textfiled1.gettext ();
for (int i = 0; i < s.length (); i++)
{
char C = S.charat (i);
if (! ( C >= ' 0 ' && C <= ' 9 '))
{
Text2.settext ("illegal input");
Return
}
}
Int year =integer.parseint (s);
If (year% 400 = = 0)
{
Text2.settext ("Leap Year");
Return
}
if (year%!! = 0 && Year% 4 = = 0)
{
Text2.settext ("Leap Year");
Return
}
Text2.settext ("Not a leap year");
}
});
Stage.setscene (new Scene (root, 500, 500));
Stage.show ();
}
}
2. Test Cases:
Number |
Test Cases |
Output results |
1 |
2000 |
Leap |
2 |
1900 |
Non-leap year |
3 |
1996 |
Leap |
4 |
1995 |
Non-leap year |
5 |
Abcd |
Illegal input |
3. Result diagram:
Solution for "Judging leap Year" program throws exception