The Artima Developer Community
Sponsored Link

Java Answers Forum
arrays and final outcome

6 replies on 1 page. Most recent reply: Apr 7, 2008 9:47 PM by nelson ruiz

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 6 replies on 1 page
nelson ruiz

Posts: 7
Nickname: nogal
Registered: Mar, 2008

arrays and final outcome Posted: Mar 30, 2008 12:07 PM
Reply to this message Reply
Advertisement
Hi all,

I am doing a program that contains an array of 10 multiple choice questions. Each question contains 3 answers. Also I have to create an array that holds the correct answer to each question (A, B or C). When the question is displayed, the program verifies that the user enters only a, b, or c (no matter if it is lower or upper case)otherwise do nothing. At the end of the 10 questions it should display the numbers of correct and incorrect answers. I did it simple without importing the javax.swing.OPtionPane because i am not sure how to work with this and arrays.

I am stuck at this point, reason why I am here asking for help from people who knows more than me (this is my first java course)

So far I have 1 error

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


thanks.




import java.io.*;

public class Quiz
{
String questionaire;
int correctCounter =0;
int incorrectCounter=0;
String input=a, b, c;

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(input == "a" || input == "b" || input =="c")
{
correctCounter = correctCounter + 1;
}
else
{
System.out.println("Please try again.\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);
*/


}

}



Rajeev Mutalik

Posts: 57
Nickname: rajmutalik
Registered: Sep, 2002

Re: arrays and final outcome Posted: Mar 30, 2008 10:34 PM
Reply to this message Reply
It should be:

String[] input={"a", "b", "c"};

Regards,
Rajeev

nelson ruiz

Posts: 7
Nickname: nogal
Registered: Mar, 2008

Re: arrays and final outcome Posted: Mar 31, 2008 8:56 PM
Reply to this message Reply
Hi Rajeev,

thanks for answering. Could you please be more specific what you mean by String[] input={"a", "b", "c"}; Is this the comparison line to get the number of correct and incorrect or another line.

Thanks

Rajeev Mutalik

Posts: 57
Nickname: rajmutalik
Registered: Sep, 2002

Re: arrays and final outcome Posted: Mar 31, 2008 9:52 PM
Reply to this message Reply
In ur previous code String input=a, b, c; it means u r trying to allocate multiple value to a variable which can hold only one. So in the new code String[] input={"a", "b", "c"}; I am declaring an array which can hold multiple values. Thats the reason I am able to store a, b and c in the array. Which cannot be done with only String. Now the values in this array can be compared with the ones u r getting from keyboard i.e

boolean validAnswer = false;
for (int i = 0; i < input.length; i ++) {
if (fromKeyBoard.equalsIgnoreCase())
validAnswer = true;
}

if (validAnswer) {
// Here u can check for the correct answer.
// The above for loop only checks whether the user has
// keyed in valid options or not.
}

Regards,
Rajeev

nelson ruiz

Posts: 7
Nickname: nogal
Registered: Mar, 2008

Re: arrays and final outcome Posted: Apr 1, 2008 10:13 PM
Reply to this message Reply
Interesting the way you did it. Thanks Rajeev. I worked it and it is pretty much finish.

If anything I will be back. Thanks again.

nelson ruiz

Posts: 7
Nickname: nogal
Registered: Mar, 2008

Re: arrays and final outcome Posted: Apr 6, 2008 8:55 PM
Reply to this message Reply
Hi Rajeev,

i did a couple of changes to the program but it is given me 2 errors and I don't get it

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

Thanks




/* 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 ach 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);
}



nelson ruiz

Posts: 7
Nickname: nogal
Registered: Mar, 2008

Re: arrays and final outcome Posted: Apr 7, 2008 9:47 PM
Reply to this message Reply
Fixed them. everything ok. Thanks

Flat View: This topic has 6 replies on 1 page
Topic: arrays and final outcome Previous Topic   Next Topic Topic: logging framework

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use