Minimize o código em referência para operações de leitura/gravação
Pergunta
Comecei com o seguinte código:
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
... e eu terminei com:
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
Você vê mais alguma coisa como código morto ou comutável?
Obrigado por todas as respostas. Acabei com esta versão de trabalho:
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
Solução
Para sua primeira versão:
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);
}
}
E para a segunda versão:
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 ;
}
}
Dessa forma, você não faz o mesmo teste 2 vezes.
Outras dicas
Que tal isso? Você não precisa verificar a igualdade C1, C2 duas vezes e pode evitar verificar a igualdade C1, C3 uma vez ..
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);
}
Editar: editado para combinar com a versão original, e não com a versão acabada.
Use abrevias para c3 = c3 * c2;
: c3 *= c2;
/* 4 */ System.out.println(c3 += c2) ;
deveria estar
/* 4 */ System.out.println(c3 += c1) ;
Eu acredito, depois de olhar para sua versão original. E aqui está minha versão.
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, não é uma boa ideia atribuir nada a alguém em sout
.
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow