-3
I created this method for a validation when the user will type the option that appears on the screen:
private int menu;
private int menuImprimir;
public int getMenu()
{
return menu;
}
public void ativarStatus()
{
this.status = "Ativado";
}
public void menuImprimir()
{
System.out.println("Deseja Imprimir os dados?" +
"\n1 - Sim" +
"\n2 - Não");
this.menuImprimir = input.nextInt();
while(this.menuImprimir < 1 || this.menuImprimir > 2)
{
System.out.println("\nErro! - Digite uma opção válida");
menuImprimir();
}
switch(this.menuImprimir)
{
case 1:
imprimir();
break;
case 2:
System.out.println("Obrigado! ... ");
break;
default:
System.out.println("Opção inválida!\nDigite novamente: ");
break;
}
}
If the client type the option other than 0 and 1 it stays inside the while asking him to enter the correct option. But if the client type 3 times the wrong option and on the fourth he type the correct system is printing System.out.println("Concluído com Sucesso!");
"3 times to later exit the method, even if I put this Return..
Please provide a code that is a [mcve] so that it is possible to test.
– user28595
The method does exactly what is asked. Repeat until the user enters a valid option; in that case it exits.
– Jefferson Quesado
@Articuno I put the whole method as requested.. Note that when I call the menuImprimir() method, a message appears and stores a value in the menuImprimir variable, I type the invalid option 3x and the 3 times it enters the while and calls the menuImprimir() method again, in the 4ºx I type the right one, it jumps the while and enters the switch. So if I typed 2, it should appear message 2 finish the method. Only it appears the message and back to compare the while again, passes from while pq the option typed is 2, print again and back, repeating this process for more 2x
– Luiz Gustavo