3 回答
TA贡献1853条经验 获得超18个赞
我仍然认为自己相当新,但这就是我会写的内容。我编辑了几次。
if (hasSkunk) {
if (!hasThreePlayer) {
if (redTeamScore<=90) {
baseVictoryText += "\nAnd Red Team got Skunked";
}
} else {
if ((redTeamScore <= 90) && (greenTeamScore <= 90)) {
baseVictoryText += "\nAnd Red and Green Teams got Skunked";
} else if (redTeamScore <= 90) {
baseVictoryText += "\nAnd Red Team got Skunked";
} else if (greenTeamScore <= 90) {
baseVictoryText += "\nAnd Green Team got Skunked";
}
}
}
winningTeamTextView.setText(baseVictoryText);
假设您不介意更改 baseVictoryText (不是常量)。否则将其分配给其他东西并使用它
不假设“hasSkunk”意味着至少有一支球队的得分<90,否则最后一个 else if 会变成 else ...
由于性能原因,“Else if”在理论上会更好 - 只有在其中一个为真时才会评估 if。这在这里不是问题,但如果其中一个条件是某种重型方法的结果......
像这样的一堆 Else If 的工作方式非常类似于带有中断的 switch。
TA贡献1934条经验 获得超2个赞
您可以尝试三元运算符 ?: 。上面代码中的示例我只是简化了某些部分。你可以尝试其他类似的东西
if(!hasThreePlayer) {
winningTeamTextView.setText(hasSkunk&&redTeamScore<=90 ? baseVictoryText + "\nAnd Red Team got Skunked" : baseVictoryText);
}
else {
winningTeamTextView.setText(baseVictoryText);
if (hasSkunk) {
if ((redTeamScore <= 90) && (greenTeamScore <= 90)) {
winningTeamTextView.setText(baseVictoryText + "\nAnd Red and Green Teams got Skunked");
}
if ((redTeamScore <= 90) && (greenTeamScore >= 90)) {
winningTeamTextView.setText(baseVictoryText + "\nAnd Red Team got Skunked");
}
if ((redTeamScore >= 90) && (greenTeamScore <= 90)) {
winningTeamTextView.setText(baseVictoryText + "\nAnd Green Team got Skunked");
}
}
}
TA贡献1848条经验 获得超6个赞
更干净一点,因为只在一个地方设置 textField,不需要 == 一个布尔值,if 应该分支,因为如果一个成功,另一个就会失败。与初始响应略有不同。
if(blueTeamScore >= ENDGAMESCORE) {
String victoryText = "Blue Team has won!";
if(!hasThreePlayer && hasSkunk && redTeamScore <= 90) {
victoryText += "\nAnd Red Team got Skunked");
}else if(hasThreePlayer && hasSkunk) {
if (redTeamScore <= 90 && greenTeamScore <= 90) {
victoryText += "\nAnd Red and Green Teams got Skunked";
}else if (redTeamScore <= 90) {
victoryText += "\nAnd Red Team got Skunked";
}else if (greenTeamScore <= 90) {
victoryText += "\nAnd Green Team got Skunked";
}
}
TextView winningTeamTextView = (TextView) findViewById(R.id.winningTeam);
//maybe check for null textfield here?
winningTeamTextView.setText(victoryText);
}
但我会分解成一个函数
if(blueTeamScore >= ENDGAMESCORE) {
String victoryText = "Blue Team has won!";
if(hasSkunk){
victoryText += getSkunkText(hasThreePlayer, redTeamScore, greenTeamScore)
}
TextView winningTeamTextView = (TextView) findViewById(R.id.winningTeam);
//maybe check for null textfield here?
winningTeamTextView.setText(victoryText);
}
功能:
private String getSkunkText(boolean hasThreePlayer, int redTeamScore, int greenTeamScore){
String skunkText = ""
if(!hasThreePlayer) {
skunkText = redTeamScore <= 90 ? "\nAnd Red Team got Skunked") : skunkText;
}else if (redTeamScore <= 90 && greenTeamScore <= 90) {
skunkText = "\nAnd Red and Green Teams got Skunked";
}else if ( redTeamScore <= 90) {
skunkText = "\nAnd Red Team got Skunked";
}else if (greenTeamScore <= 90) {
skunkText = "\nAnd Green Team got Skunked";
}
return skunkText;
}
添加回答
举报