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.pri开发者_开发知识库ntln(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..
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.
Use shorthands for c3 = c3 * c2;
: c3 *= c2;
/* 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
.
精彩评论