使if-else解决方案更高效、代码行更少(Java)

使if-else解决方案更高效、代码行更少(Java),java,if-statement,switch-statement,conditional,Java,If Statement,Switch Statement,Conditional,有没有一种更有效的方法来编写这样的代码,而不使用那么多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 * Ma

有没有一种更有效的方法来编写这样的代码,而不使用那么多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;
    }
{
如果组中有3个成员,if-else语句将在总数中添加2个,如果组中有5个成员,则添加4个


我知道我可以用一个组数组做一些更有效的事情,但是有没有一种不用数组的方法呢?也许是CalculateMethod方法的一种方法,可以在不重复其他方法的情况下获得每组的团队成员数?如果您有任何建议,我们将不胜感激。

假设您正在编写java,您应该编写一个case语句,并将每个变量传递给函数。您也应该在第一个函数中定义total,但我不会告诉您如何定义。无论如何,像这样的东西然后在for循环中将每个组传递给它:

public int calculateSomething(groupx){
    switch (groupx) 
        {
            case 3:
            total += 2;
            break;
            case 5:
            total += 4;
            break;
        }

请注意,case不需要在前一行加括号。

假设您正在编写java,您应该编写case语句并将每个变量传递给函数。您也应该在第一个函数中定义total,但我不会告诉您如何定义。无论如何,像这样的东西然后在for循环中将每个组传递给它:

public int calculateSomething(groupx){
    switch (groupx) 
        {
            case 3:
            total += 2;
            break;
            case 5:
            total += 4;
            break;
        }

请注意,case不需要在下一行中加括号。

如果您似乎在代码中发现了冗余模式,那么此时您将创建一个可重用的函数

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 = (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

        }
    }

有关更多信息,请参见

,因为代码中存在冗余模式

    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

        }
    }
有关更多信息,请尝试此

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;
}
试试这个

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;
}

对于以数据为中心的问题,更喜欢数据方法而不是代码方法

首先,以声明方式定义额外的点

private static Map<Integer, Integer> extras = new HashMap<Integer, Integer>() {{
    put(3, 2);
    put(5, 4);
}};
使用map可以避免一个if,并且代码简单且被流自动重用


免责声明:代码可能无法编译或工作,因为它是在我的手机上,但有一个合理的机会,它将工作

对于什么是数据中心的问题,更喜欢数据方法而不是代码方法

首先,以声明方式定义额外的点

private static Map<Integer, Integer> extras = new HashMap<Integer, Integer>() {{
    put(3, 2);
    put(5, 4);
}};
使用map可以避免一个if,并且代码简单且被流自动重用


免责声明:代码可能无法编译或工作,因为它是在我的手机上,但有一个合理的机会,它将工作

总数增加了6,当groupx是3。不,它不是吗?或者你的意思是第三组应该有所不同?在这种情况下,您需要使用类似CalculateMething2GroupX的sig编写一个新func,并将第5行设为=+6而不是3@tenshiman您可能需要更新有关switch/case语句的知识,特别是break的作用以及如果缺少,会发生什么情况。当groupx为3时,total增加6。否,不是吗?或者你的意思是第三组应该有所不同?在这种情况下,您需要使用类似CalculateMething2GroupX的sig编写一个新func,并将第5行设为=+6而不是3@tenshiman你可能想刷新你的Switk/case语句的知识,尤其是一个中断,如果缺少的话会发生什么。你也应该考虑把所有的组变量放进一个数组或一个列表中。这使得通过迭代数据结构来更容易地对它们进行操作,并且如果需要超过4,则可以更容易和更高效地存储大量变量。还应该考虑将所有的组变量放入数组或列表中。这使得通过迭代数据结构对所有变量进行操作变得更容易,并且如果您需要4个以上的变量,则可以更容易、更高效地存储大量变量。回答得很好,谢谢!如果calc方法是从另一个无法修改且没有参数的类调用的,该怎么办?让calc在该类中,而Castint组在该类中会产生问题吗?@Bluasul我根据您更新的要求更新了我的答案。现在你可以在课外打电话了。回答得很好,谢谢!如果calc方法是从另一个无法修改且没有参数的类调用的,该怎么办?让calc在该类中,而Castint组在该类中会产生问题吗?@Bluasul我根据您更新的要求更新了我的答案。现在你可以在课外叫它了。