麻烦在java中写二十一点和纸牌程序

时间:2014-11-14 20:37:48

标签: java blackjack

我已经在这个项目上工作了几天,我无法弄清楚它为什么不起作用。希望一双新鲜的眼睛会有所帮助。当我运行时,有几件事情不会发生。

唯一真正有效的是管理用户赌注的部分。一些问题包括:

  • 它没有带来卡片值(应该来自一个从1-13中随机生成数字的类)。

  • 不等待用户输入0来抽取更多卡片

  • 当它说出第三张卡的内容时,它会显示Card@42a57993
  • 不会给总卡价值。
  • 当我尝试解决问题时,也会发生错误。在cardTotal的补充中,错误显示“参数类型的运算符+未定义。”

它可能缺失或有额外的括号,但我找不到它们应该/不应该在哪里。这是我的代码:

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");
    }
 }
}

这是我的卡类的代码

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; 
}       

}

2 个答案:

答案 0 :(得分:1)

我猜测你的错误发生在这里:

cardTotal= card1 + card2; 

card1card2Card类型。编译器不知道如何添加它们。

你应该像这样得到你的卡片:

cardTotal = card1.cardValue+ card2.cardValue;

答案 1 :(得分:0)

这可能会对你有所帮助。我建议你阅读有关如何引用对象及其方法的内容。验证键盘输入的数据缺失,但这是你的工作:

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();
 }     

}