java gui if statment help
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: java gui if statment help

  1. #1
    Join Date
    Feb 2013
    Posts
    22

    java gui if statment help

    I'm finishing up my code to my java program and its basically going to read questions from the array, ask user for the answer(a, b, c, or d). Then I'm going to make it calculate number wrong and right. I'm stuck on the part where my if statement only displays my else case "wrong". Here's the code thanks


    Code:
    import javax.swing.JOptionPane;
    import java.text.DecimalFormat;
    
    /**
    	This program is a guiquiz
    */
    
    public class GuiQuiz
    {
    	public static void main(String[] args)
    	{
    
    
    		String[] questions = { "What is science", "How many piers", "What is economics",
    			"What year is columbus" };
    
    		JOptionPane.showInputDialog(null, questions[0] + "\nA" + " metal," + "\nB. An art, \nC, Blue. \nD.Rockets");
    
    			parseInt;
    			if(questions[0].equalsIgnoreCase("a"))
    			{
    				System.out.println("Correct");
    
    			}
    			else
    			{
    				JOptionPane.showMessageDialog(null, "WRONG");
    
    			}
    PS I am going to make another array with the correct answers so it can be easier calculating but, I want the first answer to be a but no matter what I put even a I get "WRONG"

  2. #2
    Join Date
    Jun 2011
    Location
    .NET4.0 / VS 2010
    Posts
    71

    Re: java gui if statment help

    Quote Originally Posted by jumpman8947 View Post

    Code:
    import javax.swing.JOptionPane;
    import java.text.DecimalFormat;
    
    /**
    	This program is a guiquiz
    */
    
    public class GuiQuiz
    {
    	public static void main(String[] args)
    	{
                    // Creating array to hold questions
    		String[] questions = { "What is science", "How many piers", "What is economics",
    			"What year is columbus" };
                    // Show input dialog but not storing the answer into any variable
    		JOptionPane.showInputDialog(null, questions[0] + "\nA metal, \nB. An art, \nC, Blue. \nD.Rockets");
    
    			// Checking array question at first position to see if it matches "a"
                            // You need to store the input from the dialog into a new array and compare that not the question itself
    			if(questions[0].equalsIgnoreCase("a"))
    			{
    				System.out.println("Correct");
    
    			}
    			else
    			{
    				JOptionPane.showMessageDialog(null, "WRONG");
    
    			}
    You keep getting wrong because you are not saving any of the input from the dialog box. You show the question and show the answers but no variable to store it in.

    Code:
    String usrAnswer = JOptionPane.showInputDialog(null, questions[0] + "\nA metal, \nB. An art, \nC, Blue. \nD.Rockets");
    Next you would just need to check if usrAnswer is the correct answer within your if statements.

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

This is a CodeGuru survey question.


Featured


HTML5 Development Center