我必须解决一个导航流程问题,我按下一个按钮,它会随机选择 B 或失败。如果选择B,则继续选择C。C 选择胜利或失败。该函数位于一个托管 bean 中。我已经完成了大部分 html,但我无法让我的函数工作。
这是我的 bean 。
public class Navigation
public String randomGame(String s){
String result;
double x = Math.random();
if (s.equals("a")){
if (x > 0.5)
result = "Defeat?faces-redirect=true";
else
result = "B?faces-redirect=true";
}
else if (s.equals("b")){
if (x < 0.5)
result = "Defeat2?faces-redirect=true";
else
result = "C?faces-redirect=true";
}
else
{
if (x < 0.5)
result = "Defeat?faces-redirect=true";
else
result = "Victory?faces-redirect=true";
}
return result;
}
}
这是起始页的索引
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:head>
<title>MP1 Game</title>
</h:head>
<h:body>
<p>Click "START" to play!</p>
<h:form>
<h:commandButton value="Start" action="#{navigation.randomGame('a')}" />
<input type="button" value="Exit" onclick="self.close()"></input>
</h:form>
</h:body>
我无法让我的函数正常工作,它只能循环 B 和 Defeated,有人可以给我一些提示吗
最佳答案
考虑 x = 0.4; s = 'a'
第一个 else 条件会将您重定向到页面 B,但是 x 仍然小于 0.5。
if (x < 0.5)
result = "Defeat2?faces-redirect=true";
成为现实。
关于java - 选择随机数来显示胜利或失败的导航游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57581073/