How to stop looking in an array when finding an element?

0

I have the following exercise:
A restaurant has commissioned us an application to place customers in your tables. On a table you can seat from 0 (empty table) to 4 people (full table). When a customer arrives, they are asked how many they are. For now the program is not prepared to place groups larger than 4, therefore, If a client says for example that they are a group of 6, the program will give the message "Sorry, we do not support groups of 6, make groups of 4 maximum people and try again. " For the group that arrives, always look for the first free table (with 0 people). If there are no tables free, search where there is a gap for the whole group, for example if the group is two people, you can place where there is one or two people. Initially, the tables are loaded with random values between 0 and 4. Each Once new customers feel they must show the status of the tables. Groups can not be broken even if there are enough loose holes.

The problem I have is that I look for an empty table worth, compare and enter the number of people .... at that time I was supposed to stop looking for empty tables, but the program keeps looking for empty tables and people enter , I can not find a way to get the program out of the cycle or stop looking for tables when I have already found the first ... I hope to make myself understood.

public static void main(String[] args) {
    Scanner entrada = new Scanner (System.in);

    int mesa[] = {1,2,3,4,5,6,7,8,9,10};
    int ocupacion [] = {3,2,0,2,4,1,0,2,1,1};
    int cant;
    int aux;

    do{
        System.out.println("Bienvenidos a mi restaurante");    
        System.out.println("Cuantos son?: (Introduzca -1 para salir del programa)");
        cant = entrada.nextInt();

        if (cant == -1){
            break;
        }else if (cant >4 ){
            System.out.println("Lo sentimos no admitimos grupos de:" + cant);
        } // Esto se puede utilizar mas adelante
    } while(cant>4);
    aux = cant;

    while (aux>0){
        int j = 0;
        for (int i = 0; i < 10; i++) {
            if(ocupacion [i] ==0){
                System.out.println("Por favor, sientese en la mesa numero: "+ mesa[j]);
                ocupacion [i] = cant;
                break;
            }

       /* CODIGO INCOMPLETO POR LOS MOMENTOS UwU 
            else if ((ocupacion [i] + cant) < 5){break;
                System.out.println("Lo sentimos, tendra que compartir mesa: ");
                ocupacion [i] = ocupacion [i] + cant;
                break;
            }*/
            j++;
        }
    }


    for (int i = 0; i < 10; i++) {
        System.out.print(ocupacion [i]);
    }
}
    
asked by B_BchronoFantasma 04.07.2018 в 18:28
source

2 answers

0

My dear, the break you put is just to get out of the for loop, the while always keeps iterating, so your while cycle is cycled, you can come up with many ways to finish your while, the simplest and fastest one could be when you find an empty month, put your aux variable with zero value, that way it will not take another turn and therefore, it will not read your array again,

while (aux>0){
    int j = 0;    
    for (int i = 0; i < 10; i++) {
        if(ocupacion [i] ==0){
            System.out.println("Por favor, sientese en la mesa numero: "+ mesa[j]);
            ocupacion [i] = cant;
            //Algo así, esto ya podría ser cómo lo quieras tratar
           //pero debes de cambiar el estado de esta variable para que te rompa el ciclo.                           
           aux=0;
           break;
        }

 /* CODIGO INCOMPLETO POR LOS MOMENTOS UwU 
   else if ((ocupacion [i] + cant) < 5){break;
   System.out.println("Lo sentimos, tendra que compartir mesa: ");
    ocupacion [i] = ocupacion [i] + cant;
    break;
     }*/
     j++;
    }
}
    
answered by 04.07.2018 / 19:07
source
0

You missed making me cut the second while it seems to me: 'while (aux> 0)' ... 'aux' is still a number greater than zero and the 'break' I think cuts only the 'for' cycle.

    
answered by 04.07.2018 в 18:47