简体   繁体   中英

How can I get the min and max value of the user input? (Java)

I am new to Java and I am trying to do a program that asks the user for:

  1. Number of students (it has to be from 1-35). I managed to do this right.
  2. Grade per each student available (from 0-100). I managed to do this right, BUT if someone introduces a for example 200, the system will notify that is not allowed but the variable will still affect the average of the grades (how can i delete the last inserted variable?).
  3. And last I want to give an average which I managed to do, but I can't find a way to show the maximum grade and the minimum grade.

public class ExtraClase {
    public static void main(String[] args) {
        Estudiantes estudiantes = new Estudiantes();
        estudiantes.numeroEstudiantes();
    }
}

public class Estudiantes {
    public void numeroEstudiantes() {
    System.out.print("Introduzca numero de estudiantes: ");
    Scanner cantidad = new Scanner(System.in);
    int number = cantidad.nextInt();
    int i=1;
    int total=0;
    int promedio;
    if(number>=1 && number<=35){
        while(i<=number){
           System.out.print("Ingrese la nota del 1-100: ");
           Scanner nota = new Scanner(System.in);
           int note = nota.nextInt();
           if(note>=1 && note<=100) {
           }else {
               //como elimino la ultima nota introducida
               System.out.println("Ingrese valor entre 1 y 100.");
               number++;
           }
              total=total+note;
              i++;
        }
    }else {
           System.out.println("Digite numero entre 1 y 35.");
    } 
    promedio=total/number;
    System.out.println("El promedio de notas es: "+promedio);
    System.out.println("La nota mas alta es: ");
    System.out.println("La nota mas baja es: ");
    } 
}
    if(note>=1 && note<=100) {
              total=total+note;
              i++;

     }else {
        //como elimino la ultima nota introducida
        System.out.println("Ingrese valor entre 1 y 100.");
        //there's no need to increment number here, just don't increment i if a variable has invalid value
     }

2: You're incrementing the total outside the if statement that checks whether it's valid. Therefore your invalid value of 200 isn't excluded from it.

3: You'll need to keep track of the max and min in separate variables.

You need to move the lines to add to total and increment i into the first part of if(note>=1 && note<=100) . Also you can keep track of max/min by adding another 2 variables:

public class Estudiantes {
    public void numeroEstudiantes() {
        System.out.print("Introduzca numero de estudiantes: ");
        Scanner cantidad = new Scanner(System.in);
        int number = cantidad.nextInt();
        int i=1;
        int total=0;
        //added min/max
        int baja=-1;
        int alta=-1;
        int promedio;
            if(number>=1 && number<=35){
            while(i<=number){
                System.out.print("Ingrese la nota del 1-100: ");
                Scanner nota = new Scanner(System.in);
                int note = nota.nextInt();
                if(note>=1 && note<=100) {
                    //check for min
                    if(note<baja || baja==-1){
                        baja = note;
                    }
                    //check for max
                    if(note>alta || alta==-1){
                        alta = note;
                    }
                    //moved from below
                    total=total+note;
                    i++;
                } else {
                    //como elimino la ultima nota introducida
                    System.out.println("Ingrese valor entre 1 y 100.");
                    number++;
                }
            }
        } else {
            System.out.println("Digite numero entre 1 y 35.");
        }
        promedio=total/number;
        System.out.println("El promedio de notas es: "+promedio);
        System.out.println("La nota mas alta es: " + alta);
        System.out.println("La nota mas baja es: " + baja);
    }
}

Create 2 more variables

//for your case if value is between 0 and 100
int min = 101;
int max = 0;

if(note>=1 && note<=100) {
          total=total+note;
          i++;

    //checking minimum value
    if(note < min)
       min = note;

   //checking maximum value
    if(note > max)
       max = note;

 }else {
    System.out.println("Enter integer between 1 and 100");
    //not valid no.
 }

In the end print min and max variable

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM