programacion con arreglos

nogal
07 de Abril del 2008
Hola,

Estoy haciendo un programa que contiene un arreglo de 10 que son preguntas de opcion multiple. Cada pregunta contiene 3 respuestas (a, b, c). Tambien debo crear un arreglo que contenga la respuesta correcta (a, b, c sin importar si es mayuscula o minuscula) para cada pregunta. Cuando la pregunta aparece en la pantalla, el usuario debe poner unicamente a, b, o c, sino lo hace, el juego no continua hasta que lo haga. Si la respuesta es correcta, aparecera un mensaje diciendo Correct!, de otra forma, el mensaje sera: The correct answer is a=Switzerland.\n" (como ejemplo para la primera pregunta). Al final de las 10 preguntas se debe desplegar un aviso diciendo cuantas correctas y cuantas incorrectas tuvo el jugador.

Lo inicie asi simle por que no estoy seguro de como trabaja la opcion javax.swing.OPtionPane con arreglos.

Inicie el programa, puse las preguntas en los arreglos (creo que bien), pero aun me falta la parte donde el jugador entra mayusculas o minusculas y no importa y lo otro es como hacer el conteo de las respuestas correctas e incorrectas.

Llevo dos semanas en este cuento y me siento limitado por mi poco conocimiento en java y en programacion (este es mi primer curso en programacion y en java). Cualquier ayuda se la agradeceria enormemente.

aqui puse el codigo que me presenta un error;

Quiz.java:8: cannot find symbol
symbol : variable a
location: class Quiz
String input=a, b, c;
^
1 error


[code]

import java.io.*;

public class Quiz
{
String questionaire;
int correctCounter =0;
int incorrectCounter=0;

public static void main(String [ ] args) throws IOException
{
Quiz q = new Quiz();
}

public Quiz() throws IOException
{

BufferedReader keyboard = new
BufferedReader(new InputStreamReader(System.in));
String seekResponse;
String seekAnswer = null;

System.out.println("This is a 10 quiz program."
+ "\nLet\'s begin");

String[ ] questionaire = new String[10];

questionaire[0] = "1. Which country is known as the roof of the
world?"
+ "\na)Switzerland b)Argentina c)India";

System.out.println(questionaire[0]);

seekAnswer = keyboard.readLine();

if( seekAnswer.equals("a"))
{
System.out.println("\n Correct!\n");
}
else
{
System.out.println("The correct answer is a=Switzerland.\n");
}

questionaire[1] = "2. Which is the largest island in the world?"
+ "\na)Srilanka b)Australia c)Greenland";

System.out.println(questionaire[1]);

seekAnswer = keyboard.readLine();

if( seekAnswer.equals("c"))
{
System.out.println("\n Correct!\n");
}
else
{
System.out.println("The correct answer is c=Greenland.\n");
}

questionaire[2] = "3. What is the study of curves in three-dimensional
space, such as spheres or cones, called?"
+ "\na)Solid Geometry b)Space
Geometry c)Abstract Geometry";

System.out.println(questionaire[2]);

seekAnswer = keyboard.readLine();

if( seekAnswer.equals("a"))
{
System.out.println("\n Correct!\n");
}
else
{
System.out.println("The correct answer is a=Solid
Geometry.\n");
}

questionaire[3] = "4. Charles Darwin began his voyage on HMS
Beagle from what port?"
+ "\na)Devonport, England b)Dover,
France c)New York";

System.out.println(questionaire[3]);

seekAnswer = keyboard.readLine();

if( seekAnswer.equals("a"))
{
System.out.println("\n Correct\n");
}
else
{
System.out.println("The correct answer is a=Devonport.\n");
}

questionaire[4] = "5. The city of Manta is located in?"
+ "\na)Egypt b)France c)Ecuador";

System.out.println(questionaire[4]);

seekAnswer = keyboard.readLine();

if( seekAnswer.equals("c"))
{
System.out.println("\n Correct!\n");
}
else
{
System.out.println("The correct answer is c=Ecuador.\n");
}


questionaire[5] = "6. The longest highway in the world is?"
+ "\na)Trans-Canada b)Inter-State 90 USA c)E30 Route
Europe";

System.out.println(questionaire[5]);

seekAnswer = keyboard.readLine();

if( seekAnswer.equals("a"))
{
System.out.println("\n Correct!\n");
}
else
{
System.out.println("The correct answer is
a=trans-Canada.\n");
}


questionaire[6] = "7. Which is the shallowest sea in the world?"
+ "\na)Caspian Sean b)Baltic Sea c)Sea of Azov";

System.out.println(questionaire[6]);

seekAnswer = keyboard.readLine();

if( seekAnswer.equals("c"))
{
System.out.println("\n Correct!\n");
}
else
{
System.out.println("The correct answer is c=Sea of Azov.\n");
}


questionaire[7] = "8. Which country is known as the lady of snow?"
+ "\na)Greenland b)Canada c)Pakistan";

System.out.println(questionaire[7]);

seekAnswer = keyboard.readLine();

if( seekAnswer.equals("b"))
{
System.out.println("\n Correct!\n");
}
else
{
System.out.println("The correct answer is b=Canada.\n");
}


questionaire[8] = "9. How many feathers are used to make
badminton shuttle?"
+ "\na)10 to 12 b)18 to 20 c)14 to 16";

System.out.println(questionaire[8]);

seekAnswer = keyboard.readLine();

if( seekAnswer.equals("c"))
{
System.out.println("\n Correct!\n");
}
else
{
System.out.println("The correct answer is c=14 to 16.\n");
}



questionaire[9] = "10. Who developed the World Wide Web {www}?"
+ "\na)Tim Bernes Lee b)Charles Babbage c)Jim
Osborne";

System.out.println(questionaire[9]);

seekAnswer = keyboard.readLine();

if( seekAnswer.equals("a"))
{
System.out.println("\n Correct!\n");
}
else
{
System.out.println("The correct answer is a=Tim Bernes
Lee.\n");
}

if (questionaire[9] == correct)
{
correctCounter = correctCounter + 1;
}
else
{
incorrectCounter = incorrectCounter + 1;
}


System.out.println("The number of correct answers is:"
+ correctCounter +"\nThe number of incorrect
answers is:"
+ incorrectCounter);
}

}


[/code]

ozito
07 de Abril del 2008
nogal:

1º.- para detectar mayúsculas o minúsculas simplemente convierte la respuesta del usuario a minúsculas utilizando el método
seekAnswer.toLowerCase();

2º.- Para contar los acierto debes poner un
correctCounter = correctCounter + 1;
en cada bloque if de la pregunta y un
incorrectCounter = incorrectCounter + 1;
en cada bloque else


De todas formas te adjunto, una forma un poco más 'dirigida a objetos' de hacer lo que quieres:

import java.io.*;

public class Quiz {
String [][] questionaire;
int correctCounter =0;
int incorrectCounter=0;

public static void main(String [ ] args) throws IOException {
Quiz q = new Quiz();
}

public Quiz() throws IOException {

BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
String seekAnswer = null;

System.out.println("This is a 10 quiz program." + "nLet's begin");

// crea un arreglo de dos dimensiones, para las preguntas
// y las soluciones y lo rellena
questionaire = new String[10][2];
questionaire[0][0] = "1. Which country is known as the roof of the world?" +
"na)Switzerland b)Argentina c)Indian";
questionaire[0][1] = "a";
questionaire[1][0] = "2. Which is the largest island in the world?" +
"na)Srilanka b)Australia c)Greenlandn";
questionaire[1][1] = "c";
questionaire[2][0] = "3. What is the study of curves in three-dimensional space, such as spheres or cones, called?" +
"na)Solid Geometry b)Space Geometry c)Abstract Geometryn";
questionaire[2][1] = "a";
questionaire[3][0] = "4. Charles Darwin began his voyage on HMS Beagle from what port?" +
"na)Devonport, England b)Dover, France c)New Yorkn";
questionaire[3][1] = "a";
questionaire[4][0] = "5. The city of Manta is located in?" +
"na)Egypt b)France c)Ecuadorn";
questionaire[4][1] = "c";
questionaire[5][0] = "6. The longest highway in the world is?" +
"na)Trans-Canada b)Inter-State 90 USA c)E30 Route Europen";
questionaire[5][1] = "a";
questionaire[6][0] = "7. Which is the shallowest sea in the world?" +
"na)Caspian Sean b)Baltic Sea c)Sea of Azovn";
questionaire[6][1] = "c";
questionaire[7][0] = "8. Which country is known as the lady of snow?" +
"na)Greenland b)Canada c)Pakistann";
questionaire[7][1] = "b";
questionaire[8][0] = "9. How many feathers are used to make badminton shuttle?" +
"na)10 to 12 b)18 to 20 c)14 to 16n";
questionaire[8][1] = "c";
questionaire[9][0] = "10. Who developed the World Wide Web {www}?" +
"na)Tim Bernes Lee b)Charles Babbage c)Jim Osbornen";
questionaire[9][1] = "a";

// bucle para pasar por todas las preguntas
for (int i = 0; i<10; i++) {
System.out.println(questionaire[i][0]);
seekAnswer = keyboard.readLine();
//Convierte la respuesta a minúsculas por si el usuario ha utilizado mayúsculas
seekAnswer.toLowerCase();
//Comprueba si la respuesta era correcta
if( seekAnswer.equals(questionaire[i][1])) {
System.out.println("Correct!n");
correctCounter = correctCounter + 1;
}
else {
System.out.println("The correct answer was: " + questionaire[i][1] + "n");
incorrectCounter = incorrectCounter + 1;
}
}

//Muestra los resultados
System.out.println("The number of correct answers is: " + correctCounter +
"nThe number of incorrect answers is: " + incorrectCounter);
}
}

nogal
07 de Abril del 2008
Gracias ozito por tu respuesta. El programa corre muy bien y ahora entiendo mas que cosas me hacian falta y que cosas no tenia ni idea de como hacerlas.

Pregunta: cuando declaras el "String [][] questionaire;" por que le pones doble [ ], hay alguna razon especifica para ello?

de nuevo muchas gracias por tu ayuda.

ozito
07 de Abril del 2008
Nogal,

los dobles corchetes [][], indican que es un array de dos dimensiones (también conocido como una matriz):
[pregunta1][respuesta1]
[pregunta2][respuesta2]
...
[pregunta10][respuesta10]

Me pereció las forma más sencillas de tener unidas las preguntas a las respuestas...

nogal
07 de Abril del 2008
Gracias por la explicacion. Algo completamente nuevo para mi. Existe algun limite en el numero de dimensiones o es abierto a cualquier cosa.

Ahora, disculpa la molestia pero en mi mensaje anterior cometi un error. Esta es la correcta premisa del programa, el jugador debe usar unicamente a, b, o c, cuando conteste, de otra forma el programa no seguira corriendo hasta que el jugador entre alguna de estas letras. Esto se haria por comparacion o usando algun arreglo. Me podriaas dar una mano en esto. Gracias.

ozito
07 de Abril del 2008
Para hacer lo que quieres puedes utilizar un bucle do-while, sustituye el bucle for de mi ejemplo anterior por este:
for (int i = 0; i<10; i++) {
do {
System.out.println(questionaire[i][0]);
seekAnswer = keyboard.readLine();
//Convierte la respuesta a minúsculas por si el usuario ha utilizado mayúsculas
seekAnswer.toLowerCase();
if (seekAnswer.equals("a") || seekAnswer.equals("b") || seekAnswer.equals("c")) {
//Comprueba si la respuesta era correcta
if( seekAnswer.equals(questionaire[i][1])) {
System.out.println("Correct!n");
correctCounter = correctCounter + 1;
break;
}
else {
System.out.println("The correct answer was: " + questionaire[i][1] + "n");
incorrectCounter = incorrectCounter + 1;
break;
}
}
else {
System.out.println("The answer must be: a), b) or c)"+ "n");
}
}
while(true);
}

Esto tiene un problema, se crea un bucle infinito hasta que el usuario teclee a, b ó c...

nogal
07 de Abril del 2008
Disculpa pero se me olvidaba una premisa del programa.

Cuando el jugador conteste y si no es la respuesta correcta, el programa debera seguir esperando por la respuesta correcta para seguir a la siguiente pregunta.

Gracias