2013-12-13 4 views
-2

변수 walletMoney를 사용자가이기거나 끊어짐에 따라 지속적으로 업데이트하려고합니다. Java에 익숙하지 않으며 약간 혼란 스럽습니다. 나는 해결책과 설명에 정말로 감사 할 것이다. 고맙습니다. slotMachine() 내부다음 코드에서 변수를 업데이트하는 방법

/* 
    * 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."); 
       } 

     } 

} 

답변

2

변수를 클래스에 넣으면됩니다. 이렇게하면 어디에서나 액세스 할 수 있으며 그 값은 rollSlots() 메서드 호출 사이에 유지됩니다.

public class SlotMachine extends ConsoleProgram { 

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

    //... 

} 
+1

완벽한, 감사합니다. – TheZac2613

1

선언은 단지 그 방법에 로컬 변수한다. 필드 선언으로 이동하십시오.

이유는 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 
} 
0

변수를 클래스 변수로 설정할 수 있습니다. 바로 아래에 설정하십시오.

public int CHERRY = 2;

도움이되지 않는 경우 변경하거나 프로그램을 종료 할 때마다 파일에 기록하고 프로그램 시작시 읽을 수 있습니다.

관련 문제