[英]Minimize code in reference to read/write operations
I started with the following code: 我从以下代码开始:
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
...and I ended with: ...最后我得到:
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
Do you see anything else like dead or switchable code? 您是否看到其他任何东西,例如无效代码或可切换代码?
Thanks for all answers. 感谢所有答案。 I ended up with this working version:
我最终得到了这个工作版本:
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
For your first version: 对于您的第一个版本:
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);
}
}
and for the second version: 对于第二个版本:
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 ;
}
}
This way you don't do the same test 2 times. 这样,您不会进行两次相同的测试。
What about this? 那这个呢? you don't need to check for c1, c2 equality twice and you can avoid checking for c1,c3 equality once..
您不需要两次检查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);
}
EDIT: Edited to match with the original version rather than with your ended up version. 编辑:编辑以匹配原始版本,而不是最终版本。
/* 4 */ System.out.println(c3 += c2) ;
should be 应该
/* 4 */ System.out.println(c3 += c1) ;
I believe, after looking at your original version. 我相信,在查看您的原始版本之后。 And here is my version.
这是我的版本。
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, its not a good idea to assign anything to anyone in sout
. 海事组织,它不是一个好主意,指定什么任何人
sout
。
Use shorthands for c3 = c3 * c2;
对
c3 = c3 * c2;
使用简写形式c3 = c3 * c2;
: c3 *= c2;
:
c3 *= c2;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.