简体   繁体   English

如何返回我的 getWin,而不仅仅是 true 或 false

[英]How to return my getWin with more than just true or false

I'm trying to get my program to take multiple different return statements so i can have a win split and lose rather than just win or lose and cant seem to figure out how to do it.我试图让我的程序采用多个不同的返回语句,这样我就可以分出胜负,而不仅仅是赢或输,而且似乎不知道该怎么做。 My program is made to be able to have either you win and it doubles your bet that you made.我的程序可以让你赢,而且你的赌注加倍。 If you tie you get your money back.如果你打领带,你会拿回你的钱。 if you lose you lose your bet.如果你输了,你就输了。 Ive gotten win and lose to work previously with using a booelean and returning true or false but needed a way to add in a way to do split.我以前使用布尔值并返回真或假,但需要一种方法来添加一种方法来进行拆分。

I've tried multiple methods I've seen online but to no avail any help would be appreciated.我尝试了多种我在网上看到的方法,但无济于事,我们将不胜感激。

//Import Random Number Generator
import java.util.Random;

class BlackJackPlayer{
   //Keep the data secure by using private
   private String hand;
   private int sum;
   private int numAces;
   private static Random gen = new Random();
   private String Win;
   private String Lose;
   private String Split;

   private final int ACE = 1;
   private final int JACK = 11;
   private final int QUEEN = 12;
   private final int KING = 13;

   //constructor
   public BlackJackPlayer(){
      hand = "";
      sum = 0;
      numAces = 0;
   }

   //Getter for hand variable
   public String getHand(){
      return hand;
   }

   public String setHand(){
      hand = " ";
      return hand;
   }

   //Getter for sum variable
   public int getSum(){
      return sum;
   }

   public void hit(){
      //local variable
      int currentCard = gen.nextInt(13) + 1;

      if(currentCard > ACE && currentCard < JACK){
         sum += currentCard;
         hand += currentCard + "   ";
      }
      else if(currentCard == ACE){
         sum += 11;
         numAces++;
         hand += "A  ";
      }

      else if(currentCard == QUEEN){
         sum += 10;
         hand += "Q  ";
      }

      else if(currentCard == QUEEN){
         sum += 10;
         hand += "Q  ";
      }

      else if(currentCard == KING){
         sum += 10;
         hand += "K  ";
      }//Ends Else If

      //Is Ace 1 or 11
      if(sum > 21 && numAces > 0){
         numAces--;
         sum -= 10;
      }

   }//ENDS HIT

   public String getWin(BlackJackPlayer other) {
      if(sum > 21){
         Win = "Win";
      }
      else if(sum < other.getSum()){
         Lose = "Lose";
      }
      else if(sum == other.getSum()){
         Split = "Split";

   }
   return Win;
      }


}//end main



import java.util.Scanner;
class BlackJackGame{
   public static void main(String args[]){
      BlackJackPlayer you = new BlackJackPlayer();
      BlackJackPlayer enemy = new BlackJackPlayer();
      int chips = 100;
      int bet;
      int winnings;
      int multiply = 2;
      String qORc;
      Scanner in = new Scanner(System.in);

      String choice;
      do{
      you.setHand();
      enemy.setHand();
      you.hit();
      enemy.hit();
      enemy.hit();
         System.out.println("Chips: " + chips);
         System.out.println("How much do you want to bet");
         bet = in.nextInt();
         chips -= bet;
         System.out.println("You bet: " + bet + ", You now have " + chips + " chips");
      do{
         System.out.println("Your Cards:  " + you.getHand());         
         System.out.println("(H)it or (S)tand");
         choice = in.next();
         if(choice.equalsIgnoreCase("h")){
            you.hit();
         }
      }while(choice.equalsIgnoreCase("h"));

      while(enemy.getSum() < 16){
         enemy.hit();
      }
      System.out.println(you.getWin());
       if(you.getWin()){
         System.out.println("You Win");
         System.out.println(enemy.getHand());
         winnings = bet * multiply;
         chips += winnings;
         System.out.println("You now have: " + chips + " chips!");
      }
      else{
         System.out.println("You Lose");
         System.out.println(enemy.getHand());
         System.out.println("You now have: " + chips + " chips.");
      }
               System.out.println("(C)ontinue or (Q)uit");
         qORc = in.next();
      }while(chips > 0 && qORc.equalsIgnoreCase("c"));

   }//end main

}//end class

I expect to be able to get different return statements that way I can actually set a win aspect a lose aspect but also a split aspect if both players tie.我希望能够得到不同的返回语句,这样我实际上可以设置胜利方面和失败方面,但如果两个玩家打成平手,也可以设置分裂方面。

You have three results to return which are: WIN, LOSE and TIE right?您要返回三个结果,它们是:WIN、LOSE 和 TIE,对吗?

In that case you have to use three different variables like,在这种情况下,您必须使用三个不同的变量,例如,

public String Win = "Win",Lose = "Lose",Split = "Split"; 

and use other variable to store result like result and return this result in getWin() .并使用其他变量来存储结果,如result ,并在getWin()中返回此结果。 See below code:见下面的代码:

import java.util.Random;
import java.util.Scanner;
class BlackJackPlayer{
   //Keep the data secure by using private
   private String hand;
   private int sum;
   private int numAces;
   private static Random gen = new Random();
   public String Win = "Win",Lose = "Lose",Split = "Split";
   private String result = "";
   private final int ACE = 1;
   private final int JACK = 11;
   private final int QUEEN = 12;
   private final int KING = 13;

   //constructor
   public BlackJackPlayer(){
      hand = "";
      sum = 0;
      numAces = 0;
   }

   //Getter for hand variable
   public String getHand(){
      return hand;
   }

   public String setHand(){
      hand = " ";
      return hand;
   }

   //Getter for sum variable
   public int getSum(){
      return sum;
   }

   public void hit(){
      //local variable
      int currentCard = gen.nextInt(13) + 1;

      if(currentCard > ACE && currentCard < JACK){
         sum += currentCard;
         hand += currentCard + "   ";
      }
      else if(currentCard == ACE){
         sum += 11;
         numAces++;
         hand += "A  ";
      }

      else if(currentCard == QUEEN){
         sum += 10;
         hand += "Q  ";
      }

      else if(currentCard == QUEEN){
         sum += 10;
         hand += "Q  ";
      }

      else if(currentCard == KING){
         sum += 10;
         hand += "K  ";
      }//Ends Else If

      //Is Ace 1 or 11
      if(sum > 21 && numAces > 0){
         numAces--;
         sum -= 10;
      }

    }//ENDS HIT

   public String getWin(BlackJackPlayer other) {
      if(sum > 21){
         result = Win;
      }
      else if(sum < other.getSum()){
          result = Lose;
      }
      else if(sum == other.getSum()){
          result = Split;
      }
   return result;
  }
}//end main

class BlackJackGame{
    public static void main(String args[]){
       BlackJackPlayer you = new BlackJackPlayer();
       BlackJackPlayer enemy = new BlackJackPlayer();
       int chips = 100,bet,winnings,multiply = 2;
       String qORc;
       Scanner in = new Scanner(System.in);

       String choice;
       do{
          you.setHand();
          enemy.setHand();
          you.hit();
          enemy.hit();
          enemy.hit();
          System.out.println("Chips: " + chips);
          System.out.println("How much do you want to bet");
          bet = in.nextInt();
          chips -= bet;
          System.out.println("You bet: " + bet + ", You now have " + chips + " chips");
          do{
             System.out.println("Your Cards:  " + you.getHand());         
             System.out.println("(H)it or (S)tand");
             choice = in.next();
             if(choice.equalsIgnoreCase("h")){
                 you.hit();
             }
          }while(choice.equalsIgnoreCase("h"));

          while(enemy.getSum() < 16){
             enemy.hit();
          }
          String result = you.getWin(enemy);
          System.out.println(result);
          if(result == you.Win){
              System.out.println("You Win");
              System.out.println(enemy.getHand());
              winnings = bet * multiply;
              chips += winnings;
              System.out.println("You now have: " + chips + " chips!");
          }
          else if (result == you.Lose){
              System.out.println("You Lose");
              System.out.println(enemy.getHand());
              System.out.println("You now have: " + chips + " chips.");
          }else{
              System.out.println("You Split");
          }
          System.out.println("(C)ontinue or (Q)uit");
         qORc = in.next();
     }while(chips > 0 && qORc.equalsIgnoreCase("c"));

   }//end main

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM