gpt4 book ai didi

java - 切换大小写不返回字符串

转载 作者:行者123 更新时间:2023-12-02 03:03:50 25 4
gpt4 key购买 nike

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

例如。曼联 - 利物浦重返英超巴萨 - 皇家马德里重返西甲

我需要使用 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;
}

提前感谢您的任何指导。

最佳答案

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

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

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

关于java - 切换大小写不返回字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42039350/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com