2012-11-03 63 views
2

我正在做一个java项目,我正在使用骰子来模拟二十一点游戏,但是,我碰到了一个我写错了代码的区域。如果球员得分高于他,我试图让经销商模拟行动。我的checkWinner()方法似乎在错误的时间开始活跃。我不是要求任何人修复它,而是告诉我代码错在哪里。使用骰子的二十一点游戏

public class BlackJack { 

    static PairOfDice cards = new PairOfDice(); 
    static Scanner scan = new Scanner(System. in); 
    static int playerScore, dealerScore, player, dealer, tempScore; 
    static boolean newGame; 
    static String play, hit; 


    public static void main(String[] args) { 
     newGame(); 
     dealerRoll(); 
     playerRoll(); 

     // checkWinner(); 
    } 

    public static void newGame() { 

     System.out.println("Would you like to play blackjack? y or n?"); 
     play = scan.nextLine(); 

     if (play == "y"); { 
      newGame = true; 
     } 
     if (play != "y") newGame = false; 
    } 

    public static void dealerRoll() { 
     while (newGame) { 
      cards.rollDice(); 
     } 

     dealerScore = (cards.rollDice()); 
     System.out.println("Dealer score is: " + dealerScore); 

     if (dealerScore <= 15 && playerScore > dealerScore) tempScore = cards.rollDice(); 
     System.out.println("The dice roll was: " + tempScore); 
     dealerScore += tempScore; 
     System.out.println("Dealer score is: " + dealerScore); 

    } 

    public static void playerRoll() { 
     while (newGame) { 
      cards.rollDice(); 
     } 

     playerScore = (cards.rollDice()); 

     System.out.println("You total is " + playerScore); 

     while (playerScore < 21 || playerScore < dealerScore) { 

      System.out.println("Would you like to take a hit? y or n?"); 
      hit = scan.nextLine(); 
      if (hit.equals("y")) { 
       tempScore = cards.rollDice(); 
       System.out.println("The dice roll was: " + tempScore); 
       playerScore += tempScore; 
       System.out.println("Your score is now: " + playerScore); 

      } 
      else if (hit.equals("n")); { 
       checkWinner(); 
      } 
     } 
    } 

    public static void checkWinner() { 
     if (playerScore == 21) System.out.println("You win!"); 
     else if (dealerScore == 21) System.out.println("Sorry, Dealer has won!"); 
     else if (dealerScore > playerScore) System.out.println("Sorry, Dealer has won!"); 
     else if (playerScore > dealer) System.out.println("You win!"); 
     else if (playerScore > 21) System.out.println("Sorry you busted, Dealer has won!"); 

     else if (dealerScore > 21) System.out.println("Dealer has busted, You win!"); 
    } 
}​ 
+0

首先是你的'而(newGame){...}'循环将不断地运行,如果'newGame'是正确的,因为没有办法了'newGame'成为内环路假。 –

+0

是啊,我只是意识到,当我发布的代码,我改变它来比较字符串,而不是引用。 –

+0

我只是想提一提,'cards.rollDice()'让我做了一个双重考虑。 – cHao

回答

0

当您完成玩家轮流时,您无法检查赢家。只有在庄家转身之后才能做到。

0

我redid的代码,并想出了这一点。任何意见将不胜感激。

import java.util.*; 


public class BlackJack 
{ 
    static PairOfDice cards = new PairOfDice(); 
    static Scanner scan = new Scanner(System.in); 
    static int playerScore, dealerScore, player, dealer, tempScore, takeHit, stand; 
    private static boolean playGame; 
    static String play= "y", yesPlay; 
    static char hit; 


    public static void main(String[] args) 
    { 
     newGame(); 



    } 

    public static void newGame() 
    { 
     do 
     { 

      playerRoll(); 
      dealerRoll(); 
      stand(); 
      checkWinner(); 
      System.out.println("Would you like to play again? (Y/N)"); 
     } while (scan.next().toLowerCase().charAt(0) == 'y'); 

    } 

    public static int takeHit() 
    { 
     return takeHit = cards.rollDice(); 
    } 

    public static int stand() 
    { 
     return playerScore; 


    } 

    public static void dealerRoll() 
    { 
     while(playGame) 
     { 
      cards.rollDice(); 
     } 

     dealerScore = (cards.rollDice()); 



       do 
       { 
        tempScore = takeHit(); 

       //System.out.println("The dice roll was: " + tempScore); 
       dealerScore += tempScore; 
       System.out.println("Dealer score is: " + dealerScore); 
       }while(dealerScore <= 15 && playerScore > dealerScore); 


    } 

    public static void playerRoll() 
    { 
     while(playGame) 
     { 
      cards.rollDice(); 
     } 

     playerScore = (cards.rollDice()); 

      System.out.println("Your total is " + playerScore); 


      System.out.println("Would you like a hit?(Y/N)"); 
      hit = scan.next().toLowerCase().charAt(0); 

      if(hit == 'y') 
       do 
       { 
        tempScore = takeHit(); 
        //System.out.println("The dice roll was: " + tempScore); 
        System.out.println("The hit gave you : " + tempScore + " Your total is now: " + (playerScore += tempScore)); 
        System.out.println("Would you like a hit?(Y/N)"); 
       }while(scan.next().toLowerCase().charAt(0) == 'y'); 



      if(hit == 'n') 
         stand(); 









    } 

    public static void checkWinner() 
    { 
     if (playerScore > 21) 
      System.out.println("Sorry you busted, Dealer has won!"); 
     else 
     if (dealerScore > 21) 
       System.out.println("Dealer has busted, You win!"); 
     else 
     if (playerScore == 21) 
      System.out.println("You win!"); 
     else 
     if (dealerScore == 21) 
      System.out.println("Sorry, Dealer has won!"); 
     else if (dealerScore > playerScore && dealerScore <= 21) 
      System.out.println("Sorry, Dealer has won!"); 
     else if (playerScore > dealer && playerScore <= 21) 
      System.out.println("You win!"); 





    }