Sunday 15 April 2007

Quiz Program using Java

// Created by Husam Jalal

import javax.swing.JOptionPane;
public class QuizProgram
{
public static void main( String args[] )
{
String firstQuestion, //first string entered by user as answer
secondQuestion,//
thiredQuestion,
fourthQuestion,
fifthQuestion,
sixthQuestion;

int answer1,//answer question 1 by user
answer2,// answer question 2 by user
answer3,//answer question 3 by user
answer4, //answer question 4 by user
answer5,// answer question 5 by user
answer6;//answer question 6 by user
JOptionPane.showMessageDialog(null,"<<<<<<<<<<<<<<<<<<<<*>*<*>>>>>>>>>>>>>>>>>>>>>\nYou will be asked SIX questions,\nif you answer the first one you will be offered anther.\nIf you don't know the answer\nand you want to skip to the other question\nSIMPLY TYPE 0\ngood luck to you and lets begin\n<<<<<<<<<<<<<<<<<<<<<<*>>>>>>>>>>>>>>>>>>>>>>>","QUIZZES",JOptionPane.QUESTION_MESSAGE);

do{
firstQuestion=JOptionPane.showInputDialog(null,"How many oceans are they in world?\nTo skip this question enter 0","QUESTION 1",JOptionPane.QUESTION_MESSAGE );//string 1 entered by user

answer1 =Integer.parseInt( firstQuestion ); // convert numbers from type String to type int

if(answer1==0) // if the user entered 0 will skip the question. the user doesn't know the answer
break;

if(answer1==4) //if answer is 4 it is correct, show thw message in the next line

JOptionPane.showMessageDialog(null,"The answer is correct, go to the next Question","WEEL DONE",JOptionPane.PLAIN_MESSAGE);

else // if the answer is wrong offer the question anther time till you get the answer

JOptionPane.showMessageDialog(null,"your answer is wrong you should try again","SORRY WRONG ANSWER",JOptionPane.WARNING_MESSAGE);

}while(answer1!=4); //if the answer is correct go to the next question

do{
secondQuestion=JOptionPane.showInputDialog(null, " How many stats in USA?\nTo skip this question enter 0 ","QUESTION 2",JOptionPane.QUESTION_MESSAGE);

answer2 =Integer.parseInt( secondQuestion ); // convert numbers from type String to type int

if(answer2==0)
break;

if(answer2==50) //if answer is 50 it is correct, show thw message in the next line

JOptionPane.showMessageDialog(null,"your answer is correct go to the next Question","WEEL DONE",JOptionPane.PLAIN_MESSAGE);

else

JOptionPane.showMessageDialog(null,"your answer is wrong you should try again","SORRY WRONG ANSWER",JOptionPane.WARNING_MESSAGE);
}while(answer2!=50);

do{
thiredQuestion=JOptionPane.showInputDialog(null, " How many countries in Europe?\nTo skip this question enter 0 ","QUESTION 3",JOptionPane.QUESTION_MESSAGE);

answer3 =Integer.parseInt( thiredQuestion ); // convert numbers from type String to type int

if(answer3==0)
break;

if(answer3==12) //if answer is 12 it is correct, show thw message in the next line

JOptionPane.showMessageDialog(null,"your answer is correct go to the next Question","WEEL DONE",JOptionPane.PLAIN_MESSAGE);

else
JOptionPane.showMessageDialog(null,"your answer is wrong you should try again","SORRY WRONG ANSWER",JOptionPane.WARNING_MESSAGE);

}while(answer3!=12);

do{
fourthQuestion=JOptionPane.showInputDialog(null, "How many continents in the world?\nTo skip this question enter 0","QUESTION 4",JOptionPane.QUESTION_MESSAGE);

answer4 =Integer.parseInt( fourthQuestion ); // convert numbers from type String to type int

if(answer4==0)
break;

if(answer4==6) //if answer is 6 it is correct, show the message in the next line

JOptionPane.showMessageDialog(null,"your answer is correct go to the next Question","WEEL DONE",JOptionPane.PLAIN_MESSAGE);

else
JOptionPane.showMessageDialog(null,"your answer is wrong you should try again","SORRY WRONG ANSWER",JOptionPane.WARNING_MESSAGE);

}while(answer4!=6);

do{
fourthQuestion=JOptionPane.showInputDialog(null, "How many campus in Middlesex University?\nTo skip this question enter 0","QUESTION 5",JOptionPane.QUESTION_MESSAGE);

answer5 =Integer.parseInt( fourthQuestion ); // convert numbers from type String to type int

if(answer5==0)
break;

if(answer5==6) //if answer is 6 it is correct, show thw message in the next line

JOptionPane.showMessageDialog(null,"your answer is correct go to the next Question","WEEL DONE",JOptionPane.PLAIN_MESSAGE);

else
JOptionPane.showMessageDialog(null,"your answer is wrong you should try again","SORRY WRONG ANSWER",JOptionPane.WARNING_MESSAGE);

}while(answer5!=6);

do{
fourthQuestion=JOptionPane.showInputDialog(null, "How many player in a footbal team?\nTo skip this question enter 0","QUESTION 6",JOptionPane.QUESTION_MESSAGE);

answer6 =Integer.parseInt( fourthQuestion ); // convert numbers from type String to type int

if(answer6==0)
break;

if(answer6==11) //if answer is 11 it is correct, show thw message in the next line

JOptionPane.showMessageDialog(null,"your answer is correct\n\nWell done and thank you for answering the quizzes","THIS IS THE END OF THE QUIZZES",JOptionPane.PLAIN_MESSAGE);

else
JOptionPane.showMessageDialog(null,"your answer is wrong you should try again","SORRY WRONG ANSWER",JOptionPane.WARNING_MESSAGE);

}while(answer6!=11);

if(answer1+answer2+answer3+answer4+answer5+answer6==0)

JOptionPane.showMessageDialog(null,"you didn't answer any question","WHAT A SHAME",JOptionPane.WARNING_MESSAGE);

else

if(answer1+answer2+answer3+answer4+answer5==0)

JOptionPane.showMessageDialog(null,"you didn't answer the first 5 questions","SORRY WRONG ANSWER",JOptionPane.WARNING_MESSAGE);
else

if(answer1+answer2+answer3+answer4==0)

JOptionPane.showMessageDialog(null,"you didn't answer the first 4 questions","SORRY WRONG ANSWER",JOptionPane.WARNING_MESSAGE);

else
if(answer1+answer2+answer3==0)

JOptionPane.showMessageDialog(null,"you didn't answer the first 3 questions","SORRY WRONG ANSWER",JOptionPane.WARNING_MESSAGE);

else
if(answer1+answer2==0)

JOptionPane.showMessageDialog(null,"you didn't answer the first 2 questions","SORRY WRONG ANSWER",JOptionPane.WARNING_MESSAGE);

else
if(answer1==0)

JOptionPane.showMessageDialog(null,"you didn't answer the first questions","SORRY WRONG ANSWER",JOptionPane.WARNING_MESSAGE);

System.exit( 0 ); // terminate the program
}
}

1 comment:

Unknown said...

very nice! basic but i like it. i am to in the swing era right now. i enjoyed ur prgram haha