Created
May 24, 2021 18:08
-
-
Save j2deme/6864cdf40fd2b97d6704ae6d089496ae to your computer and use it in GitHub Desktop.
Uso de Vectores y Menú
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.j2deme; | |
| import java.util.Scanner; | |
| import java.util.Vector; | |
| public class Main { | |
| public static void main(String[] args) { | |
| Scanner teclado = new Scanner(System.in); | |
| Vector<Integer> numeros = new Vector<Integer>(); | |
| int opcion = 0; | |
| do { | |
| System.out.println("1. Añadir un número"); | |
| System.out.println("2. Imprimir números"); | |
| System.out.println("3. Imprimir números a la inversa"); | |
| System.out.println("0. Salir"); | |
| System.out.print("Elige una opción: "); | |
| opcion = teclado.nextInt(); | |
| switch (opcion){ | |
| case 1: | |
| System.out.print("Ingresa un número: "); | |
| int numero = teclado.nextInt(); | |
| numeros.add(numero); | |
| break; | |
| case 2: | |
| for (int i=0; i < numeros.size(); i++){ | |
| if(i == numeros.size() - 1){ | |
| System.out.printf("%d.\n", numeros.get(i)); | |
| } else { | |
| System.out.printf("%d,", numeros.get(i)); | |
| } | |
| } | |
| break; | |
| case 3: | |
| //Reutilizar lógica del caso 2 y la impresión invertida de arreglos | |
| // estáticos | |
| break; | |
| default: | |
| System.out.println("Opción no válida"); | |
| } | |
| } while(opcion != 0); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment