CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: array quiz

  1. #1
    Join Date
    Feb 2013

    array quiz

    I'm trying to create a program where there are two array's. An array of questions and an array of answers. Basically i want to show the elements in the question array one by one get the user input, then compare the user input with the same number element in the answer array. For example the first element in question array should be connected with the first element in answer array. Here is the code i have so far, and the problem i'm having is when i check the answer it doesn't return right or wrong. Here is the code thanks
    import java.util.*;
    public class arrayTest
    	public static void main(String[] args)
    		String[] question = {"what is bread", "what is milk", "what is sugar", "what is coffee"};
    		String[] anwser = {"a", "c", "a", "b"};
    				System.out.println("a dairy");
    				System.out.println("b meat");
    				System.out.println("c bread");
    				System.out.println("d sweet");
    		Scanner scan = new Scanner(System.in);
    		String in;
    		in = scan.nextLine();

  2. #2
    Join Date
    Jun 2013
    New York, USA

    Re: array quiz

    Arrays start on 0, so to get answer "a" for number 1 you have to call answer[0].

    I would also change (in.equals(anwser[1])) to (in.equalsIgnoreCase(anwser[1])) so if you type in B instead of b, the program counts it as the same. Other than that I think it should work properly.

  3. #3
    Join Date
    Sep 2013

    Re: array quiz

    Try this..

    package com.java_sample;

    import java.util.Scanner;

    public class ArrayQuiz {
    public static void main(String args[]) {
    String[] questions = {"what is apple?", "what is coffee?", "what is banana?", "What is milk?"};
    String[] answers = {"C", "B", "C", "A"};

    String[] options = {"A Health drink", "B Hot drink", "C Fruit", "D Bread"};

    System.out.println("Answer the following questions one by one - Give options like A, B and C etc!");

    Scanner scan = new Scanner(System.in);
    String ans;

    int scores = 0;

    for (int i = 0; i < questions.length; i++) {

    for (String opt : options) {

    ans = scan.nextLine();

    if (ans.equals(answers[i])) {
    } else {

    System.out.println("Quiz completed!");
    System.out.println("Your score is : " +scores);

    if (scores == 4) {
    System.out.println("Done very well, Congraz!");
    } else if (scores == 3) {
    System.out.println("Tried well, Improve more!");
    } else {
    System.out.println("Need more improvements, Try again!");



  4. #4
    Join Date
    Oct 2013

    Re: array quiz

    you have to use these questions in for loop to be printed one by one and for answers to be chosen and compared with the user input values i would like to choose mapping (part of collection). you can do without mapping but it will give some pain to you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.