当我输入int时,我有一个随机发生的错误,有人知道什么是错的吗?

时间:2016-05-26 02:33:56

标签: java java.util.scanner poker

好的,所以我正在为学校的年终项目制作二十一点游戏,但当我选择在游戏中“站立”时,代码可能会随机忽略我的输入,然后停止一起工作。有人能搞清楚吗?我和我的老师都不能。 (对不起,有这么多代码,我不知道你是否需要所有课程来帮助我)

转轮:

import static java.lang.System.*;
import java.util.Arrays;

public class BlackJackRunner
{
   public static void main( String args[] )
    {
      Deck deck = new Deck();
      Game game = new Game();
      game.startGame();
      // work on resetting btwn each hand and still work on choices i think
   }
}

甲板课程:

public class Deck
{
   public int[] card = new int[2];
   public String number = "";
   public Deck()
   {

   }
   public void drawCard()
   {
      int[] drawn = new int[2];
      int switc;
      //do{
      do{
         switc = Rndm.i(0, 51);
         String sw = switc + " ";

         if(number.indexOf(sw) == -1)
         {
            if (number.indexOf(sw) > -1)
               System.out.println("Problem!!");
            switch(switc)
            {
               //face first, then suit;
               //1 - spades   2-clubs  3-hearts 4-diamonds


              //2-10 - face value  j-11  q-12  k-13  a-1
               case 0 : drawn[0] = 2;   drawn[1] = 1;   number = number + 0 + " ";   break; 
               case 1 : drawn[0] = 3;   drawn[1] = 1;   number = number + 1 + " ";   break;
           case 2 : drawn[0] = 4;   drawn[1] = 1;   number = number + 2 + " ";   break;
           case 3 : drawn[0] = 5;   drawn[1] = 1;   number = number + 3 + " ";   break;
           case 4 : drawn[0] = 6;   drawn[1] = 1;   number = number + 4 + " ";   break;
           case 5 : drawn[0] = 7;   drawn[1] = 1;   number = number + 5 + " ";   break; 
           case 6 : drawn[0] = 8;   drawn[1] = 1;   number = number + 6 + " ";   break;
           case 7 : drawn[0] = 9;   drawn[1] = 1;   number = number + 7 + " ";   break;
           case 8 : drawn[0] = 10;   drawn[1] = 1;   number = number + 8 + " ";   break;
           case 9 : drawn[0] = 11;   drawn[1] = 1;   number = number + 9 + " ";   break;
           case 10 : drawn[0] = 12;   drawn[1] = 1;   number = number + 10 + " ";   break; 
           case 11 : drawn[0] = 13;   drawn[1] = 1;   number = number + 11 + " ";   break;
           case 12 : drawn[0] = 1;   drawn[1] = 1;   number = number + 12 + " ";   break;
           case 13 : drawn[0] = 2;   drawn[1] = 2;   number = number + 13 + " ";   break;
           case 14 : drawn[0] = 3;   drawn[1] = 2;   number = number + 14 + " ";   break;
           case 15 : drawn[0] = 4;   drawn[1] = 2;   number = number + 15 + " ";   break; 
           case 16 : drawn[0] = 5;   drawn[1] = 2;   number = number + 16 + " ";   break;
           case 17 : drawn[0] = 6;   drawn[1] = 2;   number = number + 17 + " ";   break;
           case 18 : drawn[0] = 7;   drawn[1] = 2;   number = number + 18 + " ";   break;
           case 19 : drawn[0] = 8;   drawn[1] = 2;   number = number + 19 + " ";   break;
           case 20 : drawn[0] = 9;   drawn[1] = 2;   number = number + 20 + " ";   break; 
           case 21 : drawn[0] = 10;   drawn[1] = 2;   number = number + 21 + " ";   break;
           case 22 : drawn[0] = 11;   drawn[1] = 2;   number = number + 22 + " ";   break;
           case 23 : drawn[0] = 12;   drawn[1] = 2;   number = number + 23 + " ";   break;
           case 24 : drawn[0] = 13;   drawn[1] = 2;   number = number + 24 + " ";   break;
           case 25 : drawn[0] = 1;   drawn[1] = 2;   number = number + 25 + " ";   break; 
           case 26 : drawn[0] = 2;   drawn[1] = 3;   number = number + 26 + " ";   break;
           case 27 : drawn[0] = 3;   drawn[1] = 3;   number = number + 27 + " ";   break;
           case 28 : drawn[0] = 4;   drawn[1] = 3;   number = number + 28 + " ";   break;
           case 29 : drawn[0] = 5;   drawn[1] = 3;   number = number + 29 + " ";   break;
           case 30 : drawn[0] = 6;   drawn[1] = 3;   number = number + 30 + " ";   break; 
           case 31 : drawn[0] = 7;   drawn[1] = 3;   number = number + 31 + " ";   break;
           case 32 : drawn[0] = 8;   drawn[1] = 3;   number = number + 32 + " ";   break;
           case 33 : drawn[0] = 9;   drawn[1] = 3;   number = number + 33 + " ";   break;
           case 34 : drawn[0] = 10;   drawn[1] = 3;   number = number + 34 + " ";   break;
           case 35 : drawn[0] = 11;   drawn[1] = 3;   number = number + 35 + " ";   break; 
           case 36 : drawn[0] = 12;   drawn[1] = 3;   number = number + 36 + " ";   break;
           case 37 : drawn[0] = 13;   drawn[1] = 3;   number = number + 37 + " ";   break;
           case 38 : drawn[0] = 1;   drawn[1] = 3;   number = number + 38 + " ";   break;
           case 39 : drawn[0] = 2;   drawn[1] = 4;   number = number + 39 + " ";   break;
           case 40 : drawn[0] = 3;   drawn[1] = 4;   number = number + 40 + " ";   break; 
           case 41 : drawn[0] = 4;   drawn[1] = 4;   number = number + 41 + " ";   break;
           case 42 : drawn[0] = 5;   drawn[1] = 4;   number = number + 42 + " ";   break;
           case 43 : drawn[0] = 6;   drawn[1] = 4;   number = number + 43 + " ";   break;
           case 44 : drawn[0] = 7;   drawn[1] = 4;   number = number + 44 + " ";   break;
           case 45 : drawn[0] = 8;   drawn[1] = 4;   number = number + 45 + " ";   break; 
           case 46 : drawn[0] = 9;   drawn[1] = 4;   number = number + 46 + " ";   break;
           case 47 : drawn[0] = 10;   drawn[1] = 4;   number = number + 47 + " ";   break;
           case 48 : drawn[0] = 11;   drawn[1] = 4;   number = number + 48 + " ";   break;
           case 49 : drawn[0] = 12;   drawn[1] = 4;   number = number + 49 + " ";   break;
           case 50 : drawn[0] = 13;   drawn[1] = 4;   number = number + 50 + " ";   break;
           case 51 : drawn[0] = 1;   drawn[1] = 4;   number = number + 51 + " ";   break;

        }
     }
  }while(drawn[0]==0 && drawn[1]==0);
  //}while(number.indexOf(switc) > -1);
  if (drawn[0]==0 && drawn[1]==0)
     System.out.println("Problem!!");


     card[0] = drawn[0];
     card[1] = drawn[1];


  }
   public void shuffleDeck()
   {
      number = "";
   }
   public int[] getCard()
   {
      drawCard();
      return card;
   }
   public String toString()
   {
      return card[0] + " " + card[1];
   }
}

