如何更新以下代码中的变量

时间:2013-12-13 05:13:41

标签: java variables

我试图让变量walletMoney在用户获胜或失败时不断更新。我是java新手,有点困惑。我真的很感激解决方案和解释。谢谢。

    /*
    * This program will simulate playing a slot machine.
    * It will provide instructions to the user with an initial stake of $50 and then let the user play until either the money runs out or the player quits.
    * Author: Zac Saunders
    * Version: 1.0
    */


import acm.program.*;
import acm.util.RandomGenerator;

public class SlotMachine extends ConsoleProgram {

        RandomGenerator rgen = new RandomGenerator();

        public int BAR_BAR_BAR = 250;
        //The value that the user wins when he rolls a bar/bar/bar.

        public int BELL_BELL_BELL = 20;
        //The value that the user wins when he rolls a bell/bell/bell.
        // BELL_BELL_BELL is the same as BELL_BELL_BAR.

        public int PLUM_PLUM_PLUM = 14;
        //The value that the user wins when he rolls a plum/plum/plum.
        // PLUM_PLUM_PLUM is the same as PLUM_PLUM_BAR.

        public int ORANGE_ORANGE_ORANGE = 10;
        //The value that the user wins when he rolls an orange/orange/orange.
        // ORANGE_ORANGE_ORANGE is the same as ORANGE_ORANGE_BAR;

        public int CHERRY_CHERRY_CHERRY = 7;
        //The value that the user wins when he rolls a cherry/cherry/cherry.

        public int CHERRY_CHERRY = 5;
        //The value that the user wins when he rolls a cherry/cherry/-.

        public int CHERRY = 2;
        //The value that the user wins when he rolls a cherry/-/-.


        public void run()
        {

                addInitialMenu();
                String instructions = readLine("Enter a Y/N here: ");

                if(instructions.equals("N")){
                        addInstructions();

                }else{
                        rollSlots();
                }

        }
        public void addInitialMenu()
        {
                println("Do you know how to play?");
                println("(Type yes or no)");
        }
        public void addInstructions(){
                println("Instructions: ");

                println(" Click to roll the slot machine. Each roll costs $1 from your wallet.");

                println("Your wallet begins with $50 and it will change depending on your wins and losses.");

                println("Are you ready to play?");

                String instructions = readLine("Enter a Y/N here: ");

                if(instructions.equals("N")){

                        addInstructions();

                }else{
                        rollSlots();
                }
        }

        public void rollSlots(){

                int spins = rgen.nextInt(1, 500);


                int walletMoney = 50;//Sets the initial wallet amount.

                if(walletMoney > 1){//Checks to see if you have any money in your wallet.

                        if(spins < 20 ){//CHERRY/-/-

                                println("You rolled: CHERRY   -   -");
                                println("Congrats, you won $2 ");

                                walletMoney = walletMoney + CHERRY;

                        }else if(spins > 20 && spins < 50 ){//CHERRY/CHERRY/-

                                println("CHERRY   CHERRY   -");
                                println("Congrats, you won $5 ");

                                walletMoney = walletMoney +     CHERRY_CHERRY;

                        }else if(spins > 50 && spins < 75 ){//CHERRY/CHERRY/CHERRY

                                println("CHERRY   CHERRY   CHERRY");
                                println("Congrats, you won $7 ");

                                walletMoney = walletMoney + CHERRY_CHERRY_CHERRY;

                        }else if(spins > 75 && spins < 110){//ORANGE/ORANGE/ORANGE

                                println("ORANGE   ORANGE   ORANGE");
                                println("Congrats, you won $10 ");

                                walletMoney = walletMoney + ORANGE_ORANGE_ORANGE;

                        }else if(spins > 110 && spins < 125){//PLUM/PLUM/PLUM

                                println("PLUM   PLUM   PLUM");
                                println("Congrats, you won $14 ");

                                walletMoney = walletMoney + PLUM_PLUM_PLUM;

                        }else if(spins > 125 && spins < 170){//BELL/BELL/BELL

                            println("BELL   BELL   BELL");
                            println("Congrats, you won $20 ");

                                walletMoney = walletMoney + BELL_BELL_BELL;

                        }else if(spins > 170 && spins < 200){//BAR/BAR/BAR

                            println("BAR   BAR   BAR");
                            println("Congrats, you won $250 ");

                                walletMoney = walletMoney + BAR_BAR_BAR;

                        }else if(spins > 200 && spins < 250){//-/-/-

                         println("-   -   -");
                         println("You didn't win. Better luck next time.");

                        }else if(spins > 250 && spins < 300){//ORANGE/CHERRY/CHERRY

                         println("ORANGE   CHERRY   CHERRY");
                         println("You didn't win. Better luck next time.");

                        }else if(spins > 300 && spins < 350){//BELL/PLUM/BELL

                         println("BELL   PLUM   BELL");
                         println("You didn't win. Better luck next time.");

                        }else{//CHERRY/CHERRY/BAR

                         println("CHERRY   CHERRY   BAR");
                         println("You didn't win. Better luck next time.");

                        }

                        walletMoney--;//Subtracts the fee from the users wallet.

                        println("Your wallet has: $" + walletMoney );
                        println("Do you want to roll again?");
                        String instructions = readLine("Enter a Y/N here: ");

                        if(instructions.equals("N")){

                                run();

                        }else{
                                rollSlots();
                        }


                }else{
                        println("You have run out of money. How unfortunate.");
                }

        }

}

3 个答案:

答案 0 :(得分:2)

你应该把变量放在类上。通过这种方式,它可以在任何地方访问,并且它的值将保留在rollSlots()方法的调用之间。

public class SlotMachine extends ConsoleProgram {

    int walletMoney = 50;//Sets the initial wallet amount.

   //...

}

答案 1 :(得分:1)

slotMachine()内的声明使其成为仅适用于该方法的局部变量。将其移至字段声明。

原因是variable scope。由于变量只在该方法中出现过,所以应用程序的其余部分并不知道;因此,无论何时调用,您都将每次创建walletMoney的新实例。

例如,您可以将其直接放在CHERRY下面。

public class SlotMachine extends ConsoleProgram {

    // implementation above

    public int CHERRY = 2;
    //The value that the user wins when he rolls a cherry/-/-.

    int walletMoney = 50;

    // implementation to follow
}

答案 2 :(得分:0)

您可以将变量设置为类变量。将其设置在

之下

public int CHERRY = 2;

如果这没有帮助,您可以在每次更改或程序终止后将其写入文件,并在程序启动时读取它。