Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ejercicio 13 #5

Closed
ijaureguialzo opened this issue Oct 21, 2017 · 4 comments
Closed

Ejercicio 13 #5

ijaureguialzo opened this issue Oct 21, 2017 · 4 comments

Comments

@ijaureguialzo
Copy link

Observaciones

(1)

Para controlar el bucle do-while, en vez de una variable numérica usa un boolean, queda más clara su función. Una variable numérica da a entender que vas a contar algo.

(2)

En vez de escribir esto:

if (new String(x).equals("par")==true) {

Escribe:

if (x.equals("par")) { // .equals() ya devuelve true o false

Queda mucho más claro.

(3)

En este bloque:

if (new String(x).equals("par")==true) {
                    System.out.println("Los 100 primeros números pares son:");
                    int vueltas = 99;
                    int par = 0;
                    while (vueltas > 0) {
                        System.out.print(par+", ");
                        par = par + 2;
                        --vueltas;
                    }
                    System.out.println("198.");
                    ++end;
                } else {
                    if (new String(x).equals("impar")==true) {
                        System.out.println("Los 100 primeros números impares son:");
                        int vueltas = 99;
                        int impar = 1;
                        while (vueltas > 0) {
                            System.out.print(impar+", ");
                            impar = impar + 2;
                            --vueltas;
                        }
                        System.out.println("199.");
                        ++end;
                    } else {

¿No hay mucha repetición? Hay muy poco que cambie de uno a otro si te fijas.

Mejoras necesarias

  • Reescribe el bloque (3) usando solo un if y un for.
@danieltamargo
Copy link
Owner

Lo acabo de rehacer. A ver que pinta tiene ahora.
Como me gusta mantener las distintas versiones para ver los cambios en un momento dado lo he resubido sin eliminar el anterior, la carpeta del ejercicio "mejorado" se llama "Ejercicio13-100ParImparV2" y la carpeta del ejercicio obsoleto "version1confallos".

@ijaureguialzo
Copy link
Author

No hace falta que subas nuevas copias, puedes ver como estaba el archivo .java en cualquier versión anterior en GitKraken usando File History.

@ijaureguialzo
Copy link
Author

El for también admite esto:

 for (vuelta = 1; vuelta < 100; vuelta += 2) {

Y avanza de dos en dos.

El if del final es un poco raro, si cambiamos el límite del bucle, hay que calcular a mano los números. ¿No hay otro modo de conseguir esto?

@danieltamargo
Copy link
Owner

Lo he vuelto a resubir, tiene mejor pinta ahora

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants