Results 1 to 3 of 3
  1. #1
    incxx is offline Member
    Join Date
    Mar 2013
    Posts
    21
    Rep Power
    0

    Default Help with methods in Java?

    My program is a math game which consists of different levels and different math operations. I'm not completely finished yet but I would like to know if there is a way to put the code for each of the four operators into their own method, and just have the main method print the questions and ask for the answers? Also, why when I run my program, when the user enters their answer, does the pop up close and make me enter it in the program (not in a window)?
    Java Code:
    public class mathgame
    {
      
      public static void main (String [] args) throws IOException
      {
        BufferedReader myInput = new BufferedReader (new InputStreamReader (System.in));// Buffered Reader reads the number inputed by the user 
        
        int money = 0;
        
       
        String player = JOptionPane.showInputDialog(null, "Welcome to... \n - Are YOU Smarter Than a 12 Year Old? - \n Please enter your name to begin!", "Welcome", JOptionPane.INFORMATION_MESSAGE);
       
       JOptionPane.showMessageDialog(null, "Hi " + player + ", " + " let's see if you are really smarter than a 12 year old. \n This games consists of 3 levels of difficulty. \n Answer all 4 questions in each level and earn $500, 000! \n If you get an answer wrong you lose $100, you go home EMPTY HANDED if your money reaches 0!");
       
       Object[] options = {"Yes!", "No way!"};
       
       int x = JOptionPane.showOptionDialog(null,"Are you ready to play?","Start?", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
    
    if (x == JOptionPane.YES_OPTION) {
      
       JOptionPane.showMessageDialog(null, "Your first level consists of addition and substraction of 2 numbers! \n In order to pass this level, you will need to answer all 3 questions correctly. \n For every correct question, you will earn $1,000.");
       
       int addA = leveloneA();
       int addB = leveloneB();
       String sumA = JOptionPane.showInputDialog(null, addA + "+" + addB + " = ", "Question", JOptionPane.INFORMATION_MESSAGE);
       sumA = myInput.readLine();
       int sum = Integer.parseInt (sumA);
      
       int realSum = addA + addB;
       JOptionPane.showMessageDialog(null, realSum);
      
       if (sum == realSum){
         
         money = money + 1000;
         
          JOptionPane.showMessageDialog(null, "CORRECT! \n You have $" + money);
       }
          if (sum != realSum){
            JOptionPane.showMessageDialog(null, "INCORRECT! \n You are not smarter than a 12 year old. \n You go home with $" + money);
            System.exit(0);
          }
     }
    else if (x == JOptionPane.NO_OPTION){
      JOptionPane.showMessageDialog(null, "Goodbye!");
      System.exit(0);
    }
    
       int subA = leveloneA();
       int subB = leveloneB();
       
       if (subA < subB){
         subA = subB;
         subB = subA;
       }
       String differenceA = JOptionPane.showInputDialog(null, subA + " - " + subB + " = ", "Question", JOptionPane.INFORMATION_MESSAGE);
       differenceA = myInput.readLine();
       int difference = Integer.parseInt (differenceA);
       
       int realDifference = subA - subB;
       JOptionPane.showMessageDialog(null, realDifference);
       if (difference == realDifference){
        
         money = money + 1000;
         
          JOptionPane.showMessageDialog(null, "CORRECT! \n You have $" + money);
       }
       else if (difference != realDifference){
       
         JOptionPane.showMessageDialog(null, "INCORRECT! \n You are not smarter than a 12 year old. \n You go home with $" + money);
         System.exit(0);
       }
      }
    
      public static int leveloneA ()
      {
        int firstNum = (int)(Math.random()*20);
        return firstNum;
      }
       public static int leveloneB ()
      {
        int secondNum = (int)(Math.random()*20);
        return secondNum;
      }
       
       }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,413
    Rep Power
    5

    Default Re: Help with methods in Java?

    First, comment out the lines:

    Java Code:
    sumA = myInput.readLine();
    differenceA = myInput.readLine();
    You are already getting the answer from the dialog box.

    And yes you can use a method to calculate the values. There are lots of ways to do it. The most straight forward way would be something like the following:

    Java Code:
    public int calc(int op1, int op2, String operand) {
       int answer;    
       switch (operand) {
          case "+": // java 1.7+  allows Strings
           // code  
          case "-"; 
           //code
          //        ...
       } 
        return answer;
    }
    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    incxx is offline Member
    Join Date
    Mar 2013
    Posts
    21
    Rep Power
    0

    Default Re: Help with methods in Java?

    Quote Originally Posted by jim829 View Post
    And yes you can use a method to calculate the values. There are lots of ways to do it. The most straight forward way would be something like the following:

    Java Code:
    public int calc(int op1, int op2, String operand) {
       int answer;    
       switch (operand) {
          case "+": // java 1.7+  allows Strings
           // code  
          case "-"; 
           //code
          //        ...
       } 
        return answer;
    }
    Regards,
    Jim
    If I put each operator in its own method, for example all the code for the addition, when I call that method from the main method, I can only call one variable, but I would need to call the firstNum, secondNum and the sum. Where can I place all the if statements and Joptionpane pertaining to the addition?

Similar Threads

  1. Replies: 1
    Last Post: 07-12-2012, 11:04 PM
  2. Replies: 2
    Last Post: 07-12-2012, 11:04 PM
  3. Replies: 8
    Last Post: 04-16-2012, 11:00 PM
  4. Java Noob, trying to call methods from another methods
    By gabrielpr12 in forum New To Java
    Replies: 8
    Last Post: 11-17-2011, 09:07 PM
  5. Replies: 1
    Last Post: 10-23-2011, 12:37 PM

Posting Permissions

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