【问题标题】:Predict who will win between two teams based on their history根据他们的历史预测谁将在两支球队之间获胜
【发布时间】:2013-09-30 10:15:55
【问题描述】:

所以我周六有一个项目到期(我上周周六开始)。该项目是一个运动预测程序。

我昨天花了所有时间研究和获取有关 EPL(英超联赛)的所有信息,因此我将所有结果和赛程都保存在数据库中。以及他们目前的统计数据(每场比赛的平均进球数、总胜负数、联赛积分等)

我目前正在比较他们赢了多少场比赛。它所做的是从数据库中检索他们本赛季 2 场比赛的数据(他们与每支球队比赛 2 场,对于那些不看足球的人)。它将为平局检索一个“1”,或者为两场比赛检索一个名称,例如“Man Utd”,或者为一场比赛检索“Man Utd”和“Man City”。

我如何编写逻辑来预测哪支球队会赢,谁赢了什么?即:曼联击败曼城,曼城击败曼联,下一场比赛有50-50的机会,但曼联两次击败斯旺西,所以曼联有90%的机会在下一场比赛中击败他们。

这是我的代码,它总是带有“50-50”,所以我不知道我哪里出错了。

这可能是微不足道的,但对我来说不是,请帮忙,因为我代表我的学校在一所大学,那里会有童子军...

/**** get the result of the first game ****/
public String getGame1Result(String team1, String team2) throws SQLException{
    String resultOfGame = null;
    conn = DriverManager.getConnection(connection.conn_url, connection.conn_user, connection.conn_pass);
    statement = conn.prepareStatement("SELECT games_winner, games_draw FROM games WHERE games_team1 = '" + team2 + "' AND games_team2 = '" + team1 + "'");
    result = statement.executeQuery();
    while(result.next()){
        System.out.println(result.getString(1));
        System.out.println(result.getString(2));
        if(result.getString(1) == null){
            resultOfGame = result.getString(2);
        } else if(result.getString(2) == null) {
            resultOfGame = result.getString(1);
        } else {
            resultOfGame = "Did not work";
        }           
    }
    return resultOfGame;
}

/**** get the result of the second game ****/
public String getGame2Result(String team1, String team2) throws SQLException{
    String resultOfGame = null;
    conn = DriverManager.getConnection(connection.conn_url, connection.conn_user, connection.conn_pass);
    statement = conn.prepareStatement("SELECT games_winner, games_draw FROM games WHERE games_team1 = '" + team1 + "' AND games_team2 = '" + team2 + "'");
    result = statement.executeQuery();
    while(result.next()){
        System.out.println(result.getString(1));
        System.out.println(result.getString(2));
        if(result.getString(1) == null){
            resultOfGame = result.getString(2);
        } else if(result.getString(2) == null) {
            resultOfGame = result.getString(1);
        } else {
            resultOfGame = "Did not work";
        }   
    }
    return resultOfGame;
}

/**** compare the winner result ****/
private void compareGameWinnerResult(String game1Result, String game2Result,     String team1, String team2) {
    if((game1Result == "1") || (game2Result == "1")){   // checks for a draw
        System.out.println("draw");
        team1_winner_result = (float) 50;
        team2_winner_result = (float) 50;
    }
//      } else if((game1Result.equals(team1)) && (game2Result.equals(team1))) { // checks for a winner of both games
//          team1_winner_result = (float) 75;
//          team2_winner_result = (float) 25;
//      } 
        else if((game1Result.equals(team2)) &&     (game2Result.equals(team2))){    // checks for a winner of both games
        team1_winner_result = (float) 25;
        team2_winner_result = (float) 75;
    } else if((game1Result.equals(team1) && (game2Result == null))){    // checks for a winner and a draw
        team1_winner_result = (float) 75;
        team2_winner_result = (float) 25;
    } else if ((game1Result == null) && (game2Result.equals(team1))){   // checks for a winner and a draw
        team1_winner_result = (float) 25;
        team2_winner_result = (float) 75;
    } else if((game1Result.equals(team2) && (game2Result == null))){    // checks for a winner and a draw
        team1_winner_result = (float) 75;
        team2_winner_result = (float) 25;
    } else if ((game1Result == null) && (game2Result.equals(team2))){   // checks for a winner and a draw
        team1_winner_result = (float) 25;
        team2_winner_result = (float) 75;
    }else { // last resort
        team1_winner_result = (float) 50;
        team2_winner_result = (float) 50;
    }
    System.out.println(team1 + " " + team1_winner_result);
    System.out.println(team2 + " " + team2_winner_result);

}

【问题讨论】:

  • 始终使用equals() 方法进行字符串值比较。
  • 也永远不要使用串联查询。使用准备语句,否则您的应用程序将暴露于 SQL 注入。
  • 另一个提示:您是否使用跟踪/调试器来查看哪个if statementteam*_winner_result = 50 结尾?

标签: java probability prediction


【解决方案1】:

将下面的 IF 条件改为使用 equals 方法,

if ((game1Result == "1") || (game2Result == "1")) 

if ((game1Result.equals("1")) || (game2Result.equals("1"))) 

【讨论】:

  • 我知道,我也试过了。我遇到的问题是比较团队名称
  • 你能告诉我们在game1Result、game2Result、team1、team2中将什么值传递给compareGameWinnerResult方法。
  • 团队名称,null 或 1
猜你喜欢
  • 2016-09-18
  • 2023-03-05
  • 1970-01-01
  • 1970-01-01
  • 2014-01-28
  • 2013-03-08
  • 2022-07-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多