0

我从以下代码开始:

class Vereinfache2_edit {

    public static void main(String[] args) {

        int c1 = Integer.parseInt(args[0]);
        int c2 = Integer.parseInt(args[1]);
        int c3 = Integer.parseInt(args[2]);

        /* 1 */if (c2 - c1 == 0) {
            /* 2 */if (c1 != c3) {
                c3 += c1;
                /* 4 */System.out.println(c3);
                /* 5 */c3 *= c2;
                /* 6 */}
        }

        /* 7 */if (c1 == c3)
            /* 8 */if (c1 - c2 == 0)
            /* 9 */{
                c3 += c1;
                /* 10 */System.out.println(c3);
                /* 11 */c3 *= c1;
                /* 12 */if (c1 < c2)
                    c2 += 7;
                /* 13 */else
                    c2 += 5;
                /* 14 */}

        /* 15 */System.out.println(c1 + c2 + c3);
    }

} // end of class Vereinfache2

......我结束了:

class Vereinfache2 { 

        public static void main(String [] args) {

           int c1 = Integer.parseInt(args[0]) ;
           int c2 = Integer.parseInt(args[1]) ;
           int c3 = Integer.parseInt(args[2]) ;

/*  1 */       
    /*  2 */        if (c2 == c1 && c1 != c3){  
    /*  4 */              System.out.println(c3 += c2) ; 
    /*  5 */              c3 = c3 * c2 ; 
    /*  6 */        }
/*  7 */      
    /*  8 */        if ( c2 == c1 && c1 == c3){
    /* 10 */            System.out.println(c3 *= 2) ; 
    /* 11 */            c3 = c3 * c2 ; c2 = c2 + 5 ; 
    /* 14 */        }


/* 15 */       System.out.println( c1+c2+c3) ;     
        }          

}  // end of class Vereinfache2

您是否看到其他类似死代码或可切换代码的​​内容?

感谢所有的答案。我最终得到了这个工作版本:

class Vereinfache2 { 

        public static void main(String [] args) {

           int c1 = Integer.parseInt(args[0]) ;
           int c2 = Integer.parseInt(args[1]) ;
           int c3 = Integer.parseInt(args[2]) ;

/*  1 */       if(c2 == c1){
    /*  2 */        if (c1 != c3){  
                        c3 += c2;
    /*  4 */            System.out.println(c3) ;          
    /*  6 */        }else{
                        c3 *= 2;
    /* 10 */            System.out.println(c3) ; 
    /* 14 */        }
                    c3 *= c2; c2 += 5;
               }

/* 15 */       System.out.println(c1+c2+c3) ;      
        }          

}  // end of class Vereinfache2
4

4 回答 4

2

对于您的第一个版本:

      if (c2 == c1) {
        if (c1 != c3) {
          c3 += c1;
          System.out.println(c3);
          c3 *= c2;
        } else {
          c3 += c1;
          System.out.println(c3);
          c3 *= c1;
          if (c1 < c2)
            c2 += 7;
          else
            c2 += 5;
        }
      } else if (c1 < c2)
          c2 += 7;
        else
          c2 += 5;
    }
    System.out.println(c1 + c2 + c3);
  }
}

对于第二个版本:

           if (c2 == c1)
              if( c1 != c3){  
                System.out.println(c3 += c2) ; 
                c3 = c3 * c2 ; 
              } else {
                System.out.println(c3 *= 2) ; 
                c3 = c3 * c2 ; c2 = c2 + 5 ; 
              }
            }          

这样你就不会做同样的测试 2 次。

于 2010-10-31T09:55:56.210 回答
2

那这个呢?您不需要检查 c1、c2 相等性两次,并且可以避免检查 c1、c3 相等性一次。

public static void main(String[] args) {

        int c1 = Integer.parseInt(args[0]);
        int c2 = Integer.parseInt(args[1]);
        int c3 = Integer.parseInt(args[2]);

        if (c2 == c1) {
        int c4 = c3 + c1;
        System.out.println(c4);
        if (c1 == c3) {
            c2 += 5;
        }
        c3 = c4 * c1;

    }

        System.out.println(c1 + c2 + c3);
    }

编辑:编辑以匹配原始版本而不是最终版本。

于 2010-10-31T09:58:46.727 回答
1
/*  4 */              System.out.println(c3 += c2) ; 

应该

/*  4 */              System.out.println(c3 += c1) ; 

我相信,在看了你的原始版本之后。这是我的版本。

public static void main(String[] args) {

    int c1 = Integer.parseInt(args[0]);
    int c2 = Integer.parseInt(args[1]);
    int c3 = Integer.parseInt(args[2]);

    if (c2 == c1) {
        c3 += c1;
        System.out.println(c3);
        if (c1 != c3) {
            c3 *= c2;
        } else {
            c3 *= c1;
            c2 += 5;
        }
        System.out.println(c1 + c2 + c3);
    }
}

IMO,将任何东西分配给sout.

于 2010-10-31T09:50:22.960 回答
1

使用简写c3 = c3 * c2;c3 *= c2;

于 2010-10-31T10:01:08.423 回答