卡类:

public class Card extends Deck
{
    public static final String FACES[] = {"ZERO","Ace","Two","Three","Four",
            "Five","Six","Seven","Eight","Nine","Ten","Jack","Queen","King"};
   public static final String SUITS[] = {"ZERO", "Spades", "Clubs", "Hearts", "Diamonds"};

    private int suit;
    private int face;


    public Card( int f, int s)
    {
        suit = s;
        face = f;
    }


    public void setFace( int f)
    {
        face = f;
    }

    public void setSuit( int s)
    {
        suit = s;
    }


    public int getSuit()
    {
        return suit;
    }

    public int getFace()
    {
        return face;
    }


    public String toString()
    {
        return FACES[face] + " of " + SUITS[suit];
    }
 }

BlackJackCard类:

public class BlackJackCard extends Card
{
   private int value;
   int handVal, dealerHandVal;
   int val;
    public BlackJackCard(int f, int s)
   {
      super(f,s);
   }

    public int getVal()
   {
      if(getFace() == 1)
      {
         if(val + 11 > 21)
         {
            value = 1;
         }
         else
         {
            value = 11;
         }
      }
      if(getFace() > 1 && getFace() < 10)
      {
         value = getFace();
      }
      if(getFace() > 10)
      {
         value = 10;
      }
      return value;
   }

   public String toString()
   {
      return FACES[getFace()] + " of " + SUITS[getSuit()]+ " ";
   }
}

