【问题标题】:Making if-else solution more efficient, less lines of code (Java)使 if-else 解决方案更高效,代码行更少(Java)
【发布时间】:2017-02-17 02:14:52
【问题描述】:

有没有更有效的方法来编写这样的代码而不使用尽可能多的 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。

我知道我可以使用“组”数组做一些更有效的事情,但是有没有没有数组的方法?可能是 calculateSomething 方法的一种方法来获取每个组的团队成员的数量,而不必重复 if-else 这么多?任何建议将不胜感激。

【问题讨论】:

  • 您还应该考虑将所有组变量放入一个数组(或列表)中。这使得通过迭代数据结构更容易对所有变量执行操作,并且如果您需要超过 4 个变量,则可以更轻松、更高效地存储大量变量。

标签: java if-statement switch-statement conditional


【解决方案1】:

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

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

【讨论】:

  • 很好的答案,谢谢!如果 calc() 方法是从另一个无法修改且没有参数的类调用的怎么办?在该类中使用 calc() 并在此类中使用 calc(int group) 会产生问题吗?
  • @Bluasul 我根据您更新的要求更新了我的答案。现在你可以在课堂外调用它了。
【解决方案2】:

由于您的代码中有冗余模式

    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

        }
    }

更多信息visit this site

【讨论】:

    【解决方案3】:

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

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

    请注意,案例不需要在前一行周围加上括号。

    【讨论】:

    • total 增加 6,而 groupx 为 3。
    • 不,不是吗?还是您的意思是 group3 应该有所不同?在这种情况下,您需要使用像 calculateSomething2(groupx) 这样的 sig 编写一个新函数,并使第 5 行为“=+6”而不是 3
    • @tenshiman 您可能想重新了解switch/case 语句,尤其是break 的作用(以及如果丢失会发生什么)。
    【解决方案4】:

    对于以数据为中心的问题,首选“数据”方法而不是“代码”方法。

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

    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,并且代码简单且被流自动重用。

    免责声明:代码可能无法编译或工作,因为它是在我的手机上翻阅的(但它很有可能会工作)

    【讨论】:

      【解决方案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;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-05-18
        • 2023-04-03
        • 2018-06-21
        • 2021-05-06
        • 1970-01-01
        • 2021-06-05
        • 2012-04-06
        相关资源
        最近更新 更多