Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Last active February 20, 2018 06:14
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/b7a69bf6e9ef2250a4da76d2b2a416c3 to your computer and use it in GitHub Desktop.
Save parzibyte/b7a69bf6e9ef2250a4da76d2b2a416c3 to your computer and use it in GitHub Desktop.
import java.util.Scanner;
public class Preguntas {
public static void main(String[] args) {
Pregunta[] preguntas = {
new Pregunta("Cual es el lenguaje que ha revolucionado la web?", new Respuesta[] {
new Respuesta("Javascript", 'A', true),
new Respuesta("Ruby", 'B', false),
new Respuesta("Python", 'C', false),
new Respuesta("PHP", 'D', false),
new Respuesta("Go", 'E', false),
new Respuesta("C++", 'F', false)
}),
new Pregunta("5 + 5?", new Respuesta[] {
new Respuesta("1", 'A', false),
new Respuesta("2", 'B', false),
new Respuesta("3", 'C', false),
new Respuesta("10", 'D', true),
new Respuesta("20", 'E', false),
new Respuesta("2", 'F', false)
}),
};
for (Pregunta p: preguntas) {
p.preguntar();
}
}
}
class Pregunta {
private String pregunta;
private Respuesta[] respuestasPosibles;
public Pregunta(String pregunta, Respuesta[] respuestasPosibles) {
this.pregunta = pregunta;
this.respuestasPosibles = respuestasPosibles;
}
public void preguntar() {
System.out.println(this.pregunta);
char letraCorrecta = 'A';
for (Respuesta opcion: this.respuestasPosibles) {
if (opcion.esCorrecta()) letraCorrecta = opcion.getLetra();
System.out.print(String.valueOf(opcion.getLetra()) + ")" + opcion.getRespuesta() + " ");
}
System.out.println("\nElige: ");
Scanner sc = new Scanner(System.in);
char letraElegidaPorElUsuario = sc.next().toUpperCase().charAt(0);
if (letraElegidaPorElUsuario == letraCorrecta)
System.out.println("Correcto");
else
System.out.println("Incorrecto, la respuesta correcta era " + String.valueOf(letraCorrecta));
}
}
class Respuesta {
private String respuesta;
private char letra;
private boolean correcta;
public Respuesta(String respuesta, char letra, boolean correcta) {
this.respuesta = respuesta;
this.letra = letra;
this.correcta = correcta;
}
public String getRespuesta() {
return this.respuesta;
}
public char getLetra() {
return this.letra;
}
public boolean esCorrecta() {
return this.correcta;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment