I'm doing an exercise in Java that asks for registration, salary, tax and exit. I used switch
case for this, of course and so far so good.
However when I run and register for Java I go through case 1
and even before I put the name of the individual it already jumps to case 2
.
I put break
, I tried to do with if
so I do not have to create another case, however it does the same thing when I have if
inside case 1
.
I can not understand why he does not wait for the guy to enter the name and then move on to the next code.
I'll put the code below:
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;
}
}
I honestly have no idea what's going on and how I can make it work. Yes, in break choiceProgram
(labeled loops) gives error with or without this. I'm going to put an image of the console: