Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Help with my program.

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with my program.

    Hi
    My program is required to have 10 questions from which 5 are chosen randomly without being repeated and shown to the user.
    I have written the program up to the point where I have 5 random questions shown but questions keep duplicating. I wonder if anyone can help me.
    Attached Files Attached Files


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Help with my program.

    Hi and welcome to the forum.
    Please see the announcements page for tips on forum use.
    Can you paste the code on the forum rather than attach it. It is just easier for people to read that way, and please don't forget code tags.

  3. #3
    Junior Member
    Join Date
    Oct 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with my program.

    Sorry here is my code:

    import java.util.*;
    import javax.swing.*;

    class Quiz {

    public static void main(String args[]){

    String question[] = new String[10];//Declare Question Array
    String ans[] = new String[10];// Declare Answer Array
    int correct=0;//Declare correct for keeping count of correct answers
    int incorrect=0;//Declare incorrect for keeping count of incorrect answers
    String input;//Declare input for answers given by user
    int n;//Declare n to be used as the random number below

    Random r = new Random();//declaring r as a random number

    question[0]= "Where is the Sea of Tranquillity?";//moon
    question[1] = "Freddie Bulgara was better known as which lead singer?"; //freddie mercury
    question[2] = "Robert Zimmerman is better known as whom in the music world?"; //bob dylan
    question[3]= "How many teeth in a full adult set?";//32
    question[4] = "Whose secretary was Miss Moneypenny?";//M
    question[5] = "Who was the first person to reach the South Pole?"; //Roald Amundsen
    question[6] = "The zloty is the currency of what country?";//Poland
    question[7] = "What famous Australian landmark was opened in 1932?"; //Sydney Harbour Bridge
    question[8] = "Siam is the former name for which country?";//Thailand
    question[9] = "What do the letters DVD stand for?";//Digital Versatile Disk

    ans[0]="Moon";
    ans[1]="Freddie Mercury";
    ans[2]="Bob Dylan";
    ans[3]= "32";
    ans[4]="M";
    ans[5]="Roald Amundsen";
    ans[6]="Poland";
    ans[7]="Sydney Harbour Bridge";
    ans[8]="Thailand";
    ans[9]="Digital Versatile Disk";

    for (int i =0; i<5; i++){
    n=r.nextInt(10);//Declare n as the random number between 0-9
    input=JOptionPane.showInputDialog(null, question[n]);

    String input1= input.trim();//Trim answer

    if(input1.equalsIgnoreCase(ans[n])){
    correct++;
    }

    else {
    JOptionPane.showMessageDialog(null, "Sorry that was incorrect\n The answer we were looking for was "+ans[n]);
    incorrect++;
    }

    }

    int output1=correct*20;//to work out correct percentage rate


    //Output message pane showing results
    JOptionPane.showMessageDialog(null, correct+ " right answers\n" +incorrect+ " incorrect answers\n " + output1+"% Success Rate ");
    }
    }

Similar Threads

  1. Replies: 1
    Last Post: July 8th, 2012, 10:23 AM
  2. Program to launch and mirror another program
    By hayate in forum Java Theory & Questions
    Replies: 13
    Last Post: March 8th, 2012, 11:47 PM
  3. Help with class program!!! STUCK! Program not doing what I Want!!!
    By sketch_flygirl in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 4th, 2011, 07:29 AM