试图找到重新访问代码的最佳方法

时间:2018-04-24 22:17:05

标签: java arrays gambling

我创建了一款包含2款游戏的赌博应用程序。但是我希望能够从一个游戏切换到另一个游戏。我试过(游戏== 1),但似乎一旦我匹配条件,它退出循环并尝试再次输入将不会切换到第二个游戏。然后我尝试了,但即使我将输入设置为" 2"它仍然开始游戏#1。关于我应该做什么的任何建议?

import java.util.Scanner;
import java.util.Random;
public class Project2 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
    int min = 1;
    int max = 10;
    int colmax = 2;
    double balance = 2500;
    double bet1 = 0;
    double bet2 = 0;
    String kBet = null;

    //Call method gameChoice to allow player to choose what game they want to play.
    gameChoice();
    int game = input.nextInt();
 do { 
    System.out.println("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-BLACK JACK-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-");
    System.out.print("Please choose Black or Red, and a number from 1-10..(Example: Red 4): ");
    String color = input.next();
    int number = input.nextInt();
    //Seperate bets for color and number. Bet1 = color bet, bet2 = number bet
    System.out.print("Your available balance is $"+balance+". How much would you like to bet on "+color+"?");
        bet1 = input.nextInt();
        if(bet1 > balance) {
           System.out.print("You dont have enough money to bet $"+bet1+". Please enter a valid bet: $");
           bet1 = input.nextInt();
        }
        else 
        balance -= bet1;
        double profit1 = (bet1 * 2) - bet1;
    //Bet 2 for number.
    System.out.print("Your available balance is $"+balance+". How much would you like to bet on "+number+"?");
        bet2 = input.nextInt();
        if(bet2 > balance) {
           System.out.print("You dont have enough money to bet $"+bet2+". Please enter a valid bet: $");
           bet2 = input.nextInt();
        }
        else 
        balance -= bet2;
        double profit2 = (bet2 * 5) - bet2;
    //Give bet info
    System.out.println("------------------------------BET INFO------------------------------------");
    System.out.println("You just bet $"+bet1+" on "+color+" and $"+bet2+" on number "+number);
    System.out.println("Spinning............");
    System.out.println("------------------------------RESULTS-------------------------------------");

    //Generate random number, Generate random color. 
    Random rouletteNum = new Random();
        int rNum = min + rouletteNum.nextInt(max);
        int rCol = min + rouletteNum.nextInt(colmax);

    //Only generate 2 numbers between 1-2; 1 is black, 2 is red.
    if (rCol == 1) {
        System.out.println("The machine landed on Black "+rNum);
    }
    else if(rCol != 1) {
        System.out.println("The machine landed on Red "+rNum);
    }
    //All possible conditions for betting outcomes.
    if(rNum == number) {
        System.out.println("Congrats, you guessed the right number! You've won $"+profit2);
            balance += (bet2 * 5);
    }
    else if(rNum != number) {
        System.out.println("Sorry!You didnt guess the right number! You've lost $"+bet2);
    }
    if(rCol == 1 && color.equals("Black")) {
        System.out.println("Congrats, you guessed the right color! You've won $"+profit1);
            balance += bet1 * 2 - bet1;
    }
    else if(rCol == 2 && color.equals("Red")) {
        System.out.println("Congrats, you guessed the right color! You've won $"+profit1);
            balance += bet1 * 2 - bet1;
    }       
    if(rCol == 2 && color.equals("Black")) {
        System.out.println("Sorry, you didn't guess the right color. You've lost $"+bet1);
    }
    else if(rCol == 1 && color.equals("Red")) {
        System.out.println("Sorry, you didn't guess the right color. You've lost $"+bet1);
    }
    System.out.println("------------------------------------------------------------------------");
    //Call isBroke method to check if player is bankrupt.
    if(isBroke(balance) == true) {
        endGame(balance);
    }
    else {
    //If player isn't bankrupt, ask if they want to place another bet.
    System.out.println("New balance: $"+balance);
        gameChoice2();
        game = input.nextInt();
    }
 }
 while(game == 1);
 {
    do {
         int bet = 0;
         double start = 1.00;
         double crashValue = 1.00;
         int stopGame = 1;
         double cashout = 0;
         System.out.println("-------------------CRASH GAME--------------------------");
         System.out.println("Welcome to Crash!");
         System.out.print("What number would you like to cashout at?(Ex. 1.15):");
            cashout = input.nextDouble();
         System.out.print("Your balance is $"+balance+". How much would you like to bet on this round?:");
            bet = input.nextInt();
            //check if bet amount is greater then the balance.
            if(bet > balance) {
               System.out.print("You dont have enough money to bet $"+bet+". Please enter a valid bet: $");
               bet = input.nextInt();
            }
            else
         System.out.println("--------------------------------------------------------------------------");
         System.out.println("Round is beginning........."); 

         for(int i =0; i < stopGame; i++) {
      //Do while to keep the numbers generating until i == 1 (until crash)
    do {
        //Generate random number from 1-100, if the number is less than 98, print the digit (Example : 1.34)
         int crash =(int)(Math.random() * 100);
         if (crash < 98) {
             start += .01;
             System.out.printf("%.2f\n",start);
         }
         //if random number from 1-100 is greater than 98, crash the game.
         else if(crash > 98) {
                 i++;
                 crashValue = start;
                 System.out.println("----------------------------RESULTS--------------------------------");
                 System.out.print("CRASH! The game crashed at "); 
                 System.out.printf("%.2f",start);
                 System.out.println("x"); 
         }
    } 
    while(i == 0);
   }
    //Check if player cashed out before game crashed.
    if(cashout < crashValue) {
        System.out.println("Congrats! You cashed out at "+cashout+" before the game crashed. You've won $"+bet*cashout);
        balance += bet * cashout;
    }
    //Player didn't cash out in time, and lost.
    else {
        System.out.println("Sorry! The game crashed before you could cash out. You've lost $"+bet);
        balance -= bet;
    }
    System.out.println("------------------------------------------------------------------------");
    //check if player is bankrupt.
    if(isBroke(balance) == true) {
        endGame(balance);
    }
    else {
    //If they arent bankrupt, ask if they want another bet.
    System.out.println("New balance: $"+balance);
    gameChoice2();
    game = input.nextInt();
     }
}  
   while(game == 2);
}

1 个答案:

答案 0 :(得分:0)

您使用了do...while循环。在评估while条件之前,这种类型的循环总是至少执行一次。