Have a loop run again

Asked

Viewed 123 times

2

Code:

while (digita != 4){
    System.out.println("Digite o o número referente ao tamanho da(s) pulseira(s)\n1.Pequena (17cm) - R$180,00\n2.Média (18cm) - R$200,00\n3.Grande (20cm) - R$240,00\n4.Prosseguir para escolha de pingentes.");
    digita = in.nextInt();
    switch(digita){
        case 1:
        valor += 180;
        tamanhopulseira = 17;
        quantidadepequena += 1;
        break;
        case 2:
        valor += 200;
        tamanhopulseira = 18;
        quantidademedia += 1;
        break;
        case 3:
        valor += 240;
        tamanhopulseira = 20;
        quantidadegrande += 1;
        break;
    }
    System.out.println("Total: " +valor);
}
if(digita==4 && tamanhopulseira == 0){
    System.out.println("Você deve escolher pelo menos uma pulseira de qualquer tamanho!");
}

Well, in this code when the user chooses option 4 without having chosen any of the other options, the program sends an error message, but how do I next to this error option it sends the user back to the while?

  • Do you have any answers you have solved? Can you accept any of them?

2 answers

3

You can do it like this:

while (digita != 4) {
    System.out.println("Digite o o número referente ao tamanho da(s) pulseira(s)\n1.Pequena (17cm) - R$180,00\n2.Média (18cm) - R$200,00\n3.Grande (20cm) - R$240,00\n4.Prosseguir para escolha de pingentes.");
    digita = in.nextInt();
    switch(digita) {
        case 1:
            valor += 180;
            tamanhopulseira = 17;
            quantidadepequena += 1;
            break;
        case 2:
            valor += 200;
            tamanhopulseira = 18;
            quantidademedia += 1;
            break;
        case 3:
            valor += 240;
            tamanhopulseira = 20;
            quantidadegrande += 1;
            break;
        case 4:
            if (tamanhopulseira == 0) {
                System.out.println("Você deve escolher pelo menos uma pulseira de qualquer tamanho!");
                digita = 0;
            }
        default:
            System.out.println("Você deve escolher de 1 à 4");
    }
    System.out.println("Total: " +valor);
}

I put in the Github for future reference.

You are checking to see if any bracelet has been selected if you type 4 and do not leave the loop until at least one of them has been chosen. And also keep in the loop if something invalid is typed.

  • In fact this your code displays this message for any value other than 1,2,3 or 4. In my case I wanted this message to appear when the user tried to proceed for direct payment without choosing any bracelet understands?

  • I get it. What happens if one type 5? or 40? Or "four"? We’re probably missing what you want because you’re doing something wrong in the rest of the code. Probably all the code is wrong and not just this part. It would be easier for you to define the problem accurately.

  • For this problem of another value being typed you are correct we can use the default. But what if the person tries to go straight to the payment? how do I display this same message and show the options again?

  • What determines that she wants to go straight for payment and that she can’t go? And when she can go straight for payment?

  • In this loop one will choose how many bracelets she will want, can be 1 small, 1 small and 1 large, 2 of each and as soon as she has just chosen option 4 sends her for choice of pendants. Sorry I misspelled in the previous comment.

  • I don’t know if this is the best way to do it but I also don’t know if I understand what you want to do. I will consider that tamanhopulseira starts with zero. I don’t know if this solves everything you want and need but with the change, I think it solves the main one. Something tells me this bracelet size control isn’t right, but I might not be getting what it’s for. Since I imagine this is an exercise it won’t cause many problems, but in real code this would be a maintenance nightmare.

Show 1 more comment

0

Put a while around everything, which only comes out when a boolean variable is set, and just put true on that variable when all is filled in correctly:

boolean opcaoEscolhida = false;
while (!opcaoEscolhida){
    while (digita != 4){
        System.out.println("Digite o o número referente ao tamanho da(s) pulseira(s)\n1.Pequena (17cm) - R$180,00\n2.Média (18cm) - R$200,00\n3.Grande (20cm) - R$240,00\n4.Prosseguir para escolha de pingentes.");
        digita = in.nextInt();
        switch(digita){
            case 1:
              valor += 180;
              tamanhopulseira = 17;
              quantidadepequena += 1;
              opcaoEscolhida = true;
              break;
            case 2:
              valor += 200;
              tamanhopulseira = 18;
              quantidademedia += 1;
              opcaoEscolhida = true;
              break;
            case 3:
              valor += 240;
              tamanhopulseira = 20;
              quantidadegrande += 1;
              opcaoEscolhida = true;
              break;
        }
        System.out.println("Total: " +valor);
    }
    if(!opcaoEscolhida){
        System.out.println("Você deve escolher pelo menos uma pulseira de qualquer tamanho!");
    }
}
  • I tested it and the message fell into an infinite loop, it just keeps showing :/

  • Really... you have to reset the variable value digita so that the while that interacts with the user between again.

  • Now that I understand the code better, the option 4 would be to leave without choosing any of the options... but before the user would have chosen one, being possible to choose several if so wish.

  • I edited to reflect this scenario.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.