gpt4 book ai didi

java - 最小化涉及读/写操作的代码

转载 作者:行者123 更新时间:2023-12-01 16:40:32 24 4
gpt4 key购买 nike

我从以下代码开始:

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

最佳答案

对于您的第一个版本:

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

这样您就不会进行两次相同的测试。

关于java - 最小化涉及读/写操作的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4062618/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com