Adding a counter to a java program with different methods

故事扮演 提交于 2019-12-11 18:59:52

问题


My program is a math game and will ask the user different questions in different levels. I would like to use a money rewards system that whenever they get one question right, $1000 will be added to their prize. I have tried to do this, however the money doesn't add when an answer is answered correctly. Help please on how I can go about fixing this.

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

} 

回答1:


You have to declare you money variable as a class variable. If you see each time you call a method, you create a new variable money and set it to 0.

public class mathGame
{
  static int money = 0;

Remove all the initializations of money (int money = 0;) in your method and it will works.




回答2:


Don't do the math with local variables. Instead declare money as a instance variable:

public class mathGame
{
    private static int money = 0;

    (...)

Remove all the other int money = 0; from the code except the one above.




回答3:


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

} 

You need to make an instance variable. Also in each method you set the money = 0 so each time you called the method it turned to 0. Posted code works.




回答4:


You are creating a new local money variable everytime you call a method so it will always start at 0 each call.

int money = 0;

You need to remove this statement at the start of addition, subtractionand addsub




回答5:


The money variables you are using are local within each method (which means they are gone as soon as the methods exit). You need to get rid of all those money variables and declare one instance variable:

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

More info on Java variables.


It is probably a better idea, to not have all your methods and variables static and work with an Instance of the class instead.

Difference between Instance and Class variables.



来源:https://stackoverflow.com/questions/17126541/adding-a-counter-to-a-java-program-with-different-methods

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!