Informática, perguntado por Jujubay, 10 meses atrás

Exercicio de java com ArrayList podem me ajudar?

Criar um ArrayList de String chamado telefones.
Criar um menu onde o usuário pode escolher entre adicionar telefone, remover telefone, listar telefones e Sair. No listar mostre a posição do telefone no array, isto facilitará para remover o telefone passando a posição. Informe ao usuário caso ele digite opções inválidas.

Exemplo:

Escolha uma opção:
(a) adicionar telefone,
(r) remover telefone
(l) listar telefones
(s) Sair.

Soluções para a tarefa

Respondido por edaframp7vdlr
2
import java.util.ArrayList;import java.util.List;import java.util.Scanner;
public class Agenda {    static List<String> telefones = new ArrayList<>();    public static void main(String[] args) {        boolean continuar = true;        while (continuar) {            System.out.println("Escolha uma opção: ");            System.out.println("(a) Adicionar Telefone");            System.out.println("(r) Remover Telefone");            System.out.println("(l) Listar Telefones");            System.out.println("(s) Sair");            char op = new Scanner(System.in).next().charAt(0);            System.out.println("---------------------------------");            switch(op) {                case 's' : continuar=false; break;                case 'a' : adicionar();break;                case 'r' : remover();break;                case 'l' : listar();break;                default : {                    System.out.println("Opção Inválida!");                    break;                }            }            System.out.println("---------------------------------");        }    }    static void adicionar () {        System.out.print("Telefone: ");        String telefone = new Scanner(System.in).nextLine();        telefones.add(telefone);        System.out.println("Adicionado com sucesso!");    }    static void remover () {        System.out.print("Posição: ");        int pos = new Scanner(System.in).nextInt();        telefones.remove(telefones.get(pos));    }    static void listar () {        if (telefones.isEmpty()) {            System.out.println("Lista Vazia, por favor adicione telefones");        } else {            int pos=0;            for (String tel : telefones) {                System.out.println("~~~~~~~~~~~~~~~~~~~~~~");                System.out.println("Posição: "+(pos++));                System.out.println("Telefone: "+tel);                System.out.println("~~~~~~~~~~~~~~~~~~~~~~");            }        }    }}
Perguntas interessantes