等价类划分的应用2
这篇博客是上一篇博客的扩展,上一篇博客中我们一起讨论了一道有关EditBox的问题,详情请见:
http://www.cnblogs.com/machuk/p/4357219.html
今天让我们一起来看看另一道类似的问题:
1. 问题描述:
有3个EditBox,每次允许输入1~6个英文字符或数字,按OK结束
2. 等价类划分:
编号 有效等价类 编号 无效等价类
1 长度1-6 4 长度小于1(即为0)
5 长度大于6
2 字符为a-z,A-Z,0-9 6 字符为英文/数字以外字符,控制字符,标点符号
3 符合要求个数为3 7 符合要求个数小于3(即为0,1,2)
3. UserForm2测试用例:
No 输入 覆盖等价类 期望输出
1 123 1,2,3 true
456
789
2 abc 1,2,3 true
def
ghi
3 4,2,7 false
123
abc
4 123 1,6,7 false
456,.
abc
5 123 5,2,7 false
abc
4567890
6 1234567 5,6,7 false
abc
123,.
4. 测试结果:
5. 测试代码:
1 import javafx.application.Application; 2 import javafx.event.ActionEvent; 3 import javafx.event.EventHandler; 4 import javafx.scene.Scene; 5 import javafx.scene.control.Button; 6 import javafx.scene.control.TextField; 7 import javafx.scene.layout.AnchorPane; 8 import javafx.scene.layout.HBox; 9 import javafx.scene.layout.VBox; 10 import javafx.scene.text.Text; 11 import javafx.stage.Stage; 12 13 public class Test extends Application{ 14 public static void main(String[] args) { 15 Test.launch(args); 16 } 17 18 public void start(Stage stage ){ 19 stage.setTitle("UserForm2"); 20 AnchorPane root = new AnchorPane(); 21 22 VBox vbox = new VBox(8); 23 HBox hbox1 = new HBox(8); 24 HBox hbox2 = new HBox(8); 25 HBox hbox3 = new HBox(8); 26 Text t1 = new Text("Name1: "); 27 final TextField t2 = new TextField(); 28 hbox1.getChildren().addAll(t1, t2); 29 30 Text t3 = new Text("Name2: "); 31 final TextField t4 = new TextField(); 32 hbox2.getChildren().addAll(t3, t4); 33 34 Text t5 = new Text("Name3: "); 35 final TextField t6 = new TextField(); 36 hbox3.getChildren().addAll(t5, t6); 37 38 vbox.getChildren().addAll(hbox1, hbox2, hbox3); 39 AnchorPane.setTopAnchor(vbox, 50.0); 40 AnchorPane.setLeftAnchor(vbox, 50.0); 41 root.getChildren().add(vbox); 42 43 Button btn = new Button("OK"); 44 AnchorPane.setTopAnchor(btn, 140.0); 45 AnchorPane.setLeftAnchor(btn, 130.0); 46 root.getChildren().add(btn); 47 48 btn.setOnAction(new EventHandler<ActionEvent>(){ 49 @Override 50 public void handle(ActionEvent actEvt) { 51 if((check(t2.getText().toString())) && (check(t4.getText().toString())) && 52 (check(t6.getText().toString()))) 53 System.out.println("true"); 54 else 55 System.out.println("false"); 56 } 57 }); 58 59 stage.setScene(new Scene(root, 300, 200)); 60 stage.show(); 61 } 62 63 public boolean check(String s){ 64 char array[] = new char[s.length()]; 65 array = s.toCharArray(); 66 if (s.length() < 1 || s.length() > 6) 67 return false; 68 if (s.length() != 0){ 69 for (int i = 0; i < s.length(); i++){ 70 if(!Character.isDigit(array[i]) && !Character.isAlphabetic(array[i])) 71 return false; 72 } 73 } 74 return true; 75 } 76 }