Error entering a value greater than 10 in Java

1

I made a program where the user types the name of a student and their 3 grades of the partial ones.

The grade can not be greater than 10 and if it is then it asks you to enter another grade again.

However when I enter numbers greater than 10 this one anyway stores it instead of asking me to change the number.

Another error is that when I enter the option of Consultation of averages this 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*/
      
      
      
      
      
      
    
asked by Stephanie B Bautista 10.02.2017 в 07:41
source

1 answer

1

On the line where you put 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 you decide to quit you put } while (exitmenu == 1); , but maybe you wanted to put it back while the state exitmenu is different from 1 : } while (exitmenu != 1); . I would have preferred to use a data type booleano . You use a reverse logic that could create problems in the future or confuse a reader.

The comments of the keys at the end do not correspond to their pairings.

You are wasting index 0 of the matrix by adding 1 to the position before saving the data in it. Doing the subsequent increment and starting the loops by 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 it in mind.

The conditionals that start with if (promedio[x] >= 6) { with mutually exclusive, so the second one can put it in else 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 do not use totales for anything even though you use it as a limit in the loops. .. what's the point? I've changed where you use x by totales to make sense and also avoid losing the account when you do lists interspersed with adding new students.

This is the resulting code:

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];
    String auxnombre;
    int[] calificacion_1 = new int[100];
    int[] calificacion_2 = new int[100];
    int[] calificacion_3 = new int[100];
    int[] promedio = new int[100];
    int op_menu, totales = 0;
    int x = 0, y = 0, i = 0;
    int ejecutar = 0, aux, passed = 0, flunked = 0;
    boolean repetirmenu = true;

    do {
      System.out.println("Menu de Captura de Calificaciones");
      System.out.println("Ingrese la opcion deseada");
      System.out.println("1. Captura");
      System.out.println("2. Consulta de promedios");
      System.out.println("3. Salir");
      op_menu = Integer.parseInt(br.readLine());
      switch(op_menu) {
      case 1:
        System.out.println("Capturas");
        System.out.println("Ingresa el nombre del alumno:");
        nombre[totales] = br.readLine().toUpperCase();
        do {
          System.out.println("Ingresa la primera calificacion:");
          calificacion_1[totales]=Integer.parseInt(br.readLine());
          if (calificacion_1[totales] > 11 || calificacion_1[totales] < 1) {
            System.out.println("Favor de Ingresar valores no mayor a 10");
          }
        } while (calificacion_1[totales] > 11 || calificacion_1[totales] < 1);

        do {
          System.out.println("Ingresa la segunda calificacion:");
          calificacion_2[totales] = Integer.parseInt(br.readLine());
          if(calificacion_2[totales] > 11 || calificacion_2[totales] < 1) {
            System.out.println("Favor de Ingresar valores no mayor a 10");
          }
        } while (calificacion_2[totales] > 11 || calificacion_2[totales] < 1);
        do {
          System.out.println("Ingresa la tercera calificacion:");
          calificacion_3[totales] = Integer.parseInt(br.readLine());
          if(calificacion_3[totales] > 11 || calificacion_3[totales] < 1) {
            System.out.println("Favor de Ingresar valores no mayor a 10");
          }
        } while (calificacion_3[totales] > 11 || calificacion_3[totales] < 1);
        promedio[x] = (calificacion_1[totales] + calificacion_2[totales] + calificacion_3[totales]) / 3;
        if (promedio[totales] >= 6) {
          passed++;
        } else {
          flunked++;
        }
        totales++;
        break;
      case 2:
        System.out.println("Consultar Promedios");
        for(x = 0; x < totales; x++) {
          for(y = 0; 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;
            }
          }
        }
        System.out.print(totales);
        for(x = 0; x < totales; x++) {
          System.out.println("Nombre del alumno o alumna: " + nombre[x] +
            " Promedio Final: " + promedio[x]);
        }
        break;
      case 3:
        System.out.println("Salida del programa");
        System.out.println("Estos son los resultados");
        System.out.println("No. de Reprobados: " + flunked);
        System.out.println("No. de Aprobados: " + passed);
        repetirmenu = false;
        break;
      default:
        System.out.println("Opción errónea");
      }
    } while (repetirmenu);
  }
}
    
answered by 10.02.2017 / 08:31
source