I am making a program that captures names and grades, you must capture 3 grades per name and then print the name along with the average of its 3 grades. My question is. You can use a for loop and arrays to store them in a single variable instead of several like cal1, cal2, and cal 3 and not be putting SOP ("Enter the grade"); three times?
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.util.*;
public class Calificaciones{
public static void main (String[]args){
Scanner entrada;
entrada=new Scanner(System.in);
Scanner sc=new Scanner(System.in);
Scanner entrada2;
entrada2=new Scanner(System.in);
String[]nombre= new String[100];
int opcion_menu=0; int ejecutar=0;
int salir=0;
int promedio;
int otro=0;
int auxpromedio;
int otro_calif;
int a=0;
int x=0;
int calificacion[]= new int[100];
int segunda[]=new int[100];
int tercera[]=new int[100];
do{
System.out.println("Elija la opcion deseada \n [1].Captura \n [2].Consulta \n [3].Salida ");
opcion_menu=entrada.nextInt();
if(opcion_menu>3)
{
System.out.println("Favor de ingresar otra opcion del 1 al 3");
}
}while(opcion_menu>3 || opcion_menu<1);
if(opcion_menu==1)
{
a++;
System.out.println("Ingresa el nombre del alumno:");
nombre[a]=sc.nextLine().toUpperCase();
do{
System.out.println("Ingresa la calificacion:");
calificacion[a]=entrada.nextInt();
if(calificacion[a]>=11)
{
System.out.println("ERROR \n Ingresa un valor no mayor de 10");
}
}while(calificacion[a]>10 || calificacion[a]<1);
}
System.out.println("Deseas Ingresar otros datos? \n 1.Si \n 2.No");
if(opcion_menu==3)
{
System.out.println("¿Desea salir del programa? 1. Si 2. No");
salir=entrada.nextInt();
System.exit(0);
System.out.println("Gracias por usar el programa");
}
}
}
First of all, with a single Scanner you can get all the data, this way declaring
Scanner entrada = new Scanner(System.in);
would be enough. A possible solution to your problem would be:But of course, in this example (which is like your code) you can't use a for loop since 3 different arrays are used to store the grades (in my version I keep all the students' grades, so I could retrieve cal2 of a student if I want something that I think your code did not contemplate) on the other hand we could see it like this:
Where now since we have a matrix if we can generalize and do it with a loop, I hope it helps you!