0

i would like to make the question random, and when i give the answer to compare if they are correct answer or not?? can someone give me a little help with it?

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Random;

public class Main {

public static void main(String[] args) { String[] aa = { "question aa" }; String[] bb = { "question bb"}; String[] cc = { "question cc"}; String[] dd = { "question dd"}; String[] e = { "answer to question aa" }; String[] f = { "answer to question bb"}; String[] g = { "answer to question cc"}; String[] h = { "answer to question dd"}; // should be here the random question System.out.print("Enter your answer: "); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String userAnswer = null; try { userAnswer = br.readLine(); } catch (IOException ioe) { System.out.println("IO error trying to read your answer!"); System.exit(1); } System.out.println("Thanks for the answer, " + userAnswer);

}

2 Answers 2

3
class QuestionAnswer{
String q;
String a;
//getters/setters and const.
}

1 Create a DS as your Question set is smaller and advisable to keep in memory

List<QuestionAnswer> lst = new ArrayList<QuestionAnswer>();
lst.add(new QuestionAnswer("Question1","Answer1")); 
lst.add(new QuestionAnswer("Question2","Answer2")); 
lst.add(new QuestionAnswer("Question3","Answer3")); 

2 Generate a random integer between 0 to list.size()

Random r = new Random();
int index = r.nextInt(lst.size());

3 Fetch question and print it and accept user;s answer

System.out.println(lst.get(index).getQ());
Scanner in = new Scanner(System.in);
// Reads a single line from the console 
String answer = in.nextLine();

4 Compare user's answer with list

if(answer.equalsIgnoreCase(lst.get(index).getA())){
   System.out.println("You are correct. !!");
} 
Sign up to request clarification or add additional context in comments.

3 Comments

life.java, I get your idea. But I'm not entirely convinced 3 Fetch question is clear enough to the OP. All he has is a Random Number, how is he supposed to figure out how to get an element of the Map using that number, which is his question is the first place...I feel you should elaborate on that. My 2 cents.
@st0le Thanks , I realized it was not so much helpful before , please check now
life.java, That's much better. +1 :)
0
  • Keep questions in questions array question[][] = {aa,bb...};
  • Keep answers in answers array arrays[][] = {e,f...};
  • generate random numbers between 0 and questions.length
  • questions[random] and answers[random] should match

    String[] aa = { "question aa" };
    String[] bb = { "question bb" };
    String[] cc = { "question cc" };
    String[] dd = { "question dd" };
    
    
    String[][] questions = { aa, bb, cc, dd };
    
    
    String[] e = { "answer to question aa" };
    String[] f = { "answer to question bb" };
    String[] g = { "answer to question cc" };
    String[] h = { "answer to question dd" };
    
    
    String[][] answers = { e, f, g, h };
    
    
    int min = 0;
    int max = 5;
    int random= 0;
    
    
    random = min + (int) (Math.random() * max);
    
    
    questions[random];
    answers[random];
    

2 Comments

what do you mean by this part , sorry i don't understand it questions[random]; answers[random];
random will be in range [0,5) so if random=0 question[0]'s answer is answer[0]. then you can match ask question[0] and user give an answer and you match it with answers[0].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.