3
How do I create a loop with options in Java? For example: "type 1 to save and 2 to delete". And when doing the commands for each option, go back to : "type 1 to save and 2 to delete".
import java.io.*;
public class CadastroPessoa{
public static void main(String[] args) throws IOException{
//burocracia para leitura de teclado
InputStream entradaSistema = System.in;
InputStreamReader leitor = new InputStreamReader(entradaSistema);
BufferedReader leitorEntrada = new BufferedReader(leitor);
String entradaTeclado;
//instanciando objetos do sistema
ControlePessoa umControle = new ControlePessoa();
System.out.println("Digite 1 para adicionar pessoa. Digite 2 para remover pessoa. Digite 3 para pesquisar uma pessoa. Digite 4 para sair.");
entradaTeclado = leitorEntrada.readLine();
String Opcao = entradaTeclado;
String opcao1 = "1";
String opcao2 = "2";
String opcao3 = "3";
String opcao4 = "4";
while (!Opcao.equals(opcao4)){
if (Opcao.equals(opcao1)){
//interagindo com usuário
System.out.println("Digite o nome da Pessoa:");
entradaTeclado = leitorEntrada.readLine();
String umNome = entradaTeclado;
System.out.println("Digite o telefone da Pessoa:");
entradaTeclado = leitorEntrada.readLine();
String umTelefone = entradaTeclado;
//adicionando uma pessoa na lista de pessoas do sistema
Pessoa umaPessoa = new Pessoa(umNome, umTelefone);
String mensagem = umControle.adicionar(umaPessoa);
System.out.println(mensagem);
}
if (Opcao.equals(opcao2)){
System.out.println("Digite o nome da pessoa que você quer remover:");
entradaTeclado = leitorEntrada.readLine();
String umNome = entradaTeclado;
//removendo uma pessoa na lista de pessoas do sistema
}
if (Opcao.equals(opcao3)){
System.out.println("Digite o nome da pessoa que você quer pesquisar:");
entradaTeclado = leitorEntrada.readLine();
String umNome = entradaTeclado;
//buscando pessoa na lista de pessoas
}
}
//conferindo saída
System.out.println("=================================");
System.out.println("=)");
}
}
Hello Rafael. Have you tried anything? Do you have any specific difficulty? It would be nice to share in your question what you have tried to do.
– Luiz Vieira
Well, I put to read from the keyboard a string that in the case was 1 to 4 and was comparing this string with strings defined from 1 to 4. If they were equal, it would execute the part of the corresponding code. But I don’t know at the end of that go back to the beginning to ask for a new string to be typed.
– Rafael
This question may sound silly, but your reading code is inside a loop (
while
, for example)? See, you’ll more easily get help from the community if you improve your question a little bit mainly by including the code you’ve already done. Makes it easier to understand its difficulty and stimulates the crowd to respond. :)– Luiz Vieira
@Luizvieira sorry for the disorganization
– Rafael
Rafael, you must edit your question and put the code there, not in a comment. In fact, put the whole code (in your comment, for example, there is a variable called
leitorEntrada
that is not defined).– Luiz Vieira
@Luizvieira now I put the whole code in my question, thank you since already for your help and attention.
– Rafael
I hope the answers help you, but see that your question is very specific (that is, a difficulty you have with the logic of programming) and so you can receive negative votes from the community. If that happens, don’t be discouraged from participating here. Try only in the future to ask the questions in as much detail as possible so that the community is really able to help you. :)
– Luiz Vieira