我们应该在我们的程序中使用方法,但我无法将它们合并到其中。我的程序就像一个数学游戏,询问简单的加法和加法/减法问题。我希望所有计算都在方法中,但我无法返回整个方程。
当涉及到我需要去的方向时,我需要一些帮助,以便在我的程序中有效地使用方法,以及我应该在哪里进行计算!以及它自己的方法中应该有什么以及主要方法中应该有什么。
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_MESSAGE,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 = 0;
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);
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_MESSAGE,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_MESSAGE,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);
//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);
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_MESSAGE,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_MESSAGE,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);
JOptionPane.showMessageDialog(null,"Cash on Hand: $ " + amount, "Cash",JOptionPane.QUESTION_MESSAGE);
}
}
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;
}
}