2
I am doing an exercise in Java that asks to register, salary, tax and leave. I used a switch
case for such, clear and so far so good.
However when I run and put to register Java travels the case 1
and before I even put the name of the individual it already jumps to the case 2
.
I put break
, I tried to do with a if
not having to create another case, however the same thing happens when you have the if
within the case 1
.
I can’t understand why he doesn’t wait for the guy to give his name so after that it goes to the next code.
I’ll put the code down:
while(true) {
System.out.println("Escolha \"1\" para cadastro; \"2\" para informar salário; \"3\" para calcular imposto; \"0\" para sair!");
choice = scan.nextInt();
choiceProgram: //LabeledLoops
switch(choice) {
case 1:
while(true) {
System.out.println("Informe o nome do indivíduo: ");
listaFuncionarios.add(scan.nextLine());
break choiceProgram;
}
case 2:
System.out.println("Informe o nome: ");
nome = scan.nextLine();
nome = nome.toLowerCase();
for(int i = 0; i < listaFuncionarios.size(); i++) {
if(listaFuncionarios.equals(nome)) {
indice = i;
status = true;
break;
}else if((i + 1) == listaFuncionarios.size() && status == false) {
System.out.println("Nome não encontrado!");
break choiceProgram;
}
}
System.out.println("Informe o salário de " + listaFuncionarios.get(indice));
listaSalario.set(indice, scan.nextDouble());
break choiceProgram;
case 3:
System.out.println("Informe o nome do indivíduo que deseja calcular o imposto sobre salário: ");
nome = scan.nextLine();
nome = nome.toLowerCase();
for(int i = 0; i < listaFuncionarios.size(); i++) {
if(listaFuncionarios.equals(nome)) {
indice = i;
valor = listaSalario.get(indice);
status = true;
break;
}else if((i + 1) == listaFuncionarios.size() && status == false) {
System.out.println("Nome não encontrado!");
break choiceProgram;
}
}
System.out.println(listaFuncionarios.get(indice) + " recebe " + listaSalario.get(indice) + "\nCalculando o desconto do imposto, seu salário final é: " + Operacoes.Imposto(valor));
break choiceProgram;
case 0:
System.out.println("O programa será finalizado!");
break mainProgram;
}
}
Honestly I have no idea what’s going on and how I can make it work. Yes, no break choiceProgram
(labeled loops) gives error with or without it. I will put an image of the console:
I believe I should have one
break
right outside thewhile
in the first case.– viana