【问题标题】:Keep track of games played JSF跟踪玩过的游戏 JSF
【发布时间】:2014-06-11 20:50:36
【问题描述】:

我想知道是否有人可以帮助我?

我正在使用 JSF 创建一个简单的游戏。我已经设法完成了主要功能,但我想告诉用户他们玩了多少游戏。

由于某种原因,我为其编写的代码不起作用。

豆子:

import java.util.Random;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean
@SessionScoped
public class GameBeans {
    private int randomNumber;
    private int userGuess;
    private int gamesPlayed;

    public String getWin() {
         if(this.userGuess == this.randomNumber)
        {

            return "Congratulations! You've Won!";
        }
        else
        {
            return "You Lose!";
        }        
    } 
    /**
     * 
     * @return randomNumber
     */
    public int getRandomNumber() {
        return randomNumber;
    }

    /**
     * sets the generated random number
     * @param randomNumber 
     */
    private void setRandomNumber(int randomNumber) {
        this.randomNumber = randomNumber;
    }

    /**
     * 
     * @return the guess of the user
     */
    public int getUserGuess() {
        return userGuess;
    }

    /**
     * Sets the guess of the user into userGuess
     * @param userGuess 
     */
    public void setUserGuess(int userGuess) {
        this.userGuess = userGuess;

    }
    /**
     * 
     * @return number of games played by the user
     */


    public int getGamesPlayed() 
    {
        return gamesPlayed;
    }

    private void setGamesPlayed(int played)
    {
        this.gamesPlayed=played;
    }

    /**
     * Creates a new instance of GameBeans
     * Generates a new random number
     * 
     * Compares random number to user's
     * choice
     * 
     * Keeps total of games played
     */
   public GameBeans() {
        Random number = new Random();
        int rNumber = number.nextInt(1000);
        setRandomNumber(rNumber);
        int played = this.gamesPlayed++;
        setGamesPlayed(played);
    }

}

首页(play_game.xhtml):

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html">
    <h:head>
        <title>Guess Numbers Page</title>
    </h:head>
    <h:body>
        <h:form>
            <h1>Welcome to Your Game Session</h1>
            <p>Number of games played this session: #{gameBeans.gamesPlayed}</p>
            <p>Enter your lucky number guess and then click play</p>
            <p>Your guess: <h:inputText id="iptxt1" value="#{gameBeans.userGuess}" /></p>
            <h:commandButton id="cmdBtn1" value="Play" action="game_result" />            
        </h:form>
    </h:body>
</html>

game_result.xhtml:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html">
    <h:head>
        <title>Game Results</title>
    </h:head>
    <h:body>
        <h:form>
            <p>Your Guess: <h:outputText id="outText1" value="#{gameBeans.userGuess}"></h:outputText></p>
            <p>Random Number: <h:outputText id="outText2" value="#{gameBeans.randomNumber}"></h:outputText></p>
            <p><h:outputText id="outText4" value="#{gameBeans.win}"></h:outputText></p>
            <p>Number of Games Played: #{gameBeans.gamesPlayed}</p>
            <h:commandButton id="cmdBtn1" value="Play Again" action="play_game" />     
        </h:form>
    </h:body>
</html>

我想让用户即使输赢也可以再次玩游戏,应该记录计数(玩过的游戏)。这目前不起作用!

有人可以帮忙吗?

谢谢

【问题讨论】:

  • “不工作”是什么意思?
  • 没有产生错误,也没有任何阻碍应用程序其余部分的东西。只是它没有记录玩了多少游戏。

标签: java jsf jsf-2 javabeans


【解决方案1】:

@SessionScoped bean 仅在客户端第一次访问您的页面时创建一次。然后它将持续到会话结束。换句话说,@SessionScoped bean 的构造函数只被调用一次。这不是增加gamesPlayed 的地方。

@ManagedBean
@SessionScoped
public class GameBeans {
    private int randomNumber;
    private int userGuess;
    private int gamesPlayed;

    public GameBeans() {
        Random number     = new Random();
        this.randomNumber = number.nextInt(1000);
        this.gamesPlayed  = 0;
    }

    public void getWin() {
        if (this.userGuess.equals(this.randomNumber)) 
             return "Congratulations! You've Won!";
        else return "You Lose!";
    }

    public void incrementGamesPlayed() {
        this.gamePlayed++;
    }

    // Getters and Setters
}

这是play_game.xhtml

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html">
    <h:head>
        <title>Guess Numbers Page</title>
    </h:head>
    <h:body>
        <h:form>
            <h1>Welcome to Your Game Session</h1>
            <p>Number of games played this session: #{gameBeans.gamesPlayed}</p>
            <p>Enter your lucky number guess and then click play</p>
            <p>Your guess: <h:inputText id="iptxt1" value="#{gameBeans.userGuess}" /></p>
            <h:commandButton id="cmdBtn1" value="Play" action="game_result" 
                             actionListener="#{gameBeans.incrementGamesPlayed}" />            
        </h:form>
    </h:body>
</html>

【讨论】:

  • 非常感谢...我知道这会很简单 - 它总是如此!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-08
  • 2023-03-15
  • 2012-12-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多