Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created January 15, 2021 06:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/65d6463a8e6ea025ea81345aeed8e960 to your computer and use it in GitHub Desktop.
Save parzibyte/65d6463a8e6ea025ea81345aeed8e960 to your computer and use it in GitHub Desktop.
/*
____ _____ _ _ _
| _ \ | __ \ (_) | | |
| |_) |_ _ | |__) |_ _ _ __ _____| |__ _ _| |_ ___
| _ <| | | | | ___/ _` | '__|_ / | '_ \| | | | __/ _ \
| |_) | |_| | | | | (_| | | / /| | |_) | |_| | || __/
|____/ \__, | |_| \__,_|_| /___|_|_.__/ \__, |\__\___|
__/ | __/ |
|___/ |___/
____________________________________
/ Si necesitas ayuda, contáctame en \
\ https://parzibyte.me /
------------------------------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
Creado por Parzibyte (https://parzibyte.me).
------------------------------------------------------------------------------------------------
Si el código es útil para ti, puedes agradecerme siguiéndome: https://parzibyte.me/blog/sigueme/
Y compartiendo mi blog con tus amigos
También tengo canal de YouTube: https://www.youtube.com/channel/UCroP4BTWjfM0CkGB6AFUoBg?sub_confirmation=1
------------------------------------------------------------------------------------------------
*/
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
class Respuesta {
String respuesta;
boolean esCorrecta;
public Respuesta(String respuesta, boolean esCorrecta) {
this.respuesta = respuesta;
this.esCorrecta = esCorrecta;
}
}
class Pregunta {
private final String pregunta;
private final List<Respuesta> respuestas;
// Nota: si crees que agregarás más respuestas, amplía las letras
private static final String letras = "abcdefghijklmnñopqrstuvwxyz";
public Pregunta(String pregunta, List<Respuesta> respuestas) {
this.pregunta = pregunta;
this.respuestas = respuestas;
}
public Pregunta(String pregunta) {
this.pregunta = pregunta;
this.respuestas = new ArrayList<>();
}
public void agregarRespuesta(Respuesta r) {
this.respuestas.add(r);
}
public boolean respuestaCorrecta(char respuesta) {
int indice = letras.indexOf(respuesta);
if (indice == -1) {
return false;
}
return this.respuestas.get(indice).esCorrecta;
}
public boolean preguntar(int numero) {
System.out.println(numero + ". " + this.pregunta);
int indice = 0;
for (Respuesta r : this.respuestas) {
System.out.printf("%c) %s\n", letras.charAt(indice), r.respuesta);
indice++;
}
System.out.println("Elige: ");
Scanner sc = new Scanner(System.in);
char respuesta = sc.nextLine().charAt(0);
return this.respuestaCorrecta(respuesta);
}
}
class Cuestionario {
private final List<Pregunta> preguntas;
private int aciertos;
private int errores;// ¿Alguien sabe el antónimo de acierto en este contexto? yo no
public Cuestionario() {
this.preguntas = new ArrayList<>();
}
public void agregarPregunta(Pregunta p) {
this.preguntas.add(p);
}
public void preguntar() {
int numero = 1;
for (Pregunta p : this.preguntas) {
boolean acierta = p.preguntar(numero);
numero++;
if (acierta) {
System.out.println("Correcto");
this.aciertos++;
} else {
System.out.println("Incorrecto");
this.errores++;
}
}
}
public void imprimirResultados() {
int total = this.preguntas.size();
double porcentajeAciertos = (100 * (double) this.aciertos) / total;
double porcentajeErrores = (100 * (double) this.errores) / total;
System.out.printf("Total de preguntas: %d\nAciertos: %d (%.2f %%)\nErrores: %d (%.2f %%)", total, this.aciertos, porcentajeAciertos, this.errores, porcentajeErrores);
}
}
public class Main {
public static void main(String[] args) {
Cuestionario c = new Cuestionario();
c.agregarPregunta(new Pregunta("Lenguaje de programación más fácil de aprender", List.of(
new Respuesta("Java", false),
new Respuesta("Go", false),
new Respuesta("Python", true),
new Respuesta("JavaScript", false)
)));
c.agregarPregunta(new Pregunta("1 + 1", List.of(
new Respuesta("3", false),
new Respuesta("2", true),
new Respuesta("4", false)
)));
c.agregarPregunta(new Pregunta("¿NetBeans es un lenguaje de programación?", List.of(
new Respuesta("Sí", false),
new Respuesta("No", true)
)));
c.agregarPregunta(new Pregunta("Lenguajes de programación (¡Miren! alguien dijo el chiste de que HTML no es un lenguaje de programación, ríanse) que sirven para programar en la web del lado del cliente", List.of(
new Respuesta("HTML, CSS y JavaScript", true),
new Respuesta("PHP, Python y JavaScript", false),
new Respuesta("C y C++", false),
new Respuesta("Perl y Kotlin", false),
new Respuesta("Go, JavaScript y TypeScript", false)
)));
c.preguntar();
c.imprimirResultados();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment