Réduire au minimum le code en référence aux opérations de lecture / écriture
Question
J'ai commencé avec le code suivant:
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
... et j'ai fini avec:
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
Est-ce que vous voyez quoi que ce soit d'autre comme code mort ou commutable?
Merci pour toutes les réponses. J'ai fini avec cette version de travail:
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
La solution
Pour votre première 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);
}
}
et pour la deuxième 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 ;
}
}
De cette façon, vous ne faites pas 2 fois le même test.
Autres conseils
Qu'en est-ce? vous n'avez pas besoin de vérifier c1, l'égalité c2 deux fois et vous pouvez éviter de vérifier c1, l'égalité c3 fois ..
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:. Edité pour correspondre avec la version originale plutôt que avec votre version fini
Utilisez pour c3 = c3 * c2;
: sténographies c3 *= c2;
/* 4 */ System.out.println(c3 += c2) ;
doit être
/* 4 */ System.out.println(c3 += c1) ;
Je crois, après avoir regardé votre version originale. Et voici ma 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);
}
}
l'OMI, ce ne est pas une bonne idée de quoi que ce soit à qui que ce soit dans assign sout
.