Can I use a FOR cycle in my program?

0

I am doing a program that captures names and qualifications, you must capture 3 qualifications by name and then print the name along with the average of your 3 qualifications. My question is. A for loop and arrays can be used to store them in a single variable instead of several as cal1, cal2, and cal 3 and not being SOP ("Enter the qualification"); 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");
              }

   }
}
    
asked by Stephanie B Bautista 09.02.2017 в 23:13
source

1 answer

1

First of all, with a single Scanner you can get all the data, this way with declaring Scanner entrada = new Scanner(System.in); would be enough. A possible solution to your problem would be:

package clicloFor;

import java.util.Scanner;

public class Calificaciones {

public static void main(String[] args) {
    /* Declaro el Scanner para poder pedir datos */
    Scanner entrada = new Scanner(System.in);

    /* Declaro los arrays */
    String[] nombres = new String[100];
    int[] cal1 = new int[100];
    int[] cal2 = new int[100];
    int[] cal3 = new int[100];
    int i = 0;

    /* Entramos al buble principal del programa */
    boolean salir = false;
    while(!salir){
        System.out.println("Por favor introduzca la opcion deseada:");
        System.out.println("\t1) Insertar nuevo alumno");
        System.out.println("\t2) Consultar promedio de los alumnos");
        System.out.println("\t3) Salir del programa");
        int opcion = entrada.nextInt();
        entrada.nextLine(); /* Limpiamos el buffer */
        switch (opcion) {
        case 1:         /* A partir de 100 alumnos el programa fallaria asi */
            System.out.println("Introduce el nombre del alumno");
            nombres[i] = entrada.nextLine();
            System.out.println("Introduce la calificacion 1");  /* NO PUEDO USAR UN BUCLE       */
            cal1[i] = entrada.nextInt();
            System.out.println("Introduce la calificacion 2");  /* PORQUE LOS DATOS SE GUARDAN */
            cal2[i] = entrada.nextInt();
            System.out.println("Introduce la calificacion 3");  /* EN ARRAYS DISTINTOS          */
            cal3[i] = entrada.nextInt();
            float promedio = (cal1[i] + cal2[i] + cal3[i]) / 3;
            System.out.println(nombres[i].toUpperCase() + " - " + promedio);
            i++;/* Aumentamos el indice */
            break;
        case 2:
            /* Recorremos todos los alumnos actuales */
            for(int j = 0 ; j < i ; j++){
                promedio = (cal1[j] + cal2[j] + cal3[j]) / 3; /* La variable no se declara ya que existe del case 1 */
                System.out.println(nombres[j].toUpperCase() + " - " + promedio);
            }
            break;
        case 3:
            salir = true;
            break;
        default:
            System.out.println("Opcion desconocida");
            break;
        }
    }

    /* Antes de terminar el programa cerramos el Scanner */
    entrada.close();
}

}

But of course, in this example (which is like your code) you can not use a for loop since 3 different arrays are used to store the notes (in my version I keep all the notes of the students, so I could recover cal2 from a student if I want something that I think your code did not contemplate) otherwise we could see it like this:

package clicloFor;

import java.util.Scanner;

public class Calificaciones {

public static void main(String[] args) {
    /* Declaro el Scanner para poder pedir datos */
    Scanner entrada = new Scanner(System.in);

    /* Declaro los arrays */
    String[] nombres = new String[100];
    int[][] calificaciones = new int[100][3]; /* Ahora tenemos una matriz */
    int i = 0;

    /* Entramos al buble principal del programa */
    boolean salir = false;
    while(!salir){
        System.out.println("Por favor introduzca la opcion deseada:");
        System.out.println("\t1) Insertar nuevo alumno");
        System.out.println("\t2) Consultar promedio de los alumnos");
        System.out.println("\t3) Salir del programa");
        int opcion = entrada.nextInt();
        entrada.nextLine(); /* Limpiamos el buffer */
        switch (opcion) {
        case 1:         /* A partir de 100 alumnos el programa fallaria asi */
            System.out.println("Introduce el nombre del alumno");
            nombres[i] = entrada.nextLine();
            for(int j = 0 ; j < 3 ; j++){
                System.out.println("Introduce la calificacion " + j);   /* SI PUEDO USAR UN BUCLE       */
                calificaciones[i][j] = entrada.nextInt();
            }
            float promedio = (calificaciones[i][0] + calificaciones[i][1] + calificaciones[i][2]) / 3;
            System.out.println(nombres[i].toUpperCase() + " - " + promedio);
            i++;/* Aumentamos el indice */
            break;
        case 2:
            /* Recorremos todos los alumnos actuales */
            for(int j = 0 ; j < i ; j++){
                promedio = (calificaciones[i][0] + calificaciones[i][1] + calificaciones[i][2]) / 3;
                System.out.println(nombres[j].toUpperCase() + " - " + promedio);
            }
            break;
        case 3:
            salir = true;
            break;
        default:
            System.out.println("Opcion desconocida");
            break;
        }
    }

    /* Antes de terminar el programa cerramos el Scanner */
    entrada.close();
}

}

Where now since we have a matrix if we can generalize and do it with a loop, I hope it will help you!

    
answered by 09.02.2017 / 23:45
source