Problem with an IF

0

I am learning to program in Java, and I have a problem, I have to enter 8 salary to a couple of arrangements, one in the morning and one in the afternoon, and ideally, when I already ellene an arrangement, I pass it for the other arrangement, I could share the code to help me with that ?

package prueba;
import java.util.Scanner;

public class Prueba {
    private Scanner teclado;
    private int[] Mañana,Tarde;
    private int Total;
    private int MT;
    private int HM, HT;

    public void cargar()
    {
        System.out.println("Solo se pueden registrar 8 empleados.");
        HM = 0;
        HT = 0;
        Mañana = new int[4];
        Tarde = new int[4];
        for (int i = 0; i < 8; i++) {
            teclado = new Scanner(System.in);
            System.out.println("Turno Mañana: 1 - Turno Tarde: 2 ");
            MT = teclado.nextInt();
            if (MT == 1) {
                if (HM >= 4) {
                    System.out.println("No puede ingresar mas salarios en la mañana.");
                    teclado = new Scanner(System.in);
                    System.out.println("Ingresar salario Tarde: ");
                    Tarde[i]=teclado.nextInt();
                    HT++;    
                }
                else {
                    teclado = new Scanner(System.in);
                    System.out.println("Ingresar salario Mañana: ");
                    Mañana[i]=teclado.nextInt();
                    HM++;
                }
            }else if (MT == 2) {
                if (HT > 4) {
                    System.out.println("No puede ingresar mas salarios en la tarde.");
                    teclado = new Scanner(System.in);
                    System.out.println("Ingresar salario Mañana: ");
                    Mañana[i]=teclado.nextInt();
                    HM++;
                    }
                else{
                    teclado = new Scanner(System.in);
                    System.out.println("Ingresar salario Tarde: ");
                    Tarde[i]=teclado.nextInt();
                    HT++;    


     }
        }else{
            System.out.println("nada");
        }     
    }
}

public static void main(String[] ar) {
        Prueba pv=new Prueba();
        pv.cargar();
       // pv.promedio();
        //pv.conteo();

    }

It enters the point when it identifies that the array is already full, it asks me for the value to fill the other array, but it does not leave the if cycles and it does not return to the for cycle. It's a silly program, but I do not know how to move forward, thanks for reading.

    
asked by Omar Noa 09.10.2018 в 22:52
source

1 answer

2

The problem is your index, because when i == 4 is leaving the limit of the array, change all your index variables for the int that are increasing, ie HM for the morning and HT for the afternoon.

it would be like this

public class Prueba {
private Scanner teclado;
private int[] Mañana,Tarde;
private int Total;
private int MT;
private int HM, HT;

public void cargar()
{
    System.out.println("Solo se pueden registrar 8 empleados.");
    HM = 0;
    HT = 0;
    Mañana = new int[4];
    Tarde = new int[4];
    for (int i = 0; i < 8; i++) {
        teclado = new Scanner(System.in);
        System.out.println("Turno Mañana: 1 - Turno Tarde: 2 ");
        MT = teclado.nextInt();
        if (MT == 1) {
            if (HM >= 4) {
                System.out.println("No puede ingresar mas salarios en la mañana.");
                teclado = new Scanner(System.in);
                System.out.println("Ingresar salario Tarde: ");
                Tarde[HT]=teclado.nextInt();
                HT++;    
            }
            else {
                teclado = new Scanner(System.in);
                System.out.println("Ingresar salario Mañana: ");
                Mañana[HM]=teclado.nextInt();
                HM++;
            }
        }else if (MT == 2) {
            if (HT > 4) {
                System.out.println("No puede ingresar mas salarios en la tarde.");
                teclado = new Scanner(System.in);
                System.out.println("Ingresar salario Mañana: ");
                Mañana[HM]=teclado.nextInt();
                HM++;
                }
            else{
                teclado = new Scanner(System.in);
                System.out.println("Ingresar salario Tarde: ");
                Tarde[HT]=teclado.nextInt();
                HT++;    


 }
    }else{
        System.out.println("nada");
    }     
}
}

public static void main(String[] ar) {
        Prueba pv=new Prueba();
        pv.cargar();
       // pv.promedio();
        //pv.conteo();

    }

A very important tip, never use "ñ" for variable names, since it can cause errors

    
answered by 09.10.2018 / 23:05
source