我的程序是一个数学游戏,会在不同级别向用户提出不同的问题。我想使用一种金钱奖励系统,只要他们答对一个问题,他们的奖金就会增加 1000 美元。我试图这样做,但是当答案正确回答时,钱并没有增加。请帮助我如何解决这个问题。
import javax.swing.*;
import java.io.*;
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, "...Level: 1...");
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 6 questions correctly. \n For every correct question, you will earn $1,000.");
for (int y = 0; y <= 3; y++){
addition();
subtraction();
}
JOptionPane.showMessageDialog(null, "...Level: 2...");
JOptionPane.showMessageDialog(null, "Your second level consists of addition and substraction of 3 numbers! \n In order to pass this level, you will need to answer all 6 questions correctly. \n For every correct question, you will earn $1,000.");
for (int z = 0; z <= 6; z++){
addSub();
}
}
else if (x == JOptionPane.NO_OPTION){
JOptionPane.showMessageDialog(null, "Goodbye!");
System.exit(0);
}
}
public static int addition()
{
int money = 0;
int firstNum = (int)(Math.random()*20);
int secondNum = (int)(Math.random()*20);
String sumA = JOptionPane.showInputDialog(null, firstNum + "+" + secondNum + " = ", "Question", JOptionPane.INFORMATION_MESSAGE);
int sum = Integer.parseInt (sumA);
int realSum = firstNum + secondNum;
if (sum == realSum){
money = money + 1000;
JOptionPane.showMessageDialog(null, "CORRECT! \n You have $" + money);
}
else 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);
}
return sum;
}
public static int subtraction ()
{
int money =0;
int firstNum = (int)(Math.random()*20);
int secondNum = (int)(Math.random()*20);
if (firstNum < secondNum){
firstNum = secondNum;
secondNum = firstNum;
}
String differenceA = JOptionPane.showInputDialog(null, firstNum + " - " + secondNum + " = ", "Question", JOptionPane.INFORMATION_MESSAGE);
int difference = Integer.parseInt (differenceA);
int realDifference = firstNum - secondNum;
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);
}
return difference;
}
public static int addSub ()
{
int money = 0;
int signNum = (int)(Math.random()*1);
int firstNum = (int)(Math.random()*20);
int secondNum = (int)(Math.random()*20);
int thirdNum = (int)(Math.random()*10);
String answerA = JOptionPane.showInputDialog(null, firstNum + " + " + secondNum + " - " + thirdNum + " = ", "Question", JOptionPane.INFORMATION_MESSAGE);
int answer = Integer.parseInt (answerA);
int realAnswer = firstNum + secondNum - thirdNum;
if (answer == realAnswer){
money = money + 1000;
JOptionPane.showMessageDialog(null, "CORRECT! \n You have $" + money);
}
else if (answer != realAnswer){
JOptionPane.showMessageDialog(null, "INCORRECT! \n You are not smarter than a 12 year old. \n You go home with $" + money);
System.exit(0);
}
return answer;
}
}