2016-09-26 180 views
2

如何在類Casino.java中存儲amount()存儲總值並允許將其返回到類Roulette.java更新不同類別中的變量

當我使用:

int amount = Casino.amount(); 

它給了我幾百行的錯誤。

我想要做的是運行遊戲number()和值存儲到Casino.amount()

請注意Roulette.java類有一個叫amountUpdate()功能,應該更新Casino.amount()

這是Casino.java

package Casino; 

import java.util.*; 

public class Casino { 

    static String player = ""; 
    static int playAmount = 0; 

    public static void main(String[] args) { 

     System.out.println("Welcome to Michael & Erics Casino!"); 
     player(); 
     ask(); 
     start(); 

    } 

    public static String player() { 
     if (player.equals("")) { 
      Scanner sc = new Scanner(System.in); 
      System.out.println("Please enter your name : "); 
      player = sc.nextLine(); 
     } 
     return player; 
    } 

    public static void ask() { 
     Scanner sc = new Scanner(System.in); 

     System.out.println("How much would you like to play with? "); 
     playAmount = sc.nextInt(); 
     if (playAmount < 1) { 
      System.out.println("Please enter a value that is more than $1"); 
      playAmount = 0; 
     } 
     System.out.println("You are playing with: $" + playAmount + "\n"); 

    } 

    public static int amount() { 
     int amount = playAmount; 
     if (Roulette.amountUpdate() >= 1) { 
      amount += Roulette.amountUpdate(); 
     } 
     return amount; 
     /*if (Blackjack.amountUpdate() >= 1) 
      amount += Blackjack.amountUpdate(); 
      return amount;*/ 
    } 

    public static void start() { 
     System.out.println("Which table would you like to play at?"); 
     System.out.println("1: Blackjack"); 
     System.out.println("2: Roulette"); 
     System.out.println("3: Check Balance"); 
     System.out.println("4: Exit"); 
     Scanner sc = new Scanner(System.in); 
     int table = sc.nextInt(); 

     switch (table) { 
      case 1: 
       //blackjack.main(new String[]{}); 
       break; 
      case 2: 
       Roulette.main(new String[]{}); 
       break; 
      case 3: 
       System.out.println("You have : $" + playAmount); 
       start(); 
       break; 
      case 4: 
       System.exit(0); 
       break; 
     } 
    } 

} 

這是類Roulette.java

package Casino; 

import java.util.*; 
import java.lang.*; 

public class Roulette { 

    public static void main(String[] args) { 
     System.out.println("Welcome to the roulette table " + Casino.player()); 
     placeBet(); 
     amountUpdate(); 
     //number(); 
    } 

    public static int amountUpdate() { 
     int amount = 0; 
     if (number() > 0) { 
      amount += number(); 
     } 
     if (br() > 0) { 
      amount += br(); 
     } 
     if (oe() > 0) { 
      amount += oe(); 
     } 
     if (third() > 0) { 
      amount += third(); 
     } 
     return amount; 
    } 

    public static void placeBet() { 
     Scanner sc_Choice = new Scanner(System.in); 

     System.out.println("What would you like to bet on?"); 
     System.out.println("1: Numbers"); 
     System.out.println("2: Black or Red"); 
     System.out.println("3: Odd or Even"); 
     System.out.println("4: One Third"); 
     System.out.println("5: Count Chips"); 
     System.out.println("6: Restart"); 

     int choice = sc_Choice.nextInt(); 

     if (choice > 0 && choice < 7) { 
      switch (choice) { 
       case 1: 
        number(); 
        break; 
       case 2: 
        br(); 
        break; 
       case 3: 
        oe(); 
        break; 
       case 4: 
        third(); 
        break; 
       case 5: 
        System.out.println(Casino.amount()); 
        break; 
       case 6: 
        Casino.main(new String[]{}); 
        break; 
      } 
     } else { 
      System.out.println("You must choose between 1 and 6"); 
     } 
    } 

    public static int number() { 
     Boolean betting = true; 
     //int amount = Casino.amount(); 
     int amount = 5000; 
     int number; 
     int winnings; 
     String reply; 
     int betX; 
     int betAgain = 1; 
     Scanner sc_Number = new Scanner(System.in); 

     ArrayList<Integer> list = new ArrayList<Integer>(); 
     ArrayList<Integer> bet = new ArrayList<Integer>(); 

     while (betAgain == 1) { 
      System.out.println("What number would you like to place a bet on?"); 
      int listCheck = sc_Number.nextInt(); 

      if (listCheck >= 0 && listCheck <= 36) { 
       list.add(listCheck); 
      } else { 
       System.out.println("You must choose a number between 0 and 36"); 
      } 

      System.out.println("How much do you want to bet?"); 
      betX = sc_Number.nextInt(); 

      if (betX > amount) { 
       System.out.println("You have insufficient funds to make that bet"); 
       number(); 
      } else if (betX < 1) { 
       System.out.println("You must bet more than 1$"); 
      } else { 
       bet.add(betX); 
       amount = amount - betX; 
      } 

      System.out.println("Do you want to bet on more numbers?"); 
      reply = sc_Number.next(); 

      if (reply.matches("no|No|false|nope")) { 
       betAgain = betAgain - 1; 
       //No - Don't bet again 
      } else { 
       betAgain = 1; 
       //Yes - Bet again 
      } 

      int result = wheel(); 
      System.out.println("Spinning! .... The number is: " + result); 

      for (int i = 0; i < bet.size(); i++) { 
       if (list.get(i) == result) { 
        winnings = bet.get(i) * 35; 

        System.out.println("Congratulations!! You won: $" + winnings); 
        amount = amount + winnings; 
        System.out.println("Current Balance: " + amount); 
        betAgain = betAgain - 1; 

       } else { 
        System.out.println("Sorry, better luck next time!"); 
        System.out.println("Current Balance: " + amount); 
        betAgain = betAgain - 1; 
       } 
      } 
      betAgain = betAgain - 1; 
     } 
     return amount; 
    } 

    public static int wheel() { 
     Random rnd = new Random(); 

     int number = rnd.nextInt(37); 
     return number; 
    } 

    //NOT WORKING - AFTER MAKING A BET IT RUNS Number() 
    public static int br() { 
     Scanner sc_br = new Scanner(System.in); 
     int amount = Casino.amount(); 
     int winnings; 
     System.out.println("Would you like to bet on Black or Red?"); 
     String replyBR = sc_br.nextLine(); 
     boolean black; 
     //**************** 
     if (replyBR.matches("black|Black")) { 
      black = true; 
     } else { 
      black = false; 
     } 

     System.out.println("How much would you like to bet?"); 
     int betBR = sc_br.nextInt(); 
     if (betBR > amount) { 
      System.out.println("You have insufficient funds to make that bet"); 
      br(); 
     } else if (betBR < 1) { 
      System.out.println("You must bet more than 1$"); 
      br(); 
     } else { 
      amount = amount - betBR; 
     } 
     //***************** 
     boolean resultColour = colour(); 
     if (resultColour == black) { 
      winnings = betBR * 2; 
      //PRINT OUT WHAT COLOUR!!! 
      System.out.println("Congratulations!! You won: $" + winnings); 
      amount = amount + winnings; 
      System.out.println("Current Balance: " + amount); 

     } else { 
      System.out.println("Sorry, better luck next time!"); 
     } 
     System.out.println("Current Balance: " + amount); 
     return amount; 
    } 

    public static boolean colour() { 
     Random rnd = new Random(); 

     boolean colour = rnd.nextBoolean(); 
     return colour; 
    } 
    //NOT WORKING - AFTER MAKING A BET IT RUNS Number() 

    public static int oe() { 
     Scanner sc_oe = new Scanner(System.in); 
     int amount = Casino.amount(); 
     int winnings; 
     System.out.println("Would you like to bet on Odd or Even?"); 
     String replyOE = sc_oe.next(); 
     System.out.println("How much would you like to bet?"); 
     int betOE = sc_oe.nextInt(); 
     if (betOE > amount) { 
      System.out.println("You have insufficient funds to make that bet"); 
      oe(); 
     } 
     amount = amount - betOE; 

     boolean resultOE = oddOrEven(); 
     //PRINT OUT IF IT WAS ODD OR EVEN 
     if (resultOE == true) { 
      winnings = betOE * 2; 
      System.out.println("Congratulations!! You won: $" + winnings); 
      amount = amount + winnings; 
      System.out.println("Current Balance: " + amount); 

     } else { 
      System.out.println("Sorry, better luck next time!"); 
      System.out.println("Current Balance: " + amount); 
     } 
     return amount; 
    } 

    public static boolean oddOrEven() { 
     Random rnd = new Random(); 

     boolean num = rnd.nextBoolean(); 
     return num; 
    } 
    //NOT WORKING - AFTER MAKING A BET IT RUNS Number() 

    public static int third() { 
     Scanner sc_Third = new Scanner(System.in); 
     int amount = Casino.amount(); 
     int winnings; 
     System.out.println("Would you like to bet on 1st, 2nd or 3rd third?"); 
     String replyT = sc_Third.next(); 
     System.out.println("How much would you like to bet?"); 
     int betT = sc_Third.nextInt(); 
     if (betT > amount) { 
      System.out.println("You have insufficient funds to make that bet"); 
      third(); 
     } 
     amount = amount - betT; 

     boolean resultT = thirdResult(); 
     //PRINT OUT WHAT NUMBER IT WAS AND IF IT WAS IN WHICH THIRD 
     if (resultT == true) { 
      winnings = betT * 3; 
      System.out.println("Congratulations!! You won: $" + winnings); 
      amount = amount + winnings; 
      System.out.println("Current Balance: " + amount); 

     } else { 
      System.out.println("Sorry, better luck next time!"); 
      System.out.println("Current Balance: " + amount); 
     } 
     return amount; 
    } 

    public static boolean thirdResult() { 
     Random rnd = new Random(); 

     int num = rnd.nextInt(2); 
     if (num == 0) { 
      return true; 
     } else { 
      return false; 
     } 
    } 

} 
+0

爲什麼你所有的方法都是靜態的?! – Li357

+0

我是第一學期的編程學生,我不知道甚麼是靜態的。 –

+0

@AndrewL。沒有實例或非靜態方法。你的建議只會讓他感到困惑,除非你想解釋如何使用對象。 – shmosel

回答

1

看起來你很可能運行到一個StackOverflowException。當您在Roulette.number()內呼叫Casino.amount()時,它會呼叫Roulette.amountUpdate(),然後呼叫Roulette.number()。你的方法像這樣陷入了一個無限循環。您需要重新設計您的代碼,以使這3個函數不完全相互依賴。

你的代碼很簡潔,所以很難幫你完全解決問題,但我相信你會從將你的「金額」變量分解成單獨的實體中受益。保持投注金額,獎金等單獨的東西,直到你需要將它們合併爲止。

您可能遇到的另一個問題是在Casino.amount()中被調用兩次,但Roulette.amountUpdate()不一定會兩次返回相同的東西。考慮存儲第一次調用的返回值,而不是調用它兩次。