游戏课程:

import static java.lang.System.*;
import java.util.Scanner;
import java.util.Arrays;

public class Game
{
   Scanner keyboard = new Scanner(System.in);
   Deck deck = new Deck();
   public int money, bet;
   int[] card = new int[2];
   int[][] playerHand = new int[10][2];
   int[][] dealerHand = new int[10][2];
   int handPlace;
   int pHandVal;
   int dHandVal;
   boolean bust;
   boolean dBust;



   public void startGame()
   {
      out.println("Hello! Welcome to the Compsci Casino!");
      out.println("We have a game of Blackjack prepared for you right here.");
      out.println("It seems you've brought in $1000 in chips. You may use that to bet on your games.");
      out.println("Here is your table, good luck!");
      money = 1000;
      out.println("\n\n");
      do{
         startRound();
      }while(money > 0);
      out.println("Well, it looks like you're broke. Come back when you've got some money to bet.");
   }




   public void startRound()
   {
      out.println("Alright, before I deal anything, you gotta bet some of your chips.");
      bet();
      deal();
      game();
   }




   public void printCards()
   {
      out.println("Dealer's card : " + getCardName(dealerHand[0][0], dealerHand[0][1]) + "\n");
      out.println("Your hand : ");
      for(int i = 0; i < 10; i++)
      {
         if(playerHand[i][0] != 0)
         {
            out.println(getCardName(playerHand[i][0], playerHand[i][1]));

         }
      }
      out.println("");

   }




   public void game()
   {
         int c;
         do{
         printCards();
         c = choice();
         if(c == 1)
         {
            handPlace = 0;
            for(int i = 0; i < 10; i++)
            {
               if(playerHand[i][0] != 0)
               {
                  handPlace += 1;               
               }
            }
            card = deck.getCard();
            playerHand[handPlace][0] = card[0];
            playerHand[handPlace][1] = card[1];
            pHandVal += getHandVal(playerHand[handPlace][0], playerHand[handPlace][1]);
            if(checkBust(pHandVal) == true)
            {
               out.println("you busted" + "\n");
            }
         }
         else if(c == 2)
         {
            do{
               if(dHandVal <= pHandVal)
               {
                  dealerPlay();
               }
            }while(checkBust(dHandVal) != true);
         }
         else if(c == 3)
         {
            money -= bet;
            bet = bet*2;
         }
         bust = checkBust(pHandVal);
         dBust = checkBust(dHandVal);

      }while(c != 2 && bust != true);

      printDealerHand();
      if(dBust == true)
      {
         out.println("Dealer busts, you win!");
         money += bet*2;
      }
      else if(dBust != true)
      {
         if(bust != true && pHandVal > dHandVal)
         {
            out.print("you win!");
            money += bet*2;
         }
      }
      else if(bust == true)
      {
         out.println("you busted" + "\n");
      }
      else if(dHandVal > pHandVal)
      {
         out.println("Dealer wins");
      }
   }



   public void printDealerHand()
   {
      out.println("Dealer's cards : " + "\n");
      for(int i = 0; i < 10; i++)
      {
         if(dealerHand[i][0] != 0)
         {
            out.println(getCardName(dealerHand[i][0], dealerHand[i][1]));

         }
      }
      out.println("Your hand : ");
      for(int i = 0; i < 10; i++)
      {
         if(playerHand[i][0] != 0)
         {
            out.println(getCardName(playerHand[i][0], playerHand[i][1]));

         }
      }
      out.println("");

   }




   public int getHandVal(int f, int s)
   {
      BlackJackCard bjc = new BlackJackCard(f, s);
      int value = bjc.getVal();
      return value;
   }




   public boolean checkBust(int val)
   {
      if(val > 21)
      {
         return true;
      }
      return false;
   }




   public void dealerPlay()
   {
       handPlace = 0;
      for(int i = 0; i < 10; i++)
       {
          if(dealerHand[i][0] != 0)
         {
           handPlace += 1;               
         }
     }
     card = deck.getCard();
     dealerHand[handPlace][0] = card[0];
     dealerHand[handPlace][1] = card[1];
     dHandVal += getHandVal(dealerHand[handPlace][0], dealerHand[handPlace][1]);
   }




   public String getCardName(int f, int s)
   {
      BlackJackCard blackJackCard = new BlackJackCard(f, s);
      return blackJackCard.toString();
   }




