有没有更有效的方法来编码这样的东西,而不使用那么多的if-else语句?
private int group1, group2, group3, group4;
private int total = 0
public void assignMembers()
{
group1 = (int)((6 * Math.random()) + 1);
group2 = (int)((6 * Math.random()) + 1);
group3 = (int)((6 * Math.random()) + 1);
group4 = (int)((6 * Math.random()) + 1);
}
public void calculateSomething()
{
if(group1 == 3)
{
total += 2;
}
else if(group1 == 5)
{
total += 4;
}
if(group2 == 3)
{
total += 2;
}
else if(group2 == 5)
{
total += 4;
}
if(group3 == 3)
{
total += 2;
}
else if(group3 == 5)
{
total += 4;
}
if(group4 == 3)
{
total += 2;
}
else if(group4 == 5)
{
total += 4;
}
{
if-else语句表示如果组中有3个成员则加2,如果组中有5个成员则加4。
我知道我可以做一些更有效的"组"数组,但有没有一种方法没有数组?也许有一种方法可以让calculatessomething方法获得每个组的团队成员数量,而不必重复那么多if-else ?如有任何建议,我将不胜感激。
如果你在你的代码中发现了一个多余的模式,那就是你要创建一个可重用的函数的时候了。
private int group1, group2, group3, group4;
private int total = 0;
public void assignMembers()
{
group1 = (int)(Math.random()*6 + 1);
group2 = (int)(Math.random()*6 + 1);
group3 = (int)(Math.random()*6 + 1);
group4 = (int)(Math.random()*6 + 1);
calc(group1);
calc(group2);
calc(group3);
calc(group4);
}
public void calc(int group)
{
switch (group){
case 3:
total += 2;
break;
case 5:
total += 4;
break;
}
}
更新答案-因为要求是:方法必须在类之外调用。
private int group1, group2, group3, group4;
private int total = 0;
public void assignMembers()
{
group1 = (int)(Math.random()*6 + 1);
group2 = (int)(Math.random()*6 + 1);
group3 = (int)(Math.random()*6 + 1);
group4 = (int)(Math.random()*6 + 1);
}
private void calc(int group)
{
switch (group){
case 3:
total += 2;
break;
case 5:
total += 4;
break;
}
}
public void calculateSomething(){
calc(group1);
calc(group2);
calc(group3);
calc(group4);
}
既然你的代码中有一个冗余模式
private int group1, group2, group3, group4;
private int total = 0;
public void assignMembers()
{
group1 = randomGen();
group2 = randomGen();
group3 = randomGen();
group4 = randomGen();
function(group1);
function(group2);
function(group3);
function(group4);
}
public int randomGen(){
int x=(int)(Math.random()*6 + 1);
return x;
}
public void function(int group)
{
switch (group){
case 3:
total += 2;
break;
case 5:
total += 4;
break;
default:
// write here what you need to perform when the group value is 3 or 5
}
}
更多信息请访问本网站
假设您正在编写java,您应该编写case语句并将每个变量传递给函数。你应该在第一个函数中定义total但我不会告诉你怎么做。总之,像这样然后在for循环中将每个组传递给它:
public int calculateSomething(groupx){
switch (groupx)
{
case 3:
total += 2;
break;
case 5:
total += 4;
break;
}
请注意,case不需要在前一行周围加括号。
对于以数据为中心的问题,更倾向于使用"数据"方法而不是"代码"方法。
首先,声明性地定义额外的点。
private static Map<Integer, Integer> extras = new HashMap<Integer, Integer>() {{
put(3, 2);
put(5, 4);
}};
请注意,这是代码中唯一出现这些数字的地方,更改它们或添加更多数字很简单,并且如何做到这一点很明显。
然后使用一个流在一行中处理所有组:
public void calculateSomething() {
total += IntStream.of(group1, group2, group3, group4)
.map(i -> extras.getOrDefault(i, 0))
.sum();
}
使用映射甚至避免了一个if
,并且代码简单,可以被流自动重用。
免责声明:代码可能无法编译或工作,因为它是拇指在我的手机上(但有一个合理的机会,它将工作)
试试这个
private int group1, group2, group3, group4;
private int total = 0;
public void assignMembers() {
group1 = updateTotal((int) ((6 * Math.random()) + 1));
group2 = updateTotal((int) ((6 * Math.random()) + 1));
group3 = updateTotal((int) ((6 * Math.random()) + 1));
group4 = updateTotal((int) ((6 * Math.random()) + 1));
}
int updateTotal(int group)
{
total += group == 3 ? 2 : group == 5 ? 4 : 0;
return group;
}