简体   繁体   English

用Java编写21点和单人纸牌程序时遇到麻烦

[英]Trouble writing blackjack and solitaire program in java

I have been working on this project for a few days and I can't figure out why it's not working. 我已经在这个项目上工作了几天,但我不知道为什么它不起作用。 Hopefully a fresh set of eyes will help. 希望新的眼睛会有所帮助。 When I run this, several things don't happen. 当我运行此命令时,不会发生几件事。

The only thing that really works is the part that manages the bet from the user. 唯一有效的是管理用户下注的部分。 Some problems include: 一些问题包括:

  • It does not bring a card value (supposed to come from a class that randomly generates a number from 1-13). 它不会带来卡值(假定来自随机生成1-13之间的数字的类)。

  • Does not wait for the user to enter 0 to draw more cards or not 不等待用户输入0来绘制更多牌

  • When it says what the third card is it says Card@42a57993 当它说出第三张卡是什么时,它说出Card@42a57993
  • Gives no total card value. 不提供总卡值。
  • There's also an error that happens when I try to fix a problem. 当我尝试解决问题时,还会发生一个错误。 In the addition for cardTotal, the error says "the operator + is undefined for the argument types." 除了cardTotal之外,错误提示“对于参数类型,运算符+未定义”。

It's possible that It is missing or has extra brackets but I can't find where they should/shouldn't be. 它可能丢失或有多余的括号,但我找不到它们应该/不应该在的位置。 Here is my code: 这是我的代码:

import java.util.Scanner; 

public class Game {
   private double pot;
   private double bet; 
   private int answer; 
   private int cardTotal; 

   Card card1 = new Card();
   Card card2 = new Card();
   Card card3 = new Card(); 

   // constructor
   public Game(){
      pot = 100;
      bet = 0; 
   }

   Scanner keyboard = new Scanner(System.in);

   // GetBetFrom user, prompts user to enter a valid bet, reenter until a valid amount
   public double getBetFromUser(){
      System.out.println("Your current pot is: "+pot);

      System.out.println("Enter your bet amount");
      bet = keyboard.nextDouble();  

      //Rules for the bet
      while(bet>pot || bet<0){
         if(pot==0){
            break; 
         }
         if(bet>pot){
            System.out.println("Sorry, you don't have enough money for that, try again");
            bet = keyboard.nextDouble(); 
         }
         if(bet<0){
            System.out.println("That is not a valid bet, please try again");
            bet = keyboard.nextDouble(); 
         }
     }
     return bet;
  }

  // Game mechanics 
  public void gamePlay(){
     System.out.println("Your first card is: "+card1);
     System.out.println("Your second card is: "+card2);
     cardTotal= card1 + card2; 
     System.out.println("Your total is: "+cardTotal);
     System.out.println("Enter 0 to draw more, any other number to quit: ");

     if(answer == 0); {
        System.out.println("Your next card is: "+card3);
        System.out.println("Your total is: "); 
     }
 }

 public void results(){
    if(cardTotal > 21){
       System.out.println("You lose your bet");
       pot = pot-bet; 
    }
    if(cardTotal==21){
       System.out.println("You win three times your bet");
       pot = pot-bet+(bet*3);
    }
    if(cardTotal==20){
       System.out.println("You win double your bet amount");
       pot = pot-bet+(bet*2); 
    }
    if(cardTotal<=19){
       System.out.println("You win your bet amount");
    }
 }
}

This is the code for my Card class 这是我的Card类的代码

import java.util.Random; 

public class Card {
private int cardValue; 
Random random = new Random();

public Card(){
    cardValue = 1;
}

public int getCard(){
    cardValue = (cardValue + random.nextInt(12));
    if(cardValue>10)
        return 10;
    else
        return cardValue;   
}

public int display(){
    switch (cardValue){
    case 1: System.out.println("Ace");
    break;
    case 2: System.out.println("Two");
    break; 
    case 3: System.out.println("Three"); 
    break; 
    case 4: System.out.println("Four");
    break;
    case 5: System.out.println("Five");
    break;
    case 6: System.out.println("Six");
    break;
    case 7: System.out.println("Seven");
    break; 
    case 8: System.out.println("Eight");
    break;
    case 9: System.out.println("Nine");
    break; 
    case 10: System.out.println("Ten");
    break; 
    case 11: System.out.println("Jack");
    break;
    case 12: System.out.println("Queen");
    break;
    case 13: System.out.println("King");
    break; 

    }   
    return cardValue; 
}       

} }

I'm guessing that your error is happening here: 我猜你的错误正在这里发生:

cardTotal= card1 + card2; 

card1 and card2 are Card types. card1card2Card类型。 the compiler doesn't know how to add them. 编译器不知道如何添加它们。

You should get your card total like this: 您应该这样获取卡总数:

cardTotal = card1.cardValue+ card2.cardValue;

This may help you. 这可能对您有帮助。 I suggest you read about how the objects and their methods are referenced. 我建议您阅读有关对象及其方法的引用方法。 Validation of the data entered by keyboard missing, but that's your job: 缺少通过键盘输入的数据的验证,但这是您的工作:

import java.util.Scanner;
public class Game {
   private double pot;
   private double bet; 
   private int answer; 
   private int cardTotal;

   Card card1 = new Card();
   Card card2 = new Card();
   Card card3 = new Card(); 

   // constructor
   public Game(){
      pot = 100;
      bet = 0; 
   }

   Scanner keyboard = new Scanner(System.in);

   // GetBetFrom user, prompts user to enter a valid bet, reenter until a valid amount
   public double getBetFromUser(){
      System.out.println("Your current pot is: "+pot);

      System.out.print("Enter your bet amount: ");
      bet = keyboard.nextDouble();  

      //Rules for the bet
      while(bet>pot || bet<0){
         if(pot==0){
            break; 
         }
         if(bet>pot){
            System.out.println("Sorry, you don't have enough money for that, try again");
            bet = keyboard.nextDouble(); 
         }
         if(bet<0){
            System.out.println("That is not a valid bet, please try again");
            bet = keyboard.nextDouble(); 
         }
     }
     return bet;
  }

  // Game mechanics 
  public void gamePlay(){
     int first= card1.getCard();
     int second = card2.getCard();
     int third = card3.getCard();
     System.out.println("Your first card is: "+first);
     System.out.println("Your second card is: "+second);
     cardTotal= first + second;
     System.out.println("Your total is: "+cardTotal);
     System.out.print("Enter 0 to draw more, any other number to quit: ");
     answer = keyboard.nextInt(); 

     if(answer == 0) {
        System.out.println("Your next card is: "+third);
        cardTotal=cardTotal + third;
     }
     System.out.println("Your total is: "+cardTotal); 
 }

 public void results(){
    if(cardTotal > 21){
       System.out.println("You lose your bet");
       pot = pot-bet; 
    }
    if(cardTotal==21){
       System.out.println("You win three times your bet");
       pot = pot-bet+(bet*3);
    }
    if(cardTotal==20){
       System.out.println("You win double your bet amount");
       pot = pot-bet+(bet*2); 
    }
    if(cardTotal<=19){
       System.out.println("You win your bet amount");
    }
 }

 public static void main(String[] args) {
      Game game = new Game();
      game.getBetFromUser();
      game.gamePlay();
      game.results();
 }     

} }

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

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