Skip to content

Instantly share code, notes, and snippets.

@j2deme
Created May 26, 2021 20:56
Show Gist options
  • Save j2deme/e8721d734f1d4a6cae741ae61c4d3de2 to your computer and use it in GitHub Desktop.
Save j2deme/e8721d734f1d4a6cae741ae61c4d3de2 to your computer and use it in GitHub Desktop.
Agenda y Búsqueda de Cazadores
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);
/*String msj;
System.out.println("Escribe un mensaje:");
msj = teclado.nextLine();
System.out.println(msj);
System.out.printf("El mensaje mide %d caracteres.\n",
msj.length());
System.out.printf("El caracter inicial es %s y el final es %s.\n",
msj.charAt(0), msj.charAt(msj.length() - 1));
msj = msj.replaceAll(" ","-");
System.out.println(msj);*/
Vector<String> nombres = new Vector<String>();
String nombre;
String opcion;
boolean encontrado = false;
String[] cazadores = new String[3];
cazadores[0] = "Tanjiro";
cazadores[1] = "Zenitsu";
cazadores[2] = "Inosuke";
/**
* Alternativa de declaración e inicialización del arreglo
* String[] cazadores = new String[3] {"Tanjiro", "Zenitsu", "Inosuke"};
**/
do {
System.out.print("Dame un nombre: ");
nombres.add(teclado.next());
System.out.print("Desea agregar otro nombre [S/N]: ");
opcion = teclado.next();
} while (opcion.equalsIgnoreCase("s"));
System.out.println("¿A quién buscas? ");
nombre = teclado.next();
for (int i=0; i < nombres.size(); i++){
if(nombre.equalsIgnoreCase(nombres.elementAt(i))){
System.out.printf("Encontré a %s.\n", nombre);
encontrado = true;
}
}
if (!encontrado){
System.out.printf("No encontré a %s.\n", nombre);
}
for (int i=0; i < nombres.size(); i++){
for (int j=0; j < 3; j++){
if(nombres.elementAt(i).equals(cazadores[j])){
System.out.printf("%s es cazador.\n", nombres.elementAt(i));
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment