2016-09-26 105 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()不一定会两次返回相同的东西。考虑存储第一次调用的返回值,而不是调用它两次。