   public void bet()
   {
      bet = 0;
      do{
         out.println("you have " + money + " dollars left." + "\n");
         out.println("How much will you bet?");
         out.print("Amount :: ");
         bet = keyboard.nextInt();
         if(bet == 0)
         {
            out.println("It aint free to play here, you cant just bet nothing.");
         }
      }while(bet == 0);
      money = money - bet;
      out.println("\n");
   }




   public int choice()
   {
      int choice = 0;
      do{
      out.println("What will you do?");
      out.println("1 - Hit");
      out.println("2 - Stand");
      out.println("3 - Double");
      choice = keyboard.nextInt();

         if(choice == 1|| choice == 2|| choice == 3)
         {
            return choice;
         }
      }while(choice != 1 || choice != 2 || choice != 3);
      return 1;
   }




   public void resetHands()
   {
      pHandVal = 0;
      playerHand[0][0] = 0;
      playerHand[0][1] = 0;
      playerHand[1][0] = 0;
      playerHand[1][1] = 0;
      playerHand[2][0] = 0;
      playerHand[2][1] = 0;
      playerHand[3][0] = 0;
      playerHand[3][1] = 0;
      playerHand[4][0] = 0;
      playerHand[4][1] = 0;
      playerHand[5][0] = 0;
      playerHand[5][1] = 0;
      playerHand[6][0] = 0;
      playerHand[6][1] = 0;
      playerHand[7][0] = 0;
      playerHand[7][1] = 0;
      playerHand[8][0] = 0;
      playerHand[8][1] = 0;
      playerHand[9][0] = 0;
      playerHand[9][1] = 0;
      dHandVal = 0;
      dealerHand[0][0] = 0;
      dealerHand[0][1] = 0;
      dealerHand[1][0] = 0;
      dealerHand[1][1] = 0;
      dealerHand[2][0] = 0;
      dealerHand[2][1] = 0;
      dealerHand[3][0] = 0;
      dealerHand[3][1] = 0;
      dealerHand[4][0] = 0;
      dealerHand[4][1] = 0;
      dealerHand[5][0] = 0;
      dealerHand[5][1] = 0;
      dealerHand[6][0] = 0;
      dealerHand[6][1] = 0;
      dealerHand[7][0] = 0;
      dealerHand[7][1] = 0;
      dealerHand[8][0] = 0;
      dealerHand[8][1] = 0;
      dealerHand[9][0] = 0;
      dealerHand[9][1] = 0;

   }




   public void deal()
   {
      resetHands();
      card = deck.getCard();
      playerHand[0][0] = card[0];
      playerHand[0][1] = card[1];
      card = deck.getCard();
      playerHand[1][0] = card[0];
      playerHand[1][1] = card[1];
      pHandVal = getHandVal(playerHand[0][0], playerHand[0][1]);
      pHandVal += getHandVal(playerHand[1][0], playerHand[1][1]);
      card = deck.getCard();
      dealerHand[0][0] = card[0];
      dealerHand[0][1] = card[1];
      card = deck.getCard();
      dealerHand[1][0] = card[0];
      dealerHand[1][1] = card[1];
      dHandVal = getHandVal(dealerHand[0][0], dealerHand[0][1]);
      dHandVal += getHandVal(dealerHand[1][0], dealerHand[1][1]);

   }



}

和随机数生成器类:

public class Rndm
{   
   public static double d(double low, double high) 
   { 
      double r = Math.random()*(high-low)+low; 
      return r;
   }

   public static int i(int low, int high) 
   { 
      double r = Math.random()*(high-low+1)+low;  
      return (int)r;    
   }
}

我的问题似乎与Game类中的choice()方法有关。它时不时会被调用,它会忽略我的输入并看起来像是无限循环,但是当我查看调试器时,它只是显示扫描仪只是忽略了我的输入。

再一次,我很抱歉有这么多代码,但是没有人可以解决这个问题,我想知道是否有人可以提供帮助。

3 个答案:

答案 0 :(得分:0)

如果输入不是整数,则使用Scanner.nextInt()会有问题。它会引发异常,但不会清除缓冲区。

我建议您在try-catch块中使用String input = keyboard.nextLine()Integer.parseInt(input)之类的内容。

答案 1 :(得分:0)

输入Int之后你正在按键 - 这个字符不会被吞没,所以试试

choice = keyboard.nextInt();
keyboard.nextLine ();

答案 2 :(得分:0)

如果你使用eclipse,你可以使用调试器,它确实很有用。

您可以按行执行代码行,这样您就可以立即看到问题所在。

看看: video debugging