jueves, 12 de febrero de 2015

Ejercicio 10, tema 3 Generar un examen aleatorio de preguntas

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package ejercicio9;
/**
*
* @author juan
*/
public class Test {
private int Num;
private String Enunciado;
private String [] Opciones = new String[4];
private int Valida;
public int getNum() {
return Num;
}
public void setNum(int Num) {
this.Num = Num;
}

public String getEnunciado() {
return Enunciado;
}
public void setEnunciado(String Enunciado) {
this.Enunciado = Enunciado;
}
public String[] getOpciones() {
return Opciones;
}
public void setOpciones(String[] Opciones) {
this.Opciones = Opciones;
}
public void setOpcionesCadenas(String cero, String uno, String dos, String tres){
Opciones[0]=cero;
Opciones[1]=uno;
Opciones[2]=dos;
Opciones[3]=tres;

}
public int getValida() {
return Valida;
}
public void setValida(int Valida) {
this.Valida = Valida;
}
@Override
public String toString() {
return "Test{" + "Num=" + Num + ", Enunciado=" + Enunciado + ", Opciones=" + Opciones + ", Valida=" + Valida + '}';
}

public void presenta (){
System.out.println(Num + " .-"+ Enunciado);

for (int i = 0; i < Opciones.length; i++) {
System.out.println(" "+ (i+1) +".- "+ Opciones[i]);
}
}
public Test() {
Enunciado="";
for (int i = 0; i < Opciones.length; i++) {
Opciones[i] = "";
}

}
public Test(int Num, String Enunciado, int Valida,String [] Opciones) {
this.Num = Num;
this.Enunciado = Enunciado;
this.Valida = Valida;
this.Opciones= Opciones;
}


}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package ejercicio9;
import java.util.Random;
import java.util.Scanner;
/**
*
* @author juan
*/
public class Ejercicio9 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {

Test preguntas[] = new Test [10];
Random al = new Random();
int examen [] = new int[5];

String [] opciones ={"uno","dos","tres","cuatro"};

preguntas[0] = new Test(0,"Enunciado",0,opciones);

preguntas[1]= new Test();
preguntas[1].setNum(1);
preguntas[1].setEnunciado("Cuál es el número de planetas del sistema solar?");
preguntas[1].setOpcionesCadenas("5", "6", "7", "3");
preguntas[1].setValida(3);

for (int i = 2; i < preguntas.length; i++) {
int respuesta;
respuesta = al.nextInt(4);
preguntas[i] = new Test(i,"Enunciado "+i+"-R: "+respuesta,respuesta,opciones);

}

for (Test preg : preguntas) {
System.out.println(preg.toString());
}

//creamos el examen
int num_preguntas =5;
int i=0;
boolean repetido;

while (i

int propuesta=al.nextInt(10);
int j=0;
repetido=false;
while (j if (propuesta== examen[j]) repetido=true;
else j++;
}
if (!repetido)
{
examen[i]=propuesta;
i++;

}

}
int respuesta;
Scanner leer = new Scanner(System.in);
int correctas=0;

for (int numero : examen) {
preguntas[numero].presenta();
System.out.println("Introduzca respuesta correcta: ");
respuesta = leer.nextInt();
if (respuesta==(preguntas[numero].getValida()+1)) correctas++;
}
System.out.println("Respuestas correctas "+ correctas);
System.out.println("Respuestas incorrectas "+ (num_preguntas-correctas));
}

}

1 comentario:

Sandra Torres Facebook dijo...

Muchas gracias por el blog!
Me sirve mucho!