【问题标题】:Switch case not returning string切换案例不返回字符串
【发布时间】:2017-02-04 10:38:10
【问题描述】:

我有一个数据生成项目,我需要将生成的数据与另一个变量进行匹配。

例如。曼联-利物浦回归英超 巴萨-皇马回归西甲

我需要使用一个 switch case 方法 generateLeague,它应该作用于 MatchEvent 类中的字符串匹配变量。我所做的编码似乎没有捕捉到生成的 MatchEvent 匹配值,也没有返回字符串,因为 String LeagueName = generateLeague 没有收到字符串。它说BetEvent中的generateLeague(String)不能应用。

这是具有数据生成功能的 MatchEvent 类(确实有效)。

    /**
 * Mock a transaction for testing purposes
 */
public MatchEvent generateEvent() {
    try {
        DataFactory df = new DataFactory();

        // BetEvent
        int betID = df.getNumberBetween(220000000, Integer.MAX_VALUE);
        float odds = df.getNumberBetween((int) 1.05, 30);
        // Selections
        String selectionID = UUID.randomUUID().toString();
        // Market
        // Event
        String eventID = UUID.randomUUID().toString();
        String match = df.getItem(StaticTestData.match, 80, "Liverpool vs Manchester Utd"); // should match league and categoryID
        //SubCategory
        // category
        final int categoryID = 1;       // LeagueID by number eg. 1 is for LaLiga, should match MatchLeague and Match
        final String categoryName = "Football";
        // Subcategory
        String subCategoryID = UUID.randomUUID().toString();
        String leagueName = generateLeague();      // should match Match and category ID
        final String parentSubCategory = null;
        // market
        final String name = null;
        float matchOdds = df.getNumberBetween((int) 1.05, 30);  // since focusing on prematch should keep maxNo 20?
        //betEvent
        float stake = df.getNumberBetween(0, 1200);
        boolean mobile = Math.random() < 0.5;
        final String providerName = "EPBetsSportsbook";
        float totalOdds = matchOdds;
        float totalStake = stake;
        String nation = df.getItem(StaticTestData.countryCodes);     // should make it as realistic as possible
        final String currency = "EUR";


        Date date = new Date();
        java.text.DateFormat formatter = new java.text.SimpleDateFormat("MM-dd-yyyy");
        String calendarDate = formatter.format(date);

        return new MatchEvent(betID, odds, selectionID, eventID, match, categoryID, categoryName, subCategoryID,
                leagueName, parentSubCategory, name, matchOdds, stake, mobile, providerName,
                totalOdds, totalStake, nation, date, currency, calendarDate);


    } catch (Exception e) {

        // For demo purposes, we are not going to log errors/send to a kafka stream

        throw e;
    }
}

这是使用 Switch case 语句的 generateLeague 方法。

public static String generateLeague(String match) {

    String club = null;
    String league;
    if (match.toLowerCase().contains(" ")) {
        club = match.substring(0, match.indexOf(" "));
    }
        switch (club) {
            case "arsenal":
            case "bournemouth":
            case "burnley":
            case "chelsea":
            case "crystal":
            case "everton":
            case "hull":
            case "leicester":
            case "liverpool":
            case "manchester":
            case "middlesborough":
            case "southampton":
            case "stoke":
            case "sunderland":
            case "swansea":
            case "tottenham":
            case "watford":
            case "west":
                league = "Premier League";
                break;
            case "atletico":
            case "barcelona":
            case "real":
                league = "La Liga";
                break;
                default: league = "";

        }

    return league;
}

我将不胜感激并提前感谢您的任何指导。

【问题讨论】:

  • 你用的是哪个版本的java?
  • 我正在使用 Java 8
  • 请删除与问题无关的所有内容(我几乎不认为您需要整个 generateEvent 方法来重现您的问题)并确保 club 确实是其中一种情况(使用调试器)。您可能还想阅读:minimal reproducible example.
  • 为什么你没有在generateLeague(String match) 中传递任何String 参数
  • 您应该学习如何使用调试器。逐行检查您的代码,直到找到问题所在。

标签: java string switch-statement


【解决方案1】:

如果字符串匹配中没有空格,则 club 将为空。那么它当然不会正确切换。您需要在 if 语句中添加 else 语句。您可能还想确保俱乐部是小写的。像这样:

if (match.toLowerCase().contains(" ")) {
    club = match.toLowerCase().substring(0, match.indexOf(" "));
} else {
    club = match.toLowerCase();
}

另一件事:在 generateEvent: 你有 generateLeague()。这不会编译。做 generateLeague(match);

【讨论】:

  • 但是在这种情况下generateLeague() 不会仍然返回null 字符串吗?
  • 不,不会的。如果 switch 中的任何东西都没有运行,则默认值将运行,并且 League 将是空字符串。所以它总是不为空的。
  • 感谢您的帮助,现在可以正常工作,问题已解决。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-16
  • 1970-01-01
  • 1970-01-01
  • 2022-01-25
相关资源
最近更新 更多