I am new to Java and I am trying to do a program that asks the user for:
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.