Hello
I?ve made a quiz game and now I want to count the incorrect and the correct
answers, how can I do that?
I have an idea, but I would like to hear what you think.
Greetings
----------------------------------------------------------
import java.io.*;
import javax.swing.*;
public class Quiz
{
int i,j;
InputStreamReader reader;
BufferedReader buffer;
JTextArea txtarea;
// skapa referens till klassen Questions
//Questions myQuiz;
String choice;
String heading;
String p="\n\n Skriv (a) = konst, (b) = Geografi, (c) eller matematik (x)
avsluta:";
String answer,choice1;
String artsquestions;
String artsanswers;
String geoquestion;
String geoanswers;
/* dessa variabler inneh?ller antal korrekta och felaktiga
Arts och science fr?gor
*/
int
artCorrectAnswerCount,artWrongAnswerCount,geoCorrectanswerCount,geoWrongAnsw
erCount, mathcorrectanswercount,mathwronganswercount;
//Konstfr?gor
String artsQuestions[] = {"Kan man se Mona Lisa i Paris? (enter t or f)",
"Pablo Picasso var svensk? (enter t or f)",
"Dans ?r en konstform? (enter t or f)",
"Zorn m?lade nakna flickor (enter t or f)",
"Van Gogh skar av sig tungan (enter t or f)"};
//Geografifr?gor
final String geoQuestions[] = {"Talar man finska i Finland?(enter t or f)",
"USA ligger i sydamerika? (enter t or f)",
"Kairo ?r Egyptens huvudstad (enter t or f)",
"Palermo ?r en italiensk stad? (enter t or f)",
"Ligger Sahara ?knen i Afrika? (enter t or f)"};
//Matematikfr?gor
final String mathQuestions[] = {"11+2=24 (enter t or f)",
"6*7=42 (enter t or f)",
"2+2=4 (enter t or f)",
"2/2=1 (enter t or f)",
"0x0 =1 (enter t or f)"};
//Konstsvar
final String artsAnswers[]={"t","f","t","t","f"};
//Vetenskapsvar
final String geoAnswers[]={"t","f","t","t","t"};
//Matematiksvar
final String mathAnswers[]={"f","t","t","t","f"};
public static void main(String args[]) throws IOException // kastar IO
Exception
{
Quiz myQuiz =new Quiz();
myQuiz.gameQuestion();
String scienceQuestion;
//heading="\n"+"\t\t"+"************************************"+"\n"+
//!"\t\ skolprov...\n""************************************";
//System.out.println(heading);
}//main
public void gameQuestion()
{
choice = "";
do {
try {
//Instansierar en bufferedReader p? System.in
reader=new InputStreamReader(System.in);
buffer=new BufferedReader(reader);
System.out.print(p);
choice=buffer.readLine();
if (choice.toLowerCase().equals("a"))
artsQuestions();
else if (choice.equals("b"))
geoQuestions();
else if (choice.equals("c"))
mathQuestions();
/*else if (choice.equals("x")) {
report();
break;**/
} catch(Exception e) {
System.out.println("Input error -- "+e.toString());
}//try
} while(true);// end of while
}// gameQuestion()
public void artsQuestions() throws IOException {
System.out.println("Valkommen till Skolprov-Konst");
System.out.println("Ta den tid du behover,\n\n Det finns fem fragor till
varje amne");
i = 0; artCorrectAnswerCount = 0; artWrongAnswerCount = 0;
String answer = "";
do{
System.out.print("Q."+(i+1) + " "+ artsQuestions[i]);
//artsQuestions[] arrayen ?r definierad i Questions class
answer = buffer.readLine();
if ( answer.equals(artsAnswers[i])){
artCorrectAnswerCount++;
System.out.println(" Correct !!!");
} else {
artWrongAnswerCount++;
System.out.println(" Incorrect !!!");//blev Incorrect ist?llet f?r R?tt,
tyckte det blev ful utskrift
}
i++;
if(i==5){
System.out.println("\n\n konstsektionen Slut ");
}
}while(i<5);// while loop
} //artsQuestions()
public void geoQuestions() throws IOException {
System.out.println("Valkommen till Skolprov-Vetenskap");
System.out.println("Ta den tid du behover,\n\n Det finns fem fragor till
varje amne");
int i = 0; int geoCorrectAnswerCount = 0;int geoWrongAnswerCount = 0;
String answer = "";
do{
System.out.print("Q."+(i+1) + " "+ geoQuestions[i]);
//Anv?ndarens Indata
answer=buffer.readLine();
if ( answer.equals(geoAnswers[i])){
artCorrectAnswerCount++;
System.out.println(" Correct !!!");
}else{
artWrongAnswerCount++;
System.out.println(" Incorrect !!!");
}
i++;
if(i==5){
System.out.println("\n\n Geografisektionen Slut");
}
//}//while?
}while(i<5); //while
} //scienceQuestions()
public void mathQuestions() throws IOException {
System.out.println("V?lkommen till Skolprov-Matematik");
System.out.println("Ta den tid du behover,\n\n Det finns fem fragor till
varje amne");
int i = 0,mathCorrectAnswerCount = 0,mathWrongAnswerCount = 0;
answer = "";
do{
System.out.print("Q."+(i+1) + " "+ mathQuestions[i]);
//mathQuestions[] arrayen ?r definierad i Question class
//r?tt svar eller fel svar
answer=buffer.readLine();
if ( answer.equals(mathAnswers[i])){
artCorrectAnswerCount++;
System.out.println(" Correct !!!");
} else {
artWrongAnswerCount++;
System.out.println(" Incorrect !!!");
}
i++;
if(i==5){
System.out.println("\n\n Matematiksektionen Slut");
}
}while(i<5);// while loop
}//mathQuestions()
//Hur kan vi skriva en metod report() som skriver ut r?tt resp. fel svar per
kunskapsomr?de?
}//class Quiz
Roedy Green - 12 Apr 2004 23:13 GMT
>I´ve made a quiz game and now I want to count the incorrect and the correct
>answers, how can I do that?
int correct;
int incorrect;;
...
if ( gotItRight )
{
correct++;
}
else
{
incorrect++;
}
--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
Alex Hunsley - 12 Apr 2004 23:21 GMT
> Hello
>
[quoted text clipped - 4 lines]
>
> Greetings
Hint: post code that can be simply cut'n'pasted and compiled first time
and you will get help much more readily.
You have written code that is >= ~80 chars in width, which means that
your news posting software appears to have done word wrapping on it. The
result is code that has to be manually hacked back into working form by
anyone reader who wants to compile it. I'm willing to help you if you
can post a well formatted listing, but I've get better things to do than
reformat your code!
http://www.physci.org/codes/sscce.jsp is also a handy article.
alex