Math Class - Java

0

When I run the program it gives me a minimum value = 0, the maximum value does it correctly, HELP!

import java.util.Scanner;
import java.lang.*;
class EstadisticaBasica{

    private int numero;
    private static Scanner sc = new Scanner(System.in);

    public void setNumero(){
        numero=sc.nextInt();
    }

    public void valorMaximo(){

        int i;
        int cont=0;
        for(i=0;i<10;i++){

            System.out.print("Introduce un valor: ");
            setNumero();

            cont=Math.max(numero, cont);    
        }

        System.out.println("El valor maximo de los 10 valores es: "+cont);
    }

    public void valorMinimo(){

        int cont=0;
        int i;
        for(i=0;i<10;i++){

            System.out.print("Introduce un valor: ");
            setNumero();

            cont=Math.min(numero, cont);
        }

            System.out.println("El valor minimo de los 10 valores es: "+cont);
    }

    public static void main(String[] args) {
        EstadisticaBasica a = new EstadisticaBasica();
        a.valorMaximo();
        a.valorMinimo();
    }
}

When I run the program it gives me a minimum value = 0, the maximum value does it correctly, HELP!

    
asked by David 01.11.2017 в 15:10
source

2 answers

0

You are comparing in cont=Math.min(numero, cont) and cont initialize it to 0, therefore it is the minimum value.

You should take the first number they give you and put it as the minimum and then compare the following with that, if you compare it with 0, this will be the smallest possible.

I would do something like that

Initialize first to true at the beginning

import java.util.Scanner;
import java.lang.*;
class EstadisticaBasica{

private int numero;
private boolean first =true;
private static Scanner sc = new Scanner(System.in);

public void setNumero(){
    numero=sc.nextInt();
}

public void valorMaximo(){

    int i;
    int cont=0;
    for(i=0;i<10;i++){

        System.out.print("Introduce un valor: ");
        setNumero();

        cont=Math.max(numero, cont);    
    }

    System.out.println("El valor maximo de los 10 valores es: "+cont);
}

public void valorMinimo(){

    int cont=0;
    int i;
    int minimoActual;
    for(i=0;i<10;i++){

        System.out.print("Introduce un valor: ");
        setNumero();

        if (first){
            setNumero();
            minimoActual = numero;
            first=false;
        } else {
            minimoActual=Math.min(numero, minimoActual);
        }
    }

        System.out.println("El valor minimo de los 10 valores es: "+cont);
}

public static void main(String[] args) {
    EstadisticaBasica a = new EstadisticaBasica();
    a.valorMaximo();
    a.valorMinimo();
}
}
    
answered by 01.11.2017 / 15:18
source
1

I agree with the principle of @Orz's response, that is, just make the comparison of Math.Min from the second number.

But I do not like the use of global variables. There is no need to do this and can easily introduce difficult defects to find. (See: Why is it considered bad practice to use global variables? ).

Something of this style seems much simpler and safer:

public void valorMinimo() {
    int cont = 0;
    for (int i = 0; i < 10; i++) {
        System.out.print("Introduce un valor: ");
        int numero = sc.nextInt();

        cont = i == 0 ? numero : Math.min(numero, cont);
    }

    System.out.println("El valor minimo de los 10 valores es: " + cont);
}    

And although your method for the maximum seems to work well, it would be worthwhile to follow the same pattern in that method as well. Because if the user enters you pure negative numbers, for example, then you will also have a problem.

    
answered by 01.11.2017 в 15:41