Privacidad: Recuerde que la información escrita en los foros de programación es 100% pública y que su ip será registrada asociada a su mensaje. Si encuentra un mensaje fuera de lugar, por favor, notifiquelo para su revisión y eliminación.
programacion con arreglos
Enviado por nogal el día 29 de marzo de 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();
}
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\";
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)India\n";
questionaire[0][1] = "a";
questionaire[1][0] = "2. Which is the largest island in the world?" +
"\na)Srilanka b)Australia c)Greenland\n";
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 Geometry\n";
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 York\n";
questionaire[3][1] = "a";
questionaire[4][0] = "5. The city of Manta is located in?" +
"\na)Egypt b)France c)Ecuador\n";
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 Europe\n";
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 Azov\n";
questionaire[6][1] = "c";
questionaire[7][0] = "8. Which country is known as the lady of snow?" +
"\na)Greenland b)Canada c)Pakistan\n";
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 16\n";
questionaire[8][1] = "c";
questionaire[9][0] = "10. Who developed the World Wide Web {www}?" +
"\na)Tim Bernes Lee b)Charles Babbage c)Jim Osborne\n";
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);
}
}
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?
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...
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.
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...
Exacto, esa seria la idea. El jugador debe entrar a, b o c para que el juego continue.
Inserte el codigo pero me salen 2 errores en la misma linea:
Quiz.java:114: <identifier> expected
System.out.println("The number of correct answers is: " + correctCounter +
^
Quiz.java:114: illegal start of type
System.out.println("The number of correct answers is: " + correctCounter +
^
2 errors
gracias
[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 seekAnswer = null;
System.out.println("This is a 10 quiz program." + "\nLet's begin\n");
// 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)India\n";
questionaire[0][1] = "a";
questionaire[1][0] = "2. Which is the largest island in the world?" +
"\na)Srilanka b)Australia c)Greenland\n";
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 Geometry\n";
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 York\n";
questionaire[3][1] = "a";
questionaire[4][0] = "5. The city of Manta is located in?" +
"\na)Egypt b)France c)Ecuador\n";
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 Europe\n";
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 Azov\n";
questionaire[6][1] = "c";
questionaire[7][0] = "8. Which country is known as the lady of snow?" +
"\na)Greenland b)Canada c)Pakistan\n";
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 16\n";
questionaire[8][1] = "c";
questionaire[9][0] = "10. Who developed the World Wide Web {www}?" +
"\na)Tim Bernes Lee b)Charles Babbage c)Jim Osborne\n";
questionaire[9][1] = "a";
//Comprobar que el jugador escogio unicamente a,b o c
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);
}
}
//Muestra los resultados
System.out.println("The number of correct answers is: " + correctCounter +
"\nThe number of incorrect answers is: " + incorrectCounter);
}
Re: Re: Re: Re: Re: Re: Re: programacion con arreglos
Enviado por nogal el día 7 de abril de 2008
Hola Ozito,
Ya solucione el problema y compila bien, pero cuando corre unicamente se para si el jugador no entra a, b o c en la primera vez, de resto el programa sigue y da la respuesta correcta. Me podrias ayudar con esto
gracias
[code]
// Filename Quiz.java
// Written by Nelson Ruiz blanco
// Written on March 31, 2008
// Assignment #4 Exercise 1 Game Zone page 313 from chapter eight,
// book Java Programming, Fourth edition, Joyce Farrel
// Java 1.6.0
/* The purpose of this program is to write an application that contains
an array of 10 multiple-choice quiz questions related to my favorite hobby.
Each question contains three answer choices. Also create an array
that holds the correct answer to each question (a, b, or c). Display
each question and verify that the user enters only a, b, or c as the answer,
if not, keep prompting the user until a valid response is entered. if the
user responds to a question correctly, display Correct!; otherwise display
"The correct answer is" and the letter of the correct answer. After the user
answer all the questions, display the number of correct and incorrect
answers.
*/
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\n");
// 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)India\n";
questionaire[0][1] = "a";
questionaire[1][0] = "2. Which is the largest island in the world?" +
"\na)Srilanka b)Australia c)Greenland\n";
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 Geometry\n";
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 York\n";
questionaire[3][1] = "a";
questionaire[4][0] = "5. The city of Manta is located in?" +
"\na)Egypt b)France c)Ecuador\n";
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 Europe\n";
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 Azov\n";
questionaire[6][1] = "c";
questionaire[7][0] = "8. Which country is known as the lady of snow?" +
"\na)Greenland b)Canada c)Pakistan\n";
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 16\n";
questionaire[8][1] = "c";
questionaire[9][0] = "10. Who developed the World Wide Web {www}?" +
"\na)Tim Bernes Lee b)Charles Babbage c)Jim Osborne\n";
questionaire[9][1] = "a";
//Comprobar que el jugador escogio unicamente a,b o c
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);
}
//Muestra los resultados
System.out.println("The number of correct answers is: " + correctCounter +
"\nThe number of incorrect answers is: " + incorrectCounter);
}
}
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.