0

我的程序是一个数学游戏,会在不同级别向用户提出不同的问题。我想使用一种金钱奖励系统,只要他们答对一个问题,他们的奖金就会增加 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;
  }

} 
4

5 回答 5

1

不要用局部变量做数学运算。而是声明money为实例变量:

public class mathGame
{
    private static int money = 0;

    (...)

从代码中删除int money = 0;除上述之外的所有其他内容。

于 2013-06-15T18:20:59.157 回答
0

您必须将货币变量声明为类变量。如果你每次调用一个方法时看到,你创建一个新的变量money并将它设置为0。

public class mathGame
{
  static int money = 0;

在你的方法中删除所有钱(int money = 0;)的初始化,它会起作用。

于 2013-06-15T18:20:47.423 回答
0

您使用的money变量在每个方法中都是本地的(这意味着方法一退出它们就消失了)。您需要摆脱所有这些货币变量并声明一个实例变量:

public class mathGame
{
    /* Class variable, declared outside of any method */
    static protected int money = 0;
    ...
    /* Methods declaration here */
    ...
}

有关Java 变量的更多信息。


这可能是一个更好的主意,不要让所有的方法和变量都是静态的,而是使用类的实例。

实例变量和类变量之间的区别。

于 2013-06-15T18:22:47.500 回答
0
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import javax.swing.JOptionPane;

public class mathGame
{
      private static int money = 0;
  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 




    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 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 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 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;
  }

} 

您需要创建一个实例变量。同样在每种方法中,您都设置了money = 0,因此每次调用该方法时它都变为0。发布的代码有效。

于 2013-06-15T18:29:42.547 回答
0

每次调用方法时都会创建一个新的本地货币变量,因此每次调用它总是从 0 开始。

int money = 0;

您需要在开头删除此语句additionsubtraction并且addsub

于 2013-06-15T18:37:33.630 回答