View Single Post
  #21 (permalink)  
Old 01-22-2008, 03:16 PM
mcal mcal is offline
Member
 
Join Date: Jan 2008
Posts: 39
mcal is on a distinguished road
Thanks a lot. Its really kind of you to help me with this problem. I have understood what you told me, but unfortunately, its still repeating questions( to be precise it is repeating only the last question for 3 to 4 times). This is the code, where i put the source code you told me. I think that there is something wrong with the way "pos" is calculated. But i can't figure it out. Have i have done something wrong? Thanks a lot again and i'm sorry for bothering you. Thanks again. :-)

The code:

Code:
public void askQuestions(String[] questions, String[] answers) { int count = 0; int point = 0; int[] A = new int[questions.length]; int[] B = new int[A.length]; int countA = A.length; int countB = 0; for (int i = 0; i < A.length; i++){ A[i] = i; } while (countA > 0){ Random generator = new Random(); int pos = generator.nextInt(A.length); B[countB] = A[pos]; countB++; for (int i = pos; i < A.length - 1; i++){ A[i] = A[i + 1]; } countA--; } for(int j = 0; j < questions.length; j++) { timeForMore = true; int randomIndex = B[j]; String input = JOptionPane.showInputDialog(null, questions[randomIndex]); if(answers[randomIndex].equalsIgnoreCase(input)) count++; // incrementing counter if entered answer is correct point++; if(!timeForMore) // if time is over, the program executes the loop an stops asking questions. break; } JOptionPane.showMessageDialog(null, "You answered " + count + " out of " + questions.length + " questions correctly."); }

Last edited by mcal : 01-22-2008 at 07:12 PM.
Reply With Quote
Sponsored Links