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
    Posts
    22

    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
    Code:
    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(question[1]);
    				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();
    
    		if
    			(in.equals(anwser[1]))
    		{
    			System.out.println("Correct");
    		}

  2. #2
    Join Date
    Jun 2013
    Location
    New York, USA
    Posts
    21

    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
    Location
    Chennai
    Posts
    5

    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++) {
    System.out.println(questions[i]);

    for (String opt : options) {
    System.out.println(opt);
    }

    ans = scan.nextLine();

    if (ans.equals(answers[i])) {
    scores++;
    System.out.println("Correct!");
    } else {
    System.out.println("Wrong!");
    }
    }

    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
    Posts
    13

    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
  •  





Click Here to Expand Forum to Full Width

Featured