我正在做關於glassFish安裝附帶的Java EE7的教程。它也可用here。該代碼存在於glassFish服務器安裝目錄中將消息添加到faceContext在GlassFish上運行的Java EE7中不起作用?
/glassFish_installation/glassfish4/docs/javaee-tutorial/examples/cdi/guessnumber-cdi
。
該代碼正常工作。它當前顯示correct!
當用戶正確猜測數字,但在遊戲結束時不顯示failed
。所以我介紹了,只是一個小小的變化,以顯示failed
消息。我在代碼的相關更改之上添加了評論。
不知何故,這種改變沒有幫助。也就是說,在遊戲結束時,failed
消息不顯示。 但遊戲照常運作。我想知道爲什麼這不起作用以及如何糾正?
感謝
public class UserNumberBean implements Serializable {
private static final long serialVersionUID = -7698506329160109476L;
private int number;
private Integer userNumber;
private int minimum;
private int remainingGuesses;
@Inject
@MaxNumber
private int maxNumber;
private int maximum;
@Inject
@Random
Instance<Integer> randomInt;
public UserNumberBean() {
}
public int getNumber() {
return number;
}
public void setUserNumber(Integer user_number) {
userNumber = user_number;
}
public Integer getUserNumber() {
return userNumber;
}
public int getMaximum() {
return (this.maximum);
}
public void setMaximum(int maximum) {
this.maximum = maximum;
}
public int getMinimum() {
return (this.minimum);
}
public void setMinimum(int minimum) {
this.minimum = minimum;
}
public int getRemainingGuesses() {
return remainingGuesses;
}
public String check() throws InterruptedException {
if (userNumber > number) {
maximum = userNumber - 1;
}
if (userNumber < number) {
minimum = userNumber + 1;
}
if (userNumber == number) {
FacesContext.getCurrentInstance().addMessage(null,
new FacesMessage("Correct!"));
}
//if remainingGuesses is less than or equal to zero, display failed message
//-----------------------------------------------
if (remainingGuesses-- <= 0) {
FacesContext.getCurrentInstance().addMessage(null,
new FacesMessage("failed "));
}
return null;
}
@PostConstruct
public void reset() {
this.minimum = 0;
this.userNumber = 0;
this.remainingGuesses = 10;
this.maximum = maxNumber;
this.number = randomInt.get();
}
public void validateNumberRange(FacesContext context,
UIComponent toValidate,
Object value) {
int input = (Integer) value;
if (input < minimum || input > maximum) {
((UIInput) toValidate).setValid(false);
FacesMessage message = new FacesMessage("Invalid guess");
context.addMessage(toValidate.getClientId(context), message);
}
}
}
你得到了!對我來說太愚蠢......但如果剩下的猜測是1,遊戲還沒有完成。所以有更多的猜測可用,這將在下一次迭代中遞減。我的意思是,這種情況會在某個時候執行。只是你會得到一個額外的猜測,而不是你應該得到的。對? – 2014-10-04 05:35:36
沒有,病情評估後的變種被降低,並且沒有更多的猜測提供... – unwichtich 2014-10-04 13:57:30