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 7 of 7

Thread: Is there anyway to use a Do While statement in this program?

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Is there anyway to use a Do While statement in this program?

    is there anyway I can write a do while statement for this program. I haven't seen anyway to do it anywhere.



    import javax.swing.JOptionPane;
     
    public class History {
     
     
        public static void main(String[] args) 
        {
     
           //First Question: What is the capital of Mexico? 
            String[] question1 = {"Mexico City", "Paris"    //The choseable answers are made using String
                    ,"Washington D.C", "Tokyo"};
     
     
            String MexicoCapital = (String)JOptionPane.showInputDialog(null, "What is the capital of Mexico?:"
                    , "Question 1 of 10", JOptionPane.QUESTION_MESSAGE // question number is diplayed on top of text box
                    , null, question1, question1[0]); //Choices are called and displayed using joptionpane
     
     
            if (MexicoCapital == null)
            {
                JOptionPane.showMessageDialog(null, "You didn't pick a city!");
            }
            else
            {
     
                String funfact = "";
                switch (MexicoCapital)
                {
                    case "Mexico City":
                        funfact = "Correct";
                        break;
                    case "Paris":
                        funfact = "Incorrect! Paris is the capital of France";
                        break;
                    case "Washington D.C":
                        funfact = "Incorrect! Washington D.C is the capital of the United States";
                        break;
                    case "Tokyo":
                        funfact = "Incorrect! Tokyo is the capital of Japan";
                        break;
                    default:
                        funfact = "Something went wrong with picking an answer ";
                        break;
                }
                JOptionPane.showMessageDialog(null, funfact);
            }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Is there anyway to use a Do While statement in this program?

    Anytime a block of code needs to be repeated, a loop can be the programming tool to accomplish that. I see you have the statement "Question 1 of 10" in your code which suggests to me the code is intended to ask 10 questions, and that sounds like a repeated activity - 10 times asking a question. That's where your loop goes, around the code that asks the question. Any of the loops, for, while, or do/while would work, but since you know the loop will run exactly 10 times, the for loop would be the typical choice.

  3. #3
    Junior Member
    Join Date
    Feb 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Is there anyway to use a Do While statement in this program?

    how do i write that i can't figure out how to .

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Is there anyway to use a Do While statement in this program?

    Something like:

    int questionNumber = 1;
     
    while ( questionNumber < 11 )
    {
        // ask question, get answer, etc.
     
        // increment the loop control variable so that it's not an infinite loop
        questionNumber++;
    }

  5. #5
    Junior Member
    Join Date
    Feb 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Is there anyway to use a Do While statement in this program?

    would i do this for all questions? I want to make each correct answer count as a point and display amount of points at the end

    Here is the full code:

    import javax.swing.JOptionPane;
     
    public class History {
     
     
        public static void main(String[] args) 
        {
     
           //First Question: What is the capital of Mexico? 
            String[] question1 = {"Mexico City", "Paris"    //The choseable answers are made using String
                    ,"Washington D.C", "Tokyo"};
     
     
            String MexicoCapital = (String)JOptionPane.showInputDialog(null, "What is the capital of Mexico?:"
                    , "Question 1 of 10", JOptionPane.QUESTION_MESSAGE // question number is diplayed on top of text box
                    , null, question1, question1[0]); //Choices are called and displayed using joptionpane
     
     
            if (MexicoCapital == null)
            {
                JOptionPane.showMessageDialog(null, "You didn't pick a city!");
            }
            else 
            {
     
                String funfact = "";
                switch (MexicoCapital)
                {
                    case "Mexico City":
                        funfact = "Correct";
                        break;
                    case "Paris":
                        funfact = "Incorrect! Paris is the capital of France";
                        break;
                    case "Washington D.C":
                        funfact = "Incorrect! Washington D.C is the capital of the United States";
                        break;
                    case "Tokyo":
                        funfact = "Incorrect! Tokyo is the capital of Japan";
                        break;
                    default:
                        funfact = "Something went wrong with picking an answer ";
                        break;
                }
                JOptionPane.showMessageDialog(null, funfact);
            }
    //Second Question: When was modern Mexico established?
     
        String[] question2 = {"1954", "1897", "1929", "1789"};
     
        String secondQuestion = (String)JOptionPane.showInputDialog(null, " When was modern Mexico established?:"
                    , "Question 2 of 10", JOptionPane.QUESTION_MESSAGE
                    , null, question2, question2[0]);
        String funfact = "";
                switch (secondQuestion)
                {
                    case "1954":
                        funfact = "Incorrect, Too Late";
                        break;
                    case "1897":
                        funfact = "Incorrect, Mexico was still a dictatorship then.";
                        break;
                    case "1929":
                        funfact = "Correct, In 1929 the Institutional Revolutionary Party took contol of Mexico";
                        break;
                    case "1789":
                        funfact = "Incorrect, Spain controlled Mexico until 1810";
                        break;
                    default:
                        funfact = "Something went wrong with picking an answer ";
                        break;
                }
                JOptionPane.showMessageDialog(null, funfact);
     
    //Third Question:"When did Spain first occupy Mexico?            
    String[] question3 = {"1923", "1700", "1492", "1834"};
     
    String thirdQuestion = (String)JOptionPane.showInputDialog(null, "When did Spain first occupy Mexico?:"
                    , "Question 3 of 10", JOptionPane.QUESTION_MESSAGE
                    , null, question3, question3[0]);
        String funfacts = "";
                switch (thirdQuestion)
                {
                    case "1923":
                        funfacts = "Incorrect, Too Late";
                        break;
                    case "1700":
                        funfacts= "Incorrect, Too Late";
                        break;
                    case "1492":
                        funfacts = "Correct,Spain first took control of Mexico in 1492";
                        break;
                    case "1289":
                        funfacts = "Incorrect,The Aztecs had control until 1492";
                        break;
                    default:
                        funfacts = "Something went wrong with picking an answer";
                        break;
                }
                JOptionPane.showMessageDialog(null, funfacts);
     
    //Fourth Question: Which of these foods was NOT introduced by Mexicans?            
    String[] question11 = {"Carrots", "Corn", "Chocolate", "Chili Peppers"};
    String fourthQuestion = (String)JOptionPane.showInputDialog(null, "Which of these foods was NOT introduced by Mexicans?:"
                    , "Question 4 of 10", JOptionPane.QUESTION_MESSAGE
                    , null, question11, question11[0]);
        String funfact2 = "";
                switch (fourthQuestion)
                {
                    case "Carrots":
                        funfact2 = "Correct, Carrots came from Afghanistan";
                        break;
                    case "Corn":
                        funfact2 = "Incorrect,Corn DID originate in Mexico";
                        break;
                    case "Chocolate":
                        funfact2 = "Incorrect,Chocolate DID originate in Mexico";
                        break;
                    case "Chili Peppers":
                        funfact2 = "Incorrect,Chili Peppers DID originate in Mexico";
                        break;
                    default:
                        funfact2 = "Something went wrong with picking an answer ";
                        break;
                }
                JOptionPane.showMessageDialog(null, funfact2);
     
    //Fifth Question: What battle ended the Texas Revolution? 
    String[] question5 = {"Battle of Palo Alto", "Alamo", "Battle of Churubusco", "Battle of Puebla"};
     
    String fifthQuestion = (String)JOptionPane.showInputDialog(null, "What battle ended the Texas Revolution?"
                    , "Question 5 of 10", JOptionPane.QUESTION_MESSAGE
                    , null, question5, question5[0]);
        String funfacts3 = "";
                switch (fifthQuestion)
                {
                    case "Battle of Palo Alto":
                        funfacts3 = "Incorrect,The Battle of Palo Alto was during the Mexican-American War";
                        break;
                    case "Alamo":
                        funfacts3 = "Correct,The battle for the Alamo was the battle that ended the Texas Revolution";
                        break;
                    case "Battle of Churubusco":
                        funfacts3 = "Incorrect,The Battle of Churubusco was during the Mexican-American War";
                        break;
                    case "Battle of Puebla":
                        funfacts3 = "Incorrect,The Battle of Puebla was a revolt against the French";
                        break;
                    default:
                        funfacts3 = "Something went wrong with picking an answer ";
                        break;
                }
                JOptionPane.showMessageDialog(null, funfacts3);
     
    //Sixth Question: How many wars has Mexico been in?    
    String[] question6 = {"50", "15", "32", "79"};
     
    String sixthQuestion = (String)JOptionPane.showInputDialog(null, "How many wars has Mexico been in?"
                    , "Question 6 of 10", JOptionPane.QUESTION_MESSAGE
                    , null, question6, question6[0]);
        String funfacts4 = "";
                switch (sixthQuestion)
                {
                    case "50":
                        funfacts4 = "Incorrect,Mexico has only been in 32 official wars";
                        break;
                    case "15":
                        funfacts4 = "Incorrect, Mexico has been in 32 official wars ";
                        break;
                    case "32":
                        funfacts4 = "Correct, Mexico has only been in a total of 32 official wars since the 1600's ";
                        break;
                    case "70":
                        funfacts4 = "Incorrect, Mexico has only been in 32 official wars";
                        break;
                    default:
                        funfacts4 = "Something went wrong with picking an answer ";
                        break;
                }
                JOptionPane.showMessageDialog(null, funfacts4);         
     
    //Seventh Question:Who is the current President of Mexico?
    String[] question7 = {"Fredrik Reinfeldt", "François Hollande", "Nicolás Maduro", "Enrique Peña Nieto"};
     
    String seventhQuestion = (String)JOptionPane.showInputDialog(null, "Who is the current President of Mexico?"
                    , "Question 7 of 10", JOptionPane.QUESTION_MESSAGE
                    , null, question7, question7[0]);
        String funfacts5 = "";
                switch (seventhQuestion)
                {
                    case "Fredrik Reinfeldt":
                        funfacts5 = "Incorrect,Fredrik Reinfeldt is the Prime Minister of Sweden ";
                        break;
                    case "François Hollande":
                        funfacts5 = "Incorrect,François Hollande is the President of France";
                        break;
                    case "Nicolás Maduro":
                        funfacts5 = "Incorrect,Nicolás Maduro is the President of Venezuela";
                        break;
                    case "Enrique Peña Nieto":
                        funfacts5 = "Correct,Enrique Peña Nieto is the President of Mexico as of December 1,2012";
                        break;
                    default:
                        funfacts5 = "Something went wrong with picking an answer ";
                        break;
                }
                JOptionPane.showMessageDialog(null, funfacts5);         
     
     //Eighth Question: Which of these countries does Mexico NOT border?
    String[] question8 = {"Belize", "United States of America", "Guatemala", "Panama"};
     
    String eighthQuestion = (String)JOptionPane.showInputDialog(null, "Which of these countries does Mexico NOT border?"
                    , "Question 8 of 10", JOptionPane.QUESTION_MESSAGE
                    , null, question8, question8[0]);
        String funfacts6 = "";
                switch (eighthQuestion)
                {
                    case "Belize":
                        funfacts6 = "Incorrect,Belize borders Mexico to the southeast";
                        break;
                    case "United States of America":
                        funfacts6 = "Incorrect,The United States of America borders Mexico to the north";
                        break;
                    case "Guatemala":
                        funfacts6 = "Incorrect,Guatemala borders Mexico to the southeast";
                        break;
                    case "Panama":
                        funfacts6 = "Correct,Panama does not border Mexico,they are seperated by other countries";
                        break;
                    default:
                        funfacts6 = "Something went wrong with picking an answer ";
                        break;
                }
                JOptionPane.showMessageDialog(null, funfacts6); 
     
    //Ninth Question:What continent is Mexico located in?            
    String[] question9 = {"South America", "Europe", "North America", "Africa"};
     
    String ninthQuestion = (String)JOptionPane.showInputDialog(null, "What continent is Mexico located in?"
                    , "Question 9 of 10", JOptionPane.QUESTION_MESSAGE
                    , null, question9, question9[0]);
        String funfacts7 = "";
                switch (ninthQuestion)
                {
                    case "South America":
                        funfacts7 = "Incorrect,Mexico is located in North America";
                        break;
                    case "Europe":
                        funfacts7 = "Incorrect,Mexico is located in North America";
                        break;
                    case "North America":
                        funfacts7 = "Correct,Mexico is located south of the United States of America, which is in North America";
                        break;
                    case "Africa":
                        funfacts7 = "Incorrect,Mexico is located in North America";
                        break;
                    default:
                        funfacts7 = "Something went wrong with picking an answer ";
                        break;
                }
                JOptionPane.showMessageDialog(null, funfacts7);         
     
    //Tenth Question:What is Mexico’s official name?
    String[] question10 = {"United Mexican States", "Mexican Republic", "Democratic Nation of Mexico", "Kingdom of Mexico"};
     
    String tenthQuestion = (String)JOptionPane.showInputDialog(null, "What is Mexico’s official name?"
                    , "Final Question", JOptionPane.QUESTION_MESSAGE
                    , null, question10, question10[0]);
        String funfacts8 = "";
                switch (tenthQuestion)
                {
                    case "United Mexican States":
                        funfacts8 = "Correct,Mexico's official name is the United Mexican States.";
                        break;
                    case "Mexican Republic":
                        funfacts8 = "Incorrect,Mexico is not a republic it is a democracy";
                        break;
                    case "Democratic Nation of Mexico":
                        funfacts8 = "Incorrect,Mexico's official name is the United Mexican States." ;
                        break;
                    case "Kingdom of Mexico":
                        funfacts8 = "Incorrect,Mexico is not a kingdom, it is a democracy";
                        break;
                    default:
                        funfacts8 = "Something went wrong with picking an answer ";
                        break;
                }
                JOptionPane.showMessageDialog(null, funfacts8);          
    //Closing Statements
    JOptionPane.showMessageDialog(null,"Thank you for taking the History quiz.We hope you will also play the trivia game.");        
            }
                }

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Is there anyway to use a Do While statement in this program?

    I'm guessing you don't yet know arrays, use of methods beyond the main() method, or use of helper classes. If you can't generalize the asking of questions and providing answers, then the purpose of a loop is not clear.

    Start over. If your program works now as it is, what are you trying to do? What is the requirement either as stated in the original assignment or resulting from instructor review, etc.? It would also be helpful to know what programming tools (like those I've mentioned above) you can, can't, or must use.

  7. #7
    Junior Member
    Join Date
    Feb 2014
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Is there anyway to use a Do While statement in this program?

    Quote Originally Posted by Kyllian View Post
    would i do this for all questions? I want to make each correct answer count as a point and display amount of points at the end

    Here is the full code:

    import javax.swing.JOptionPane;
     
    public class History {
     
     
        public static void main(String[] args) 
        {
     
           //First Question: What is the capital of Mexico? 
            String[] question1 = {"Mexico City", "Paris"    //The choseable answers are made using String
                    ,"Washington D.C", "Tokyo"};
     
     
            String MexicoCapital = (String)JOptionPane.showInputDialog(null, "What is the capital of Mexico?:"
                    , "Question 1 of 10", JOptionPane.QUESTION_MESSAGE // question number is diplayed on top of text box
                    , null, question1, question1[0]); //Choices are called and displayed using joptionpane
     
     
            if (MexicoCapital == null)
            {
                JOptionPane.showMessageDialog(null, "You didn't pick a city!");
            }
            else 
            {
     
                String funfact = "";
                switch (MexicoCapital)
                {
                    case "Mexico City":
                        funfact = "Correct";
                        break;
                    case "Paris":
                        funfact = "Incorrect! Paris is the capital of France";
                        break;
                    case "Washington D.C":
                        funfact = "Incorrect! Washington D.C is the capital of the United States";
                        break;
                    case "Tokyo":
                        funfact = "Incorrect! Tokyo is the capital of Japan";
                        break;
                    default:
                        funfact = "Something went wrong with picking an answer ";
                        break;
                }
                JOptionPane.showMessageDialog(null, funfact);
            }
    //Second Question: When was modern Mexico established?
     
        String[] question2 = {"1954", "1897", "1929", "1789"};
     
        String secondQuestion = (String)JOptionPane.showInputDialog(null, " When was modern Mexico established?:"
                    , "Question 2 of 10", JOptionPane.QUESTION_MESSAGE
                    , null, question2, question2[0]);
        String funfact = "";
                switch (secondQuestion)
                {
                    case "1954":
                        funfact = "Incorrect, Too Late";
                        break;
                    case "1897":
                        funfact = "Incorrect, Mexico was still a dictatorship then.";
                        break;
                    case "1929":
                        funfact = "Correct, In 1929 the Institutional Revolutionary Party took contol of Mexico";
                        break;
                    case "1789":
                        funfact = "Incorrect, Spain controlled Mexico until 1810";
                        break;
                    default:
                        funfact = "Something went wrong with picking an answer ";
                        break;
                }
                JOptionPane.showMessageDialog(null, funfact);
     
    //Third Question:"When did Spain first occupy Mexico?            
    String[] question3 = {"1923", "1700", "1492", "1834"};
     
    String thirdQuestion = (String)JOptionPane.showInputDialog(null, "When did Spain first occupy Mexico?:"
                    , "Question 3 of 10", JOptionPane.QUESTION_MESSAGE
                    , null, question3, question3[0]);
        String funfacts = "";
                switch (thirdQuestion)
                {
                    case "1923":
                        funfacts = "Incorrect, Too Late";
                        break;
                    case "1700":
                        funfacts= "Incorrect, Too Late";
                        break;
                    case "1492":
                        funfacts = "Correct,Spain first took control of Mexico in 1492";
                        break;
                    case "1289":
                        funfacts = "Incorrect,The Aztecs had control until 1492";
                        break;
                    default:
                        funfacts = "Something went wrong with picking an answer";
                        break;
                }
                JOptionPane.showMessageDialog(null, funfacts);
     
    //Fourth Question: Which of these foods was NOT introduced by Mexicans?            
    String[] question11 = {"Carrots", "Corn", "Chocolate", "Chili Peppers"};
    String fourthQuestion = (String)JOptionPane.showInputDialog(null, "Which of these foods was NOT introduced by Mexicans?:"
                    , "Question 4 of 10", JOptionPane.QUESTION_MESSAGE
                    , null, question11, question11[0]);
        String funfact2 = "";
                switch (fourthQuestion)
                {
                    case "Carrots":
                        funfact2 = "Correct, Carrots came from Afghanistan";
                        break;
                    case "Corn":
                        funfact2 = "Incorrect,Corn DID originate in Mexico";
                        break;
                    case "Chocolate":
                        funfact2 = "Incorrect,Chocolate DID originate in Mexico";
                        break;
                    case "Chili Peppers":
                        funfact2 = "Incorrect,Chili Peppers DID originate in Mexico";
                        break;
                    default:
                        funfact2 = "Something went wrong with picking an answer ";
                        break;
                }
                JOptionPane.showMessageDialog(null, funfact2);
     
    //Fifth Question: What battle ended the Texas Revolution? 
    String[] question5 = {"Battle of Palo Alto", "Alamo", "Battle of Churubusco", "Battle of Puebla"};
     
    String fifthQuestion = (String)JOptionPane.showInputDialog(null, "What battle ended the Texas Revolution?"
                    , "Question 5 of 10", JOptionPane.QUESTION_MESSAGE
                    , null, question5, question5[0]);
        String funfacts3 = "";
                switch (fifthQuestion)
                {
                    case "Battle of Palo Alto":
                        funfacts3 = "Incorrect,The Battle of Palo Alto was during the Mexican-American War";
                        break;
                    case "Alamo":
                        funfacts3 = "Correct,The battle for the Alamo was the battle that ended the Texas Revolution";
                        break;
                    case "Battle of Churubusco":
                        funfacts3 = "Incorrect,The Battle of Churubusco was during the Mexican-American War";
                        break;
                    case "Battle of Puebla":
                        funfacts3 = "Incorrect,The Battle of Puebla was a revolt against the French";
                        break;
                    default:
                        funfacts3 = "Something went wrong with picking an answer ";
                        break;
                }
                JOptionPane.showMessageDialog(null, funfacts3);
     
    //Sixth Question: How many wars has Mexico been in?    
    String[] question6 = {"50", "15", "32", "79"};
     
    String sixthQuestion = (String)JOptionPane.showInputDialog(null, "How many wars has Mexico been in?"
                    , "Question 6 of 10", JOptionPane.QUESTION_MESSAGE
                    , null, question6, question6[0]);
        String funfacts4 = "";
                switch (sixthQuestion)
                {
                    case "50":
                        funfacts4 = "Incorrect,Mexico has only been in 32 official wars";
                        break;
                    case "15":
                        funfacts4 = "Incorrect, Mexico has been in 32 official wars ";
                        break;
                    case "32":
                        funfacts4 = "Correct, Mexico has only been in a total of 32 official wars since the 1600's ";
                        break;
                    case "70":
                        funfacts4 = "Incorrect, Mexico has only been in 32 official wars";
                        break;
                    default:
                        funfacts4 = "Something went wrong with picking an answer ";
                        break;
                }
                JOptionPane.showMessageDialog(null, funfacts4);         
     
    //Seventh Question:Who is the current President of Mexico?
    String[] question7 = {"Fredrik Reinfeldt", "François Hollande", "Nicolás Maduro", "Enrique Peña Nieto"};
     
    String seventhQuestion = (String)JOptionPane.showInputDialog(null, "Who is the current President of Mexico?"
                    , "Question 7 of 10", JOptionPane.QUESTION_MESSAGE
                    , null, question7, question7[0]);
        String funfacts5 = "";
                switch (seventhQuestion)
                {
                    case "Fredrik Reinfeldt":
                        funfacts5 = "Incorrect,Fredrik Reinfeldt is the Prime Minister of Sweden ";
                        break;
                    case "François Hollande":
                        funfacts5 = "Incorrect,François Hollande is the President of France";
                        break;
                    case "Nicolás Maduro":
                        funfacts5 = "Incorrect,Nicolás Maduro is the President of Venezuela";
                        break;
                    case "Enrique Peña Nieto":
                        funfacts5 = "Correct,Enrique Peña Nieto is the President of Mexico as of December 1,2012";
                        break;
                    default:
                        funfacts5 = "Something went wrong with picking an answer ";
                        break;
                }
                JOptionPane.showMessageDialog(null, funfacts5);         
     
     //Eighth Question: Which of these countries does Mexico NOT border?
    String[] question8 = {"Belize", "United States of America", "Guatemala", "Panama"};
     
    String eighthQuestion = (String)JOptionPane.showInputDialog(null, "Which of these countries does Mexico NOT border?"
                    , "Question 8 of 10", JOptionPane.QUESTION_MESSAGE
                    , null, question8, question8[0]);
        String funfacts6 = "";
                switch (eighthQuestion)
                {
                    case "Belize":
                        funfacts6 = "Incorrect,Belize borders Mexico to the southeast";
                        break;
                    case "United States of America":
                        funfacts6 = "Incorrect,The United States of America borders Mexico to the north";
                        break;
                    case "Guatemala":
                        funfacts6 = "Incorrect,Guatemala borders Mexico to the southeast";
                        break;
                    case "Panama":
                        funfacts6 = "Correct,Panama does not border Mexico,they are seperated by other countries";
                        break;
                    default:
                        funfacts6 = "Something went wrong with picking an answer ";
                        break;
                }
                JOptionPane.showMessageDialog(null, funfacts6); 
     
    //Ninth Question:What continent is Mexico located in?            
    String[] question9 = {"South America", "Europe", "North America", "Africa"};
     
    String ninthQuestion = (String)JOptionPane.showInputDialog(null, "What continent is Mexico located in?"
                    , "Question 9 of 10", JOptionPane.QUESTION_MESSAGE
                    , null, question9, question9[0]);
        String funfacts7 = "";
                switch (ninthQuestion)
                {
                    case "South America":
                        funfacts7 = "Incorrect,Mexico is located in North America";
                        break;
                    case "Europe":
                        funfacts7 = "Incorrect,Mexico is located in North America";
                        break;
                    case "North America":
                        funfacts7 = "Correct,Mexico is located south of the United States of America, which is in North America";
                        break;
                    case "Africa":
                        funfacts7 = "Incorrect,Mexico is located in North America";
                        break;
                    default:
                        funfacts7 = "Something went wrong with picking an answer ";
                        break;
                }
                JOptionPane.showMessageDialog(null, funfacts7);         
     
    //Tenth Question:What is Mexico’s official name?
    String[] question10 = {"United Mexican States", "Mexican Republic", "Democratic Nation of Mexico", "Kingdom of Mexico"};
     
    String tenthQuestion = (String)JOptionPane.showInputDialog(null, "What is Mexico’s official name?"
                    , "Final Question", JOptionPane.QUESTION_MESSAGE
                    , null, question10, question10[0]);
        String funfacts8 = "";
                switch (tenthQuestion)
                {
                    case "United Mexican States":
                        funfacts8 = "Correct,Mexico's official name is the United Mexican States.";
                        break;
                    case "Mexican Republic":
                        funfacts8 = "Incorrect,Mexico is not a republic it is a democracy";
                        break;
                    case "Democratic Nation of Mexico":
                        funfacts8 = "Incorrect,Mexico's official name is the United Mexican States." ;
                        break;
                    case "Kingdom of Mexico":
                        funfacts8 = "Incorrect,Mexico is not a kingdom, it is a democracy";
                        break;
                    default:
                        funfacts8 = "Something went wrong with picking an answer ";
                        break;
                }
                JOptionPane.showMessageDialog(null, funfacts8);          
    //Closing Statements
    JOptionPane.showMessageDialog(null,"Thank you for taking the History quiz.We hope you will also play the trivia game.");        
            }
                }
    Your code really dirty, let make it more clearly & reusable instead.

    Example:

    Make a function to show UI
    void showQuestion(MyQuestion question)
    void showQuestion(String question, String[] option, string[] optionAnswer){
    //in this function, you will show question to user, for each option, you will have a optionAnswer to show the result to the user
    //Flow:
    // ShowQuestion(question)
    // ShowOption(option)
    // int selected = GetUserSelectedOption();
    // ShowResult(optionAnswer[selected])
    }
    well, all you need to do now is init your list of question, option, & optionAnswer, you could make a class to hold these data, like this
    classs MyQuestion{
    String question
    String[] option,
    String[] optionAnswer
    public MyQuestion(String,String[], String[]){}
    }
    ok, then you nit to init your list question:
    List<MyQuestion> questionCollection = new ArraayList<>();
    questionCollection.add(new MyQuestion("1 + 1 = ?",{"2","3","4"},{"yes, u right","no you wrong it's 2","no you need to learn it again"}));


    and then you go through your question and show it

    for(MyQuestion q : questionCollection){
    ShowQuestion(q);
    }
    ShowEndAndResult();

    OK ?

Similar Threads

  1. My program skips an if statement I want to include in my loop--don't know why
    By ciscocoder in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 3rd, 2013, 09:53 AM
  2. [SOLVED] A Loop statement and a switch statement issue
    By sternfox in forum Loops & Control Statements
    Replies: 13
    Last Post: March 7th, 2013, 04:19 PM
  3. Help with If- Else statement program please
    By Tctwins in forum Loops & Control Statements
    Replies: 6
    Last Post: February 20th, 2012, 04:58 PM
  4. how to make program of Depreciation using if statement and loops
    By Pulsifier in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 16th, 2011, 11:23 PM
  5. Java Program Help Switch Statement
    By jwill22 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 11th, 2010, 12:31 AM