I made a program where the user enters the name of a student and their 3 midterm grades.
The rating cannot be greater than 10 and if it is then it asks you to enter another rating again.
However when I enter numbers greater than 10 it still stores it instead of asking me to change the number.
Another error is that when I enter the Consult averages option, it returns me to the main menu instead of giving me the averages.
import java.io.*;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import javax.swing.*;
public class ExamenCalificaciones{
public static void main (String[]args) throws IOException
{
BufferedReader br=new BufferedReader (new InputStreamReader(System.in));
String []nombre= new String [100];
int op_menu;
int []calificacion_1= new int[100];
int []calificacion_2= new int[100];
int []calificacion_3= new int[100];
int []promedio= new int[100];
int totales=1;
int x=0;
int y=0;
int i=0;
int ejecutar=0;
int aux;
int exitmenu=0;
String auxnombre;
int passed=0;
int flunked=0;
do{
System.out.println("Menu de Captura de Calificaciones");
System.out.println("Ingrese la opcion deseada \n 1.Captura \n 2.Consulta de promedios \n 3.Salir");
op_menu=Integer.parseInt(br.readLine());
if(op_menu==1)
{
System.out.println("Capturas");
System.out.println("Ingresa el nombre del alumno:");
x=x+1;
nombre[x]=br.readLine();
nombre[x]=nombre[x].toUpperCase();
do
{
System.out.println("Ingresa la primera calificacion:");
calificacion_1[x]=Integer.parseInt(br.readLine());
if(calificacion_1[x]>11 || calificacion_1[x]<1)
{
System.out.println("Favor de Ingresar valores no mayor a 10");
}
}while(calificacion_1[x]>11 || calificacion_1[x]<1);
do
{
System.out.println("Ingresa la segunda calificacion:");
calificacion_2[x]=Integer.parseInt(br.readLine());
if(calificacion_2[x]>11 || calificacion_2[x]<1);
{
System.out.println("Favor de Ingresar valores no mayor a 10");
}
}while(calificacion_2[x]>11 || calificacion_2[x]<1);
do{
System.out.println("Ingresa la tercera calificacion:");
calificacion_3[x]=Integer.parseInt(br.readLine());
if(calificacion_3[x]>11 || calificacion_3[x]<1)
{
System.out.println("Favor de Ingresar valores no mayor a 10");
}
}while(calificacion_3[x]>11|| calificacion_3[x]<1);
promedio[x]=(calificacion_1[x]+calificacion_2[x]+calificacion_3[x])/3;
if(promedio[x]>=6)
{
passed=passed+1;
}
if(promedio[x]<6)
{
flunked=flunked+1;
}
exitmenu=1;
}
if(op_menu==2)
{
System.out.println("Consultar Promedios");
for(x=1; x<totales; x++)
{
for(y=1; y<totales; y++)
{
if(promedio[y]<promedio[y+1])
{
aux=promedio[y];
promedio[y]=promedio[y+1];
promedio[y+1]=aux;
auxnombre=nombre[y];
nombre[y]=nombre[y+1];
nombre[y+1]=auxnombre;
}
}
}
exitmenu=1;
System.out.print(totales);
for(x=1; x<totales; x++)
{
System.out.println("Nombre del alumno o alumna: "+nombre[x]+" Promedio Final: "+promedio[x]);
}
}
if(op_menu==3)
{
System.out.println("Salida del programa \n Estos son los resultados");
System.out.println("No. de Reprobados: "+flunked);
System.out.println("No. de Aprobados: "+passed);
}
}while(exitmenu==1);
}/*cierre del if menu 2*/
}/*cierre de if opcion_menu*/
In the line where it says
if(calificacion_2[x]>11 || calificacion_2[x]<1);
you have a semicolon at the end that prevents the execution of the block{}
.On the other hand, the loop you use to repeat the menu until it decides to exit you put
} while (exitmenu == 1);
, but maybe you wanted to repeat while the stateexitmenu
is different from1
:} while (exitmenu != 1);
. I would have preferred to use a data typebooleano
. You use reverse logic that could create problems for you in the future or confuse a reader.The comments of the keys at the end do not correspond to their pairings.
You are wasting the index 0 of the array by adding 1 to the position before saving the data to it. Doing the post increment and starting the loops at index 0 fixes that problem.
The calculation
promedio[x] = (calificacion_1[x] + calificacion_2[x] + calificacion_3[x]) / 3;
is done with integer arithmetic, keep that in mind.Conditionals that start
if (promedio[x] >= 6) {
with are mutually exclusive, so you can put the second one in itelse
and not do two checks.You use
x
to keep the index the next student, but then you reuse it as a variable in the iterations that calculate averages in option 2, and then you don't use ittotales
at all even though you use it as a limit in the loops... what's the point? I have changed where you usex
tototales
to make sense of it and also prevent me from losing count when you make lists interspersed with adding new students.This is the resulting code: