Java 21点赌博和交易没有按照我的预期进行

Java 21点赌博和交易没有按照我的预期进行,java,if-statement,while-loop,boolean-logic,blackjack,Java,If Statement,While Loop,Boolean Logic,Blackjack,因此,我一直在做一个21点计划,由于某种原因,我无法让“另一张卡y/n”正常工作,而且投注系统似乎也不起作用。另外,我也不太确定布尔方法-idk在main中的何处调用它。任何提示都将不胜感激 我已经试着换掉whiles/if,结果一切都变得一团糟。我也尝试过使方法无效,但我需要它返回userWins TRUE或FALSE。。因为这是一场游戏。。你知道吗 import java.io.*; 导入java.util.Scanner; 导入java.util.Random; 公共课21点{ 公共静态v

因此,我一直在做一个21点计划,由于某种原因,我无法让“另一张卡y/n”正常工作,而且投注系统似乎也不起作用。另外,我也不太确定布尔方法-idk在main中的何处调用它。任何提示都将不胜感激

我已经试着换掉whiles/if,结果一切都变得一团糟。我也尝试过使方法无效,但我需要它返回userWins TRUE或FALSE。。因为这是一场游戏。。你知道吗

import java.io.*;
导入java.util.Scanner;
导入java.util.Random;
公共课21点{
公共静态void main(字符串[]args)引发IOException{
扫描仪输入;输入=新扫描仪(系统输入);
int money;//用户拥有的金额。
int bet;//用户在游戏上下注的金额。
布尔userWins=true;//用户赢了游戏吗?
System.out.println(“欢迎来到Lunas BlackJack table,进展如何?”);
System.out.println();
money=100;//用户从$100开始。
while(true){
System.out.println(“你有“+钱+美元”);
做{
System.out.println(“您想下注多少?或者如果下注完毕,请输入0以离开表格”);
系统输出打印($);
bet=in.nextInt();
如果(下注<0 | |下注>金钱){
System.out.println(“您的赌注必须介于0和“+money+”);
}
}
而(下注<0 | |下注>金钱){
如果(下注==0){
System.out.println(“经销商挥手告别”);
打破;//走开
}
userWins=playballjack(userWins);
如果(userWins==true){
金钱=金钱+赌注;
}
如果(userWins==false){
金钱=金钱-赌注;
System.out.println();
}
如果(货币==0){
System.out.println(“噢,糟了,看来你没钱了!”);
打破
}
}
}
System.out.println();
System.out.println(“你带着$”+money+“.”离开);
}//结束方法
公共静态布尔playBlackjack(布尔用户赢){
串另一张卡片;
字符串play=“y”;
字符串ctn;
int nextCard;
int card1;
int card2;
int dCard1;
int dCard2;
int cardTotal=0;
int dTotal=0;
扫描仪输入=新扫描仪(系统输入);
随机=新随机();
//开始给玩家发前两张牌
if(playreach.equals(“y”)){
//前两张随机牌
dCard1=random.nextInt(11)+1;
dCard2=random.nextInt(11)+1;
//玩家前两张随机牌和总牌数
card1=random.nextInt(11)+1;
card2=random.nextInt(11)+1;
cardTotal=card1+card2;
//经销商共有两张卡,仅显示一张经销商卡
dTotal=dCard1+dCard2;
System.out.println(“经销商显示:+dCard1”);
//显示玩家前两张牌和牌总数
System.out.println(“第一张卡:“+card1+”,“+card2”);
系统输出打印项次(“总计:+cardTotal”);
//询问是否要处理另一张卡
系统输出打印(“另一张卡片(y/n)?:”;
另一张卡=in.nextLine();
//如果有的话
而(另一张卡==“y”){//while1
nextCard=random.nextInt(10)+1;
cardTotal+=nextCard;
System.out.println(“卡:+nextCard”);
系统输出打印项次(“总计:+cardTotal”);
如果(cardTotal>21){//if1
System.out.println(“你失败了,庄家赢了”);
System.out.println(“您想再次播放吗?(y/n):”;
再次播放=in.nextLine();
}//如果1
如果(cardTotal<21){//if2
系统输出打印(“另一张卡片(y/n)?:”;
另一张卡=in.nextLine();
}//如果2
如果(另一张卡==“n”){//if3
系统输出打印(“按c继续经销商卡”);
ctn=in.nextLine();
而(ctn==“c”&&dTotal<17){//while2
nextCard=random.nextInt(10)+1;
dTotal+=nextCard;
}//while2
如果(数据总量>21){//if4
System.out.println(“经销商破产,你赢了!”);
System.out.println(“再次播放”(y/n):”;
再次播放=in.nextLine();
}//如果4
if(playreach.equalsIgnoreCase(“y”)//ignorecase=忽略大写/小写
{//if5
play=“y”;
}//如果5
if(playreach.equalsIgnoreCase(“n”)//否!
{//else1
系统出口(0);
}//else1
}//如果3
}//while1
}//到此为止
返回userWins=true;
}//结束二十一点游戏()
}//末端总管

用户似乎也不会输给经销商,我预计会发生一些损失。我也希望“另一张卡”中的(y/n)能起作用,但“n”什么也不起作用。

我认为您唯一的问题是您试图返回userWin布尔值的方式:

 public static boolean playBlackjack(boolean userWins)
将其更改为(并在调用时删除输入参数)

当用户丢失时返回false,否则返回true

e、 g:

完整代码:

public class ProgramBlackJack
{

  public static void main(String[] args)throws IOException
  {
    Scanner in;
    in = new Scanner(System.in);

    int money;
    int bet;            // Amount user bets on a game.
    boolean userWins = true;   // Did the user win the game?

    System.out.println("Welcome to Lunas BlackJack table. How's it goin?");
    System.out.println();

    money = 100;  // User starts with $100.

    while (true)
    {
      System.out.println("You have " + money + " dollars.");
      do
      {
        System.out.println("How much do you wanna bet? Or if you're done, enter 0 to walk away from the tables.)");
        System.out.print("$");

        bet = in.nextInt();
        if (bet < 0 || bet > money)
        {
          System.out.println("Your bet must be between 0 and " + money + '.');
        }
      }
      while (bet < 0 || bet > money);
      {
        if (bet == 0)
        {
          System.out.println("The dealer waves goodbye.");
          break; //walk away
        }

        userWins = playBlackjack();
        if (userWins == true)
        {
          money = money + bet;
        }
        if (userWins == false)
        {
          money = money - bet;
          System.out.println();
        }
        if (money == 0)
        {
          System.out.println("Aw shoot, looks like you've are out of money!");
          break;
        }
      }
    }

    System.out.println();
    System.out.println("You walk away with $" + money + '.');

  } //end method


  public static boolean playBlackjack()
  {
    String anotherCard;
    String playAgain = "y";
    String ctn;

    int nextCard;
    int card1;
    int card2;
    int dCard1;
    int dCard2;
    int cardTotal = 0;
    int dTotal = 0;

    Scanner in = new Scanner(System.in);
    Random random = new Random();

    // Begin dealing the players first two cards

    if (playAgain.equals("y"))
    {
      //dealers first two random cards
      dCard1 = random.nextInt(11) + 1;
      dCard2 = random.nextInt(11) +1;

      //players first two random cards and card total
      card1 = random.nextInt(11) + 1;
      card2 = random.nextInt(11) + 1;
      cardTotal = card1 + card2;

      //Dealers two card total and display only one dealer card
      dTotal = dCard1 + dCard2;
      System.out.println("The dealer shows: " + dCard1);

      //Display players first two cards & card total
      System.out.println("First cards: " + card1 + ", " +card2);
      System.out.println("Total: "+ cardTotal);

      //Asks if want to deal another card
      System.out.print("Another card (y/n)?: ");
      anotherCard = in.nextLine();

      //If yes
      while (anotherCard.equals("y"))
      {//while1
        nextCard = random.nextInt(10) + 1;
        cardTotal += nextCard;
        System.out.println("Card: " + nextCard);
        System.out.println("Total: " + cardTotal);

        if (cardTotal > 21)
        {//if1
          System.out.println("You busted, the dealer Wins");
          System.out.println("Do you want to play again? (y/n): ");
          playAgain = in.nextLine();
          return false;
        }//if1

        if (cardTotal < 21)
        {//if2
          System.out.print("Another Card (y/n)?: ");
          anotherCard = in.nextLine();
        }//if2

        if (anotherCard == "n")
        {//if3

          System.out.print("Press c to continue dealers cards");
          ctn = in.nextLine();

          while (ctn == "c" && dTotal < 17)
          {//while2
            nextCard = random.nextInt(10) + 1;
            dTotal += nextCard;
          }//while2

          if (dTotal > 21)
          {//if4
            System.out.println("Dealer Busts, You Win!");
            System.out.println("Play Again? (y/n): ");
            playAgain = in.nextLine();
            return false;
          }//if4

          if (playAgain.equalsIgnoreCase("y")) //ignorecase = ignore capitals/lowercase
          {//if5
            playAgain = "y";
          }//if5

          if (playAgain.equalsIgnoreCase("n")) //no!
          {//else1
            System.exit(0);
          }//else1
        }//if3

      }//while1
    }//END BIG WHILE
    return true;
  }//end playBlackJack()
} //end main
公共类程序blackj
{//if1
  System.out.println("You busted, the dealer Wins");
  System.out.println("Do you want to play again? (y/n): ");
  playAgain = in.nextLine();
  return false;
}//if1
public class ProgramBlackJack
{

  public static void main(String[] args)throws IOException
  {
    Scanner in;
    in = new Scanner(System.in);

    int money;
    int bet;            // Amount user bets on a game.
    boolean userWins = true;   // Did the user win the game?

    System.out.println("Welcome to Lunas BlackJack table. How's it goin?");
    System.out.println();

    money = 100;  // User starts with $100.

    while (true)
    {
      System.out.println("You have " + money + " dollars.");
      do
      {
        System.out.println("How much do you wanna bet? Or if you're done, enter 0 to walk away from the tables.)");
        System.out.print("$");

        bet = in.nextInt();
        if (bet < 0 || bet > money)
        {
          System.out.println("Your bet must be between 0 and " + money + '.');
        }
      }
      while (bet < 0 || bet > money);
      {
        if (bet == 0)
        {
          System.out.println("The dealer waves goodbye.");
          break; //walk away
        }

        userWins = playBlackjack();
        if (userWins == true)
        {
          money = money + bet;
        }
        if (userWins == false)
        {
          money = money - bet;
          System.out.println();
        }
        if (money == 0)
        {
          System.out.println("Aw shoot, looks like you've are out of money!");
          break;
        }
      }
    }

    System.out.println();
    System.out.println("You walk away with $" + money + '.');

  } //end method


  public static boolean playBlackjack()
  {
    String anotherCard;
    String playAgain = "y";
    String ctn;

    int nextCard;
    int card1;
    int card2;
    int dCard1;
    int dCard2;
    int cardTotal = 0;
    int dTotal = 0;

    Scanner in = new Scanner(System.in);
    Random random = new Random();

    // Begin dealing the players first two cards

    if (playAgain.equals("y"))
    {
      //dealers first two random cards
      dCard1 = random.nextInt(11) + 1;
      dCard2 = random.nextInt(11) +1;

      //players first two random cards and card total
      card1 = random.nextInt(11) + 1;
      card2 = random.nextInt(11) + 1;
      cardTotal = card1 + card2;

      //Dealers two card total and display only one dealer card
      dTotal = dCard1 + dCard2;
      System.out.println("The dealer shows: " + dCard1);

      //Display players first two cards & card total
      System.out.println("First cards: " + card1 + ", " +card2);
      System.out.println("Total: "+ cardTotal);

      //Asks if want to deal another card
      System.out.print("Another card (y/n)?: ");
      anotherCard = in.nextLine();

      //If yes
      while (anotherCard.equals("y"))
      {//while1
        nextCard = random.nextInt(10) + 1;
        cardTotal += nextCard;
        System.out.println("Card: " + nextCard);
        System.out.println("Total: " + cardTotal);

        if (cardTotal > 21)
        {//if1
          System.out.println("You busted, the dealer Wins");
          System.out.println("Do you want to play again? (y/n): ");
          playAgain = in.nextLine();
          return false;
        }//if1

        if (cardTotal < 21)
        {//if2
          System.out.print("Another Card (y/n)?: ");
          anotherCard = in.nextLine();
        }//if2

        if (anotherCard == "n")
        {//if3

          System.out.print("Press c to continue dealers cards");
          ctn = in.nextLine();

          while (ctn == "c" && dTotal < 17)
          {//while2
            nextCard = random.nextInt(10) + 1;
            dTotal += nextCard;
          }//while2

          if (dTotal > 21)
          {//if4
            System.out.println("Dealer Busts, You Win!");
            System.out.println("Play Again? (y/n): ");
            playAgain = in.nextLine();
            return false;
          }//if4

          if (playAgain.equalsIgnoreCase("y")) //ignorecase = ignore capitals/lowercase
          {//if5
            playAgain = "y";
          }//if5

          if (playAgain.equalsIgnoreCase("n")) //no!
          {//else1
            System.exit(0);
          }//else1
        }//if3

      }//while1
    }//END BIG WHILE
    return true;
  }//end playBlackJack()
} //end main