使用不同的方法将计数器添加到java程序

时间:2013-06-15 18:15:18

标签: java methods counter

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

} 

5 个答案:

答案 0 :(得分:1)

不要使用局部变量进行数学计算。而是将money声明为实例变量:

public class mathGame
{
    private static int money = 0;

    (...)

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

答案 1 :(得分:0)

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

public class mathGame
{
  static int money = 0;

删除方法中的所有资金初始化(int money = 0;),它将有效。

答案 2 :(得分:0)

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

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

Java variables 的更多信息。


可能更好的想法是,不要将所有方法和变量设置为静态,而是使用类的实例。

Instance and Class variables 之间的区别。

答案 3 :(得分: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.已发布的代码可以使用。

答案 4 :(得分:0)

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

int money = 0;

您需要在additionsubtractionaddsub

开头删除此声明