Skip to content

Instantly share code, notes, and snippets.

@j2deme
Created May 24, 2021 18:08
Show Gist options
  • Save j2deme/6864cdf40fd2b97d6704ae6d089496ae to your computer and use it in GitHub Desktop.
Save j2deme/6864cdf40fd2b97d6704ae6d089496ae to your computer and use it in GitHub Desktop.
Uso de Vectores y Menú
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