使用不同的方法向Java程序添加计数器 [英] Adding a counter to a java program with different methods

查看:108
本文介绍了使用不同的方法向Java程序添加计数器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序是一个数学游戏,会在不同级别询问用户不同的问题.我想使用金钱奖励系统,每当他们答对一个问题时,他们的奖金就会增加1000美元.我已经尝试过这样做,但是正确回答一个问题,钱却没有增加.请帮我解决这个问题.

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

} 

推荐答案

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

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;

删除方法中的所有货币初始化(int money = 0;),它将起作用.

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

这篇关于使用不同的方法向Java程序添加计数器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