• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java help with Methods (Begginer)  RSS feed

 
Maddie Cal
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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;
}
}
 
Ashish Dutt
Ranch Hand
Posts: 172
Java MySQL Database Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello there,
Welcome to Java Ranch
Please use code tags to post your code, it increases readability
I have done it for you now

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ashish Dutt wrote:Hello there,
Welcome to Java Ranch
Please use code tags to post your code, it increases readability
I have done it for you now


One of the reason that code tags greatly increases readability is the formatting. The OP's code doesn't seem to be formatted.

Add to that with the question being somewhat vague and the code doesn't isolating the issue -- but that's something else that makes it hard to for this topic to see answers.

Henry
 
Ashish Dutt
Ranch Hand
Posts: 172
Java MySQL Database Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
on a side note this program does not compile
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!