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

Thread: Java help with Methods (Begginer)

  1. #1
    Junior Member
    Join Date
    Jun 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java help with Methods (Begginer)

    We're supposed to use methods/functions in our program, but im having trouble incorporating them into it. My program is like a math game, asking simple addition and addition/subtraction questions. I want all the calculations to be in methods but i'm having trouble returning a whole equation back into the main method (sorry if that doesnt make sense, maybe the code will help better).

    I need some assistance when it comes to the direction I need to go in order to use methods effectively! As well as what should be in its own methods and what should be in the main method.
    Thanks so much for the help

    CODE:

    import javax.swing.*;
    import java.io.*;


    public class SummativeTwo
    {
    public static void main (String[] args) throws IOException
    {
    BufferedReader myInput = new BufferedReader (new InputStreamReader (System.in));
    {

    Object[] options = {"Yes! I'm Excited!","Im kind of nervous..","Now Way! Ewww!"};
    int n = JOptionPane.showOptionDialog(null, "Welcome Million Dollar Madness! "+ "Are you ready to play?","Welcome",JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE,null,options,options[2]);
    if (n == JOptionPane.YES_OPTION) {
    JOptionPane.showMessageDialog(null,"Okie Dokie! Lets get started!", "Okie Dokie", JOptionPane.INFORMATION_MESSAGE);
    }
    if (n == JOptionPane.NO_OPTION) {
    JOptionPane.showMessageDialog(null,"Aw thats okay! Im sure you'll have fun!", "It's okay", JOptionPane.INFORMATION_MESSAGE);
    }
    if (n == JOptionPane.CANCEL_OPTION) {
    JOptionPane.showMessageDialog(null,"Well too bad so sad! You're going to have to play anyways!", "Too bad so sad", JOptionPane.INFORMATION_MESSAGE);
    }

    Object[] optionsA = {"Yes Please", "Nope! I'm good!"};
    int x = JOptionPane.showOptionDialog(null,"Would you like to know the instructions for this game?","INSTRUCTIONS?", JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MES SAGE,null,optionsA,optionsA[0]);
    if (x == JOptionPane.YES_OPTION) {
    JOptionPane.showMessageDialog(null,"There will be 3 levels in this game. As you go farther, the questions get harder and harder and each question \n. Each time you get a question right you will recieve a certain amount of money, which increases each time you pass each question. \n But be careful, because if you get 3 questions wrong in one level \n it's GAME OVER for you! Meaning you lose all your money. \n You may Cash Out at any point if you're not willing to risk losing the amount of money you have. Reach the end of \n the 3 levels and you take home all your money! \n GOOD LUCK!", "Instructions", JOptionPane.INFORMATION_MESSAGE);
    }
    if (x == JOptionPane.NO_OPTION) {
    JOptionPane.showMessageDialog(null,"Suit Yourself! Lets Start!", "Your Choice", JOptionPane.INFORMATION_MESSAGE);
    }
    JOptionPane.showMessageDialog(null,"Welcome to Level: 1 " ," level 1", JOptionPane.INFORMATION_MESSAGE);

    //LEVEL ONE
    int wrong = 0;
    int run = 1;
    int questionnum = 0;
    int amount = 500;

    while (run <= 6 && wrong != 3)
    {
    run ++;
    questionnum++;
    int increment = 150;
    amount += 150;

    int numOne = additionOne ();
    int numTwo = additionTwo ();
    int answer = numOne + numTwo;


    String useranswerA = JOptionPane.showInputDialog(null,"Question #" + questionnum + " is for: $" + increment + "\n" + numOne + " + " + numTwo + " = ?", "Question", JOptionPane.INFORMATION_MESSAGE);
    useranswerA = myInput.readLine ();
    int useranswer = Integer.parseInt(useranswerA);

    if (useranswer != answer)
    {
    wrong ++;
    JOptionPane.showMessageDialog(null,"You got the wrong answer! \n The correct answer is: " + answer + " \n Questions Wrong: " + wrong, "Wrong Answer", JOptionPane.INFORMATION_MESSAGE);
    int y = JOptionPane.showOptionDialog(null,"CASH OUT with a total of $" + amount + "?","Cash Out?", JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MES SAGE,null,optionsA,optionsA[0]);
    if (y == JOptionPane.YES_OPTION) {
    JOptionPane.showMessageDialog(null,"Thanks for Playing!", "Thank You!", JOptionPane.INFORMATION_MESSAGE);
    System.exit(0);
    }
    if (y == JOptionPane.NO_OPTION) {}
    }
    else if (useranswer == answer)
    {
    JOptionPane.showMessageDialog(null,"Correct!", "Right Answer", JOptionPane.INFORMATION_MESSAGE);
    int y = JOptionPane.showOptionDialog(null,"CASH OUT with a total of $" + amount + "?","Cash Out?", JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MES SAGE,null,optionsA,optionsA[0]);
    if (y == JOptionPane.YES_OPTION) {
    JOptionPane.showMessageDialog(null,"Thanks for Playing!", "Thank You!", JOptionPane.INFORMATION_MESSAGE);
    System.exit(0);
    }
    if (y == JOptionPane.NO_OPTION) {}
    }
    }
    JOptionPane.showMessageDialog(null,"LEVEL ONE COMPLETE!", "LEVEL 1", JOptionPane.INFORMATION_MESSAGE);
    JOptionPane.showMessageDialog(null,"Cash on Hand: $ " + amount, "Cash", JOptionPane.INFORMATION_MESSAGE);
    int y = JOptionPane.showOptionDialog(null,"CASH OUT with a total of $" + amount + "?","Cash Out?", JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MES SAGE,null,optionsA,optionsA[0]);
    if (y == JOptionPane.YES_OPTION) {
    JOptionPane.showMessageDialog(null,"Thanks for Playing!", "Thank You!", JOptionPane.INFORMATION_MESSAGE);
    System.exit(0);
    }
    if (y == JOptionPane.NO_OPTION) {}

    //LEVEL TWO
    JOptionPane.showMessageDialog(null,"Welcome to Level: 2 " ," level 2", JOptionPane.INFORMATION_MESSAGE);
    int wrongB = 0;
    int runB = 1;
    int questionnumB = 0;


    while (runB <= 8 && wrongB != 3)
    {
    runB ++;
    questionnumB++;
    amount += 250;

    int increment = 250;

    int numOne = additionOne ();
    int numTwo = additionTwo ();
    int numThree = numThree ();
    int answer = numOne + numTwo - numThree;


    String useranswerA = JOptionPane.showInputDialog(null,"Question #" + questionnum + " is for: $" + increment + "\n" + numOne + " + " + numTwo + " - " + numThree + " = ?", "Question", JOptionPane.INFORMATION_MESSAGE);
    useranswerA = myInput.readLine ();
    int useranswer = Integer.parseInt(useranswerA);

    if (useranswer != answer)
    {
    wrong ++;
    JOptionPane.showMessageDialog(null,"You got the wrong answer! \n The correct answer is: " + answer + " \n Questions Wrong: " + wrongB, "Wrong Answer", JOptionPane.INFORMATION_MESSAGE);
    int z = JOptionPane.showOptionDialog(null,"CASH OUT with a total of $" + amount + "?","Cash Out?", JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MES SAGE,null,optionsA,optionsA[0]);
    if (z == JOptionPane.YES_OPTION) {
    JOptionPane.showMessageDialog(null,"Thanks for Playing!", "Thank You!", JOptionPane.INFORMATION_MESSAGE);
    System.exit(0);
    }
    if (z == JOptionPane.NO_OPTION) {}
    }
    else if (useranswer == answer)
    {
    JOptionPane.showMessageDialog(null,"Correct!", "Right Answer", JOptionPane.INFORMATION_MESSAGE);
    int z = JOptionPane.showOptionDialog(null,"CASH OUT with a total of $" + amount + "?","Cash Out?", JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MES SAGE,null,optionsA,optionsA[0]);
    if (z == JOptionPane.YES_OPTION) {
    JOptionPane.showMessageDialog(null,"Thanks for Playing!", "Thank You!", JOptionPane.INFORMATION_MESSAGE);
    System.exit(0);
    }
    if (z == JOptionPane.NO_OPTION) {}
    }
    }
    JOptionPane.showMessageDialog(null,"LEVEL TWO COMPLETE!", "LEVEL 2", JOptionPane.INFORMATION_MESSAGE);
    int z = JOptionPane.showOptionDialog(null,"CASH OUT with a total of $" + amount + "?","Cash Out?", JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MES SAGE,null,optionsA,optionsA[0]);
    if (z == JOptionPane.YES_OPTION) {
    JOptionPane.showMessageDialog(null,"Thanks for Playing!", "Thank You!", JOptionPane.INFORMATION_MESSAGE);
    System.exit(0);
    }
    if (y == JOptionPane.NO_OPTION) {}
    }
    }

    public static int additionOne ()
    {
    int x = (int)( Math.random () * 30);
    return x;
    }
    public static int additionTwo ()
    {
    int x = (int)( Math.random () * 30);
    return x;
    }
    public static int numThree ()
    {
    int x = (int) (Math.random () * 15);
    return x;
    }
    }


  2. #2
    Junior Member
    Join Date
    Mar 2013
    Posts
    26
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default Re: Java help with Methods (Begginer)

    Please surround your code with "code" and "/code" in square brackets so we can read it easier. Also, are you getting red text at the bottom of the screen? If so what is it?

  3. #3
    Junior Member
    Join Date
    Jun 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java help with Methods (Begginer)

    Quote Originally Posted by beansnbacon View Post
    Please surround your code with "code" and "/code" in square brackets so we can read it easier. Also, are you getting red text at the bottom of the screen? If so what is it?
    Hi! What do you mean red text?

  4. #4
    Junior Member
    Join Date
    Mar 2013
    Posts
    26
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default Re: Java help with Methods (Begginer)

    Sometimes you get an exception that appears in the console window as red text. It might say nullpointerexception or stackoverflowerror etc. If none shows up, what's your error?

  5. #5
    Member
    Join Date
    May 2011
    Location
    west palm beach, FL
    Posts
    189
    My Mood
    Tired
    Thanks
    41
    Thanked 11 Times in 10 Posts

    Default Re: Java help with Methods (Begginer)

    why dont you just make a method for each job and then call the methods from the main method? like

    main method here {
    method1();
    method2();
    method3();
    }
    method1() {
    some code
    }
    method2() {
    some code
    }
    method3() {
    some code
    }

Similar Threads

  1. HW BEGGINER HELP !
    By begginerJava in forum Object Oriented Programming
    Replies: 5
    Last Post: April 14th, 2014, 11:54 AM
  2. Java Help for a begginer
    By miller4103 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 15th, 2013, 09:00 PM
  3. Begginer Java - Bug in my code?
    By rk2010 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: March 7th, 2012, 08:12 AM
  4. help im a begginer
    By ivankov2 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 24th, 2011, 07:33 AM
  5. Any ideas for begginer?
    By SwEeTAcTioN in forum Java Theory & Questions
    Replies: 11
    Last Post: October 27th, 2009, 03:28 AM

Tags for this Thread