Results 1 to 7 of 7
  1. #1
    pipikrk is offline Member
    Join Date
    Apr 2010
    Posts
    34
    Rep Power
    0

    Default joptionpane quizz , improve my code ....

    i want to make kind of quizz with joption pane,
    can someone give me tips , how to improve my code??

    Java Code:
    import javax.swing.JOptionPane;
    
    public class Main {
    
    
        public static void main(String[] args) {
    
    String no = null;
    no = JOptionPane.showInputDialog("sum of 229+566=");
    if (    no == null ? "sum" == null : no.equals("795")) {
    JOptionPane.showMessageDialog(null, "correct  " + no);
    } else
    {JOptionPane.showMessageDialog(null, "wrong " + no);}
    
    String nom = null;
    nom = JOptionPane.showInputDialog("mul of 229x566=");
    if (    nom == null ? "sum" == null : nom.equals("129614")) {
    JOptionPane.showMessageDialog(null, "correct  " + nom);
    } else
    {JOptionPane.showMessageDialog(null, "wrong " + nom);}
    
    
        }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,445
    Blog Entries
    7
    Rep Power
    20

    Default

    Does your code work as you want it to work? Yes? There's a saying: "if it ain't broke, don't fix it".

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    pipikrk is offline Member
    Join Date
    Apr 2010
    Posts
    34
    Rep Power
    0

    Default

    it is working, but i want to put 50 questions , so i have to do this all the time!
    there is no better way i can fix it???

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,445
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by pipikrk View Post
    it is working, but i want to put 50 questions , so i have to do this all the time!
    there is no better way i can fix it???
    Sure there is: build a method for it and pass parameters for the part(s) that change per call of the method.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    pipikrk is offline Member
    Join Date
    Apr 2010
    Posts
    34
    Rep Power
    0

    Default

    can u please give a quick example ??

    many thanks

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,445
    Blog Entries
    7
    Rep Power
    20

    Default

    Sure, this is a quick example:

    Java Code:
    boolean sumQuestion(int x, int y) {
       return (""+(x+y)).equals(JOptionPane.showInputDialog("sum of "+x+"+"+y+"=");
    }
    Because it's spoonfeeding, you have to figure out its details.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,785
    Rep Power
    7

    Default

    Quote Originally Posted by pipikrk View Post
    how to improve my code??
    Fix the gawd awful if statements.

    Java Code:
    if ("795".equals(no)) {

Similar Threads

  1. What can i do to improve this? Explanation please
    By biggerthanblue in forum New To Java
    Replies: 1
    Last Post: 02-19-2011, 06:55 AM
  2. Improve my GUI!
    By AJArmstron@aol.com in forum New To Java
    Replies: 8
    Last Post: 04-27-2010, 09:17 PM
  3. How to improve this code
    By Raymond in forum Advanced Java
    Replies: 9
    Last Post: 03-19-2010, 08:16 AM
  4. how to improve my security?
    By anthrax in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 03-13-2009, 09:08 AM
  5. Replies: 16
    Last Post: 08-05-2008, 11:34 AM

Posting Permissions

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