Skip to content

Instantly share code, notes, and snippets.

@brunogamacatao
Created March 12, 2024 20:35
Show Gist options
  • Save brunogamacatao/884c53b3dc18bcd6a12f7479085cd5af to your computer and use it in GitHub Desktop.
Save brunogamacatao/884c53b3dc18bcd6a12f7479085cd5af to your computer and use it in GitHub Desktop.
Exemplo de como implementar um menu em modo texto
// Esse código deve vir dentro do método run
Scanner teclado = new Scanner(System.in);
while (true) { // O LOOP DO MENU
System.out.println("╔════════════════════════════╗");
System.out.println("║ MENU DE OPÇÕES ║");
System.out.println("╠════════════════════════════╣");
System.out.println("║ 1. Adicionar um contato ║");
System.out.println("║ 2. Listar os contatos ║");
System.out.println("║ 3. Sair ║");
System.out.println("╚════════════════════════════╝");;
System.out.println("╭───────────────────────────╮");
System.out.println("│ Digite a opção desejada: │");
System.out.println("╰───────────────────────────╯");
int opcao = -1;
try {
opcao = Integer.parseInt(teclado.nextLine());
} catch (NumberFormatException ex) {
System.out.println("╭──────────────────────────────────────────╮");
System.out.println("│ A opção digitada não é um número válido! │");
System.out.println("╰──────────────────────────────────────────╯");
continue;
}
if (opcao == 1) { // adicionar um contato
// TODO IMPLEMENTAR A OPÇÃO 1
} else if (opcao == 2) { // listar os contatos
// TODO IMPLEMENTAR A OPÇÃO 2
} else if (opcao == 3) { // sair
break; // o BREAK sai do laço
} else {
System.out.println("╭──────────────────────────────────────────╮");
System.out.println("│ Opção inválida ! │");
System.out.println("╰──────────────────────────────────────────╯");
}
} // FIM DO LOOP DO MENU
System.out.println("Obrigado por utilizar a aplicação !");
teclado.close();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment