Foro de elhacker.net

Programación => Java => Mensaje iniciado por: CharlyScoTT en 20 Octubre 2017, 23:53 pm



Título: Numero mayor y numero menor!!!! (error)
Publicado por: CharlyScoTT en 20 Octubre 2017, 23:53 pm
Mi problema es que cuando esta corriendo si me muestra el maximo de cualquier cantidad que yo introduzca pero... cuando es menor solo me muestra 0 no me muestra el numero menor que ponga el usuario.... ejemplo 5 es mi numero minimo que intridujo ......363 es el numero 20 lo que me muestra es
min:0
max:363

Código
  1.  
  2. package primerparteexamen;
  3.  
  4. import java.util.Scanner;
  5.  
  6.  
  7. public class Primerparteexamen {
  8.  
  9.    public static void main(String[] args) {
  10.       Scanner sr= new Scanner(System.in);
  11.  
  12.         System.out.println("1 calificación");
  13.         int q=sr.nextInt();
  14.         System.out.println("2 calificación");
  15.         int w=sr.nextInt();
  16.         System.out.println("3 celificación");
  17.         int e=sr.nextInt();
  18.         System.out.println("4 calificación");
  19.         int r=sr.nextInt();
  20.         System.out.println("5 calificación");
  21.         int t=sr.nextInt();
  22.         System.out.println("6 calificación");
  23.         int y=sr.nextInt();
  24.         System.out.println("7 calificación");
  25.         int u=sr.nextInt();
  26.         System.out.println("8 calificación");
  27.         int i=sr.nextInt();
  28.         System.out.println("9 calificación");
  29.         int o=sr.nextInt();
  30.         System.out.println("10 calificación");
  31.         int p=sr.nextInt();
  32.         System.out.println("11 calificación");
  33.         int a=sr.nextInt();
  34.         System.out.println("12 calificación");
  35.         int s=sr.nextInt();
  36.         System.out.println("13 calificación");
  37.         int d=sr.nextInt();
  38.         System.out.println("14 calificación");
  39.         int f=sr.nextInt();
  40.         System.out.println("15 calificación");
  41.         int g=sr.nextInt();
  42.         System.out.println("16 calificación");
  43.         int h=sr.nextInt();
  44.         System.out.println("17 calificación");
  45.         int j=sr.nextInt();
  46.         System.out.println("18 calificación");
  47.         int k=sr.nextInt();
  48.         System.out.println("19 calificación");
  49.         int l=sr.nextInt();
  50.         System.out.println("20 calificación");
  51.         int ñ=sr.nextInt();
  52.  
  53.  
  54.          int min = 0;
  55.        int max = 0;
  56.  
  57.        if (q < min) {
  58.          min = q;
  59.        } else if (q > max) {
  60.          max = q;
  61.        }
  62.  
  63.        if (w < min) {
  64.          min = w;
  65.        } else if (w > max) {
  66.            max = w;
  67.        }
  68.  
  69.        if (e < min) {
  70.          min = e;
  71.        } else if (e > max) {
  72.          max = e;
  73.        }
  74.        if (r < min) {
  75.          min = r;
  76.        } else if (r > max) {
  77.          max = r;
  78.        }
  79.        if (t < min) {
  80.          min = t;
  81.        } else if (t > max) {
  82.          max = t;
  83.        }
  84.  
  85.        if (y < min) {
  86.          min = y;
  87.        } else if (y > max) {
  88.            max = y;
  89.        }
  90.  
  91.        if (u < min) {
  92.          min = u;
  93.        } else if (u > max) {
  94.          max = u;
  95.        }
  96.        if (i < min) {
  97.          min = i;
  98.        } else if (i > max) {
  99.          max = i;
  100.        }
  101.        if (o < min) {
  102.          min = o;
  103.        } else if (o > max) {
  104.          max = o;
  105.        }
  106.  
  107.        if (p < min) {
  108.          min = p;
  109.        } else if (p > max) {
  110.            max = p;
  111.        }
  112.  
  113.        if (a < min) {
  114.          min = a;
  115.        } else if (a > max) {
  116.          max = a;
  117.        }
  118.        if (s < min) {
  119.          min = s;
  120.        } else if (s > max) {
  121.          max = s;
  122.        }
  123.         if (d < min) {
  124.          min = d;
  125.        } else if (d > max) {
  126.          max = d;
  127.        }
  128.  
  129.        if (f < min) {
  130.          min = f;
  131.        } else if (f > max) {
  132.            max = f;
  133.        }
  134.  
  135.        if (g < min) {
  136.          min = g;
  137.        } else if (g > max) {
  138.          max = g;
  139.        }
  140.        if (h < min) {
  141.          min = h;
  142.        } else if (h > max) {
  143.          max = h;
  144.        }
  145.        if (j < min) {
  146.          min = j;
  147.        } else if (j > max) {
  148.          max = j;
  149.        }
  150.  
  151.        if (k < min) {
  152.          min = k;
  153.        } else if (k > max) {
  154.            max = k;
  155.        }
  156.  
  157.        if (l < min) {
  158.          min = l;
  159.        } else if (l > max) {
  160.          max = l;
  161.        }
  162.        if (ñ < min) {
  163.          min = ñ;
  164.        } else if (ñ > max) {
  165.          max = ñ;
  166.        }
  167.  
  168.         System.out.println("Min: " + min);
  169.        System.out.println("Max: " + max);
  170.  
  171.    }
  172.  
  173. }
  174.  


· Los temas deben ir en su respectivo subforo (esto no es "progranacióm general", es java)
>aquí las reglas del foro (http://foro.elhacker.net/reglas.htm)
-Engel Lex


Título: Re: Numero mayor y numero menor!!!! (error)
Publicado por: biestable en 21 Octubre 2017, 08:47 am
uff, deberias buscar una forma de automatizar esos if
¿si tuvieras mil números escribirías mil ifs?

Respecto a tu duda, evidentemente el programa te devuelve como mínimo el numero 0.

Has iniciado la variable min a 0 y ningún número positivo será menor que 0, por tanto ningún numero positivo que introduzcas por teclado cumplirá la condición de ser menor que min y nunca actualizarás esa variable.


Título: Re: Numero mayor y numero menor!!!! (error)
Publicado por: engel lex en 21 Octubre 2017, 19:46 pm
 :silbar: arrays y ciclos....

minimo debe ser el posible maximo numero como declaración inicial, maximo debe ser el posible minimo numero como declaración inicial