豆類
private String password = null;
private int guessCtr = 4;
private String[] passFields;
private String[] newpassFields;
public LogBean(){
password = "12345678";
//create your password array
passFields = password.split("");
//if using java 8. remove this line and use the passFields instead.
//this is just a workaround to remove the first empty element result of .split()
newpassFields = Arrays.copyOfRange(passFields, 1, passFields.length);
//create an array of int index
int[] index = new int[newpassFields.length];
for(int i=0; i<index.length; i++){
index[i] = i;
}
//store the indexes in a list for the shuffle method.
//you can use any other method to do your randomization here
List<Integer> list = new ArrayList<Integer>();
for (int i : index) {
list.add(i);
}
Collections.shuffle(list);
//use sublist here to get only the first 4(guessCtr) elements
list = list.subList(list.size()-(list.size()-guessCtr), list.size());
//set the 4 random values to '' and will also be used for the disabled logic.
for(int i = 0; i < list.size(); i++){
newpassFields[list.get(i)] = "";
}
}
頁
<h:form>
<ui:repeat value="#{logBean.newpassFields}" varStatus="loop">
<p:inputText type="password" disabled="#{logBean.newpassFields[loop.index] != ''}" value="#{logBean.newpassFields[loop.index]}" />
</ui:repeat>
<h:commandButton value="submit" actionListener="#{logBean.submit}"></h:commandButton>
</h:form>
您提交現在應該只是newpassFields轉換爲字符串,並比較其密碼。
你的問題是什麼? – Holger
我的問題是如何隨機密碼每次禁用4個字段。 –
有什麼關於'
Holger