Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

209
Visualizações
¿Cómo hago un bucle solo para un "caso" específico en el interruptor/caso?

Utilicé switch/case para imprimir las ganancias anuales dependiendo de la cuenta bancaria que el usuario elija y quiero hacer este programa, cada vez que el usuario ingrese una entrada inapropiada, imprima el mensaje de "entrada inapropiada" y permita que el usuario vuelva a ingresar la cuenta bancaria que quiero elegir Pero, actualmente mi código solo imprime el mensaje de "entrada inapropiada". ¿Cómo puedo arreglarlo?

 public static void main(String[] args) throws Exception { //initialize variables String bank = ""; double money; //asks the user to type the amount of money Scanner myinput = new Scanner (System.in); System.out.print("Please enter the amount of money you want in the bank: "); //if the input is inappropriate, ensures the user to type the correct input while (true) { try { money = myinput.nextDouble(); break; } catch (Exception e) { System.out.println("This is inappropriate. Please enter the amount of money you want in the bank: "); myinput.nextLine(); } } //asks user to type the type of account they want System.out.println("Please enter the type of account you want: "); bank = myinput.next(); //print yearly earnings depending on the type of account that user chose switch (bank) { case "A" , "C": //if the user enters A or C, 1.5% of money will be yearly earnings System.out.println("You can make $" + (Math.round((0.015 * money)*100)) / 100.0); break; case "B": //if the user enters A or C, 2% of money will be yearly earnings System.out.println("You can make $" + (Math.round((0.02 * money)*100)) / 100.0); break; case "X": //if the user enters A or C, 5% of money will be yearly earnings System.out.println("You can make $" + (Math.round((0.05 * money)*100)) / 100.0); break; default: System.out.println ("This is inappropriate input. Please enter the type of account you want"); bank = myinput.nextLine(); } //closing scanner myinput.close(); }//end main
over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Puede tener una variable booleana para salir del bucle while. Flag se establece en false en switch/case porque while(false) no iterará más.

 boolean flag = true; while(flag){ switch(bank){ case "A","C": System.out.println("You can make $" + (Math.round((0.015 * money)*100)) / 100.0); flag = false; //put this in all case. break; ... } }
over 4 years ago · Santiago Trujillo Relatório

0

package com.javatutorial; import java.util.Scanner; class Main{ public static void main(String[] args) throws Exception { String bank = ""; double money = 0; boolean valid = false; Scanner myinput = new Scanner (System.in); System.out.print("Please enter the amount of money you want in the bank: "); while (true) { try { money = myinput.nextDouble(); break; } catch (Exception e) { System.out.println("This is inappropriate. Please enter the amount of money you want in the bank: "); myinput.next(); } } do { System.out.println("Please enter the type of account you want: "); bank = myinput.next(); switch (bank) { case "A": case "C": System.out.println("You can make $" + (Math.round((0.015 * money) * 100)) / 100.0); valid = true; break; case "B": System.out.println("You can make $" + (Math.round((0.02 * money) * 100)) / 100.0); valid = true; break; case "X": System.out.println("You can make $" + (Math.round((0.05 * money) * 100)) / 100.0); valid = true; break; default: System.out.println("This is inappropriate input. Please enter the type of account you want"); } }while( !valid); } }
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda