zoukankan      html  css  js  c++  java
  • 【判断闰年】程序抛出异常的解决方案

    ●●● 这个星期,在章院的软件测试课堂上,老师让写出判断用户输入的年份是否为闰年的程序。我给出了下面的代码来做判断条件:    

     if (year % 400 == 0)
      {
      text2.setText("闰年");
      return;
      }
      if (year % 100 != 0 && year % 4 == 0)
      {
      text2.setText("闰年");
      return;
      }
      text2.setText("不是闰年");
    }

    ●●● 该程序设计在用户非法输入后,会抛出异常。

          ^(* ̄(oo) ̄*)^ 注意:

                                 非法输入:英文字母,不满足实际的数字串,控制字符,标点符等。

          

    ●●● 解决方案:

    1.代码实现:

    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 = "合法";
    final String str2 = "不合法";
    //用户名
    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("用户名");
    root.getChildren().addAll(text1);
    AnchorPane.setTopAnchor(text1,100.0);
    AnchorPane.setLeftAnchor(text1,50.0);

    //密码
    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("密码");
    root.getChildren().addAll(text2);
    AnchorPane.setTopAnchor(text2,150.0);
    AnchorPane.setLeftAnchor(text2,50.0);

    //验证码
    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("验证码");
    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("确定");
    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("请输入年份");
    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("确定");
    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("非法输入");
     return;
     }
      }
      int year =Integer.parseInt(s);
      if (year % 400 == 0)
      {
      text2.setText("闰年");
      return;
      }
      if (year % 100 != 0 && year % 4 == 0)
      {
      text2.setText("闰年");
      return;
      }
      text2.setText("不是闰年");
    }
    });

    stage.setScene(new Scene(root, 500, 500));
    stage.show( );
         
    }

    }

    2.测试用例:

    编号 测试用例 输出结果
    1 2000 闰年
    2 1900 非闰年
    3 1996 闰年
    4 1995 非闰年
    5 abcd 非法输入

    3.结果图:

  • 相关阅读:
    MAC mysql安装及设置
    Mac下php连接mysql数据库失败解决办法
    Mac Yosemite OS10.10 Apache 虚拟主机设置
    《深入理解JavaScript》——Day1
    《JavaScript设计模式》读书笔记——Day3
    《JavaScript设计模式》读书笔记——Day2.1
    《JavaScript设计模式》读书笔记——Day2
    《JavaScript设计模式》读书笔记——Day1
    随笔,记录JavaScript中的面试题
    《JavaScript权威指南》学习笔记——Day3
  • 原文地址:https://www.cnblogs.com/summer23/p/4394696.html
Copyright © 2011-2022 走看看