java开关大小写不返回字符串
我有一个数据生成项目,我需要将生成的数据与另一个变量相匹配
曼联-利物浦重返英超 巴萨-皇家马德里回归西甲
我需要使用一个switch case方法generateLeague,它应该作用于类MatchEvent中的字符串匹配变量。我所做的编码似乎既没有捕获生成的MatchEvent匹配值,也没有返回字符串,因为string leagueName=generateLeague没有收到字符串。它表示不能应用BetEvent中的generateLeague(字符串)
这是带有数据生成的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;
}
我将提前感谢您的指导
# 1 楼答案
如果字符串匹配中没有空格,则club将为null。那当然不能正常切换了。您需要在if语句中添加else语句。您可能还需要确保club是小写的。像这样:
另一件事:在generateEvent中:您有generateLeague()。这不会编译。不生成(匹配)