Hey!I have to make a Quiz game using arrays in java...Now,i have to create a random function such that the questions appear randomly n without any repetition?plz help...
Here's the code:
import java.util.*;
public class KBC
{
String questions[];
String options[][];
int answers[];
long money[];
int history[];
public KBC()
{
questions=new String[]{
"What is the capital of Australia?",
"Where is Fort William located ?",
"Name this Indian Tennis player who has turned Hollywood filmmaker?",
"On which riverbank is Goa located?",
"Which union ministry is the nodal agency to issue passports in India ?",
"In Harry Potter who plays Ron Weasley in the movie?",
"The movies 'Hum Tum', 'Parineeta', 'Salaam Namaste', 'Being Cyrus', 'Omkara' and 'Eklavya' proved that this star had 'come of age'. Which one of the following was this star?",
"Which country won the World Cup in 1999? ",
"How many alphabets are in Gurmukhi ?",
" In the wild, which animal is found only in China?",
"In the epic Ramayana, which bird tried to prevent Ravana from carrying Sita away?",
"Who is the Author of 'The Three Musketeers'?",
"'.BAK' extension refers usually to what kind of file?",
"Which of the following is known as 'diamond city' of India?",
"On playing a dice which number is directly opposite to 1 ?"
};
options=new String[][]{
{"1.Canberra","2.Melbourne","3.Brisbane","4.Sydney"},
{"1.Chennai","2.Kolkata","3.Goa","4.Mysore"},
{"1.Leander Paes","2.Mahesh Bhupathi","3.Vijay Amritraj","4.Ashok Amritraj"},
{"1.Ganga","2.Mandovi","3.Gomati","4.Sabarmati"},
{"1.External Affairs","2.Home Affairs","3.Defense Ministry","4.Tourism & Culture"},
{"1.Macaulay Culkin","2.Robin Pinter","3.Robert Pattison","4.Rupert Grint"},
{"1.Vidya Balan","2.Raima Sen","3.Sanjay Dutt","4.Saif Ali Khan"},
{"1.Australia","2.South Africa","3.Pakistan","4.England"},
{"1.35(Thirty-five)","2.36(Thirty-six)","3.37(Thirty-seven)","4.26(Twenty-six)"},
{"1.Tiger","2.Asian Elephant","3.Giant Panda","4.Crocodile"},
{"1.Vibhishan","2.Jatayu","3.Garuda","4.Bhulinga"},
{"1.William Shakespeare","2.Alexandre Dumas","3.Robert L.B.Stevenson","4.Charles Dickens"},
{"1.Backup file","2.Audio file","3.System file","4.MS Encarta document"},
{"1.Ahmedabad","2.Mumbai","3.Surat","4.Goa"},
{"1.Four","2.Two","3.Five","4.Six"},
};
answers=new int[]{1,2,3,2,1,4,4,1,1,3,2,2,1,3,4};
money=new long[]{1000,3000,5000,10000,20000,50000,100000,160000,320000,640000,1250000,2500000,5000000,10000000,20000000};
history=new int[15];
}
public int getRandomQuestion()
{
int i=(int)(Math.random()*15);
return i;
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
KBC k=new KBC();
System.out.println("********************************************************************************");
System.out.println(" :: :: :::::::: ::::::::");
System.out.println(" :: :: :: :: :: ");
System.out.println(" :: :: :: :: :: ");
System.out.println(" ::::: ::::::: :: ");
System.out.println(" :: :: :: :: :: ");
System.out.println(" :: :: :: :: :: ");
System.out.println(" :: :: :::::::: :::::::: ");
System.out.println("********************************************************************************");
System.out.println(" ");
int choice=0;
for(int i=0;i!=k.questions.length;i++)
{
int idx=k.getRandomQuestion();
System.out.println(k.questions[idx]);
System.out.println(k.options[idx][0]);
System.out.println(k.options[idx][1]);
System.out.println(k.options[idx][2]);
System.out.println(k.options[idx][3]);
System.out.println(" ");
System.out.println("PRESS 1,2,3 OR 4 : ");
choice=Integer.parseInt(sc.nextLine());
if(choice==k.answers[idx])
{
System.out.println("CONGRATS!!CORRECT ANSWER...YOU HAVE WON RS." +k.money[i]);
System.out.println(" ");
System.out.println(" ");
}
else
{
System.out.println("WRONG ANSWER!GAME OVER!");
System.out.println(" ");
System.out.println(" ");
break;
}
}
}
}