2014-09-04 25 views
1

我是編程新手。我試圖製造二十一點,並遇到一些問題。Java - 我正在編寫我的第一個項目。這是二十一點,但我遇到了一些麻煩

我的控制檯輸出:

Would you like to play again? y/n 
y 
You have: $50 
Whats your bet: 
12 
You get a 7 and a 10 
Your total is 17 
The dealer has a 9 11 showing 
Would you like to hit? y/n : 
n 
The dealer has 20       //Here it is supposed to say You lost blah blah blah 
Would you like to play again? y/n   //but its skipping to System.println("play again"); 

package loops; 
import java.util.Scanner; 
import java.util.Random; 
public class loops { 
    public static void main(String[] args) { 
     Scanner reader = new Scanner(System.in); 
     char hit = 0; 
     char playAgain = 0; 
     String dolphin = "banana"; 
     int c = 5; 
     int win = 0; 
     int[] cards = {2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6,7,7,7,7,8,8,8,8,9,9,9,9,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,11,11,11,11}; 
     int bet, money = 999999999, cardValue = 0, dealersValue = 0; 
     if(money == 999999999){ 
      System.out.println("How much money would you like to start with?"); 
      money = reader.nextInt(); 
     } 


     do { 
      shuffleArray(cards); 
      System.out.println("You have: $" + money); 
      System.out.println("Whats your bet: "); 
      bet = reader.nextInt(); 
      if(bet > money){ 
       System.out.println("You cannot bet over " + money + "!"); 
       System.exit(0); 
       break; 
      } 


      System.out.println("You get a " + cards[0] + " and a " + cards[1]); 
      cardValue = cards[0] + cards[1]; 
      System.out.println("Your total is " + cardValue); 

      if(cardValue == 21){ 
       win = 1; 
      } 


      if(cardValue > 21){ 
       win = 2; 
      } 


      if(win != 2){ 

       int i = 9; 
       System.out.println("The dealer has a " + cards[7] + " " + cards[8] + " showing"); 
       dealersValue = cards[7] + cards[8]; 
       System.out.println("Would you like to hit? y/n : "); 
       hit = reader.next().charAt(0); 
       while(dealersValue <= 17){ 
        if(dealersValue <= 17){ 
         dealersValue = cards[7] + cards[8] + cards[i]; 
        } 


        if(dealersValue <= 17){ 
         dealersValue = cards[7] + cards[8] + cards[i] + cards[i]; 
        } 


        if(dealersValue <= 17){ 
         dealersValue = cards[7] + cards[8] + cards[i] + cards[i] + cards[i]; 
        } 

        if(cards[i] > 11 && dealersValue > 21){ 
         cards[i] = 1; 
        } 
        if(cards[7] > 11 && dealersValue > 21){ 
         cards[7] = 1; 
        } 
        if(cards[8] > 11 && dealersValue > 21){ 
         cards[8] = 1; 
        } 

        i++; 
       }} 


      while(hit == 'y'){ 
       System.out.println("You get a " + cards[c]); 
       cardValue = cardValue + cards[c]; 
       System.out.println("Your total is " + cardValue); 
       if(cardValue > 21){ 
        if(cards[5] > 11 && cardValue > 21){ 
         cards[5] = 1; 
         System.out.println("To avoid busting, you 11 changes to 1"); 
        } 

        if(cards[6] > 11 && cardValue > 21){ 
         cards[6] = 1; 
         System.out.println("To avoid busting, you 11 changes to 1"); 
        } 

        if(cards[7] > 11 && cardValue > 21){ 
         cards[7] = 1; 
         System.out.println("To avoid busting, you 11 changes to 1"); 
        } 

        if(cards[8] > 11 && cardValue > 21){ 
         cards[8] = 1; 
         System.out.println("To avoid busting, you 11 changes to 1"); 
        } 

        if(cards[9] > 11 && cardValue > 21){ 
         cards[9] = 1; 
         System.out.println("To avoid busting, you 11 changes to 1"); 
        } 

        if(cardValue > 21){ 
         break; 
        } 
       } 
       System.out.println("Would you like to hit? y/n : "); 
       hit = reader.next().charAt(0); 
       c++; 
       if(hit =='n'){ 
        System.out.println("You stay!"); 
        cardValue = cardValue + 0; 
        break; 
       } 
      } 
      System.out.println("The dealer has " + dealersValue); 

      if(dealersValue > 21){ 
       win = 1; 
      } 
      if(cardValue > 21){ 
       win = 2; 
      } 
      if(cardValue == 21){ 
       win = 1; 
      } 
      if(dealersValue == 21){ 
       win = 2; 
      } 
      if (dealersValue > cardValue && dealersValue > 21){ 
       win = 2; 
      } 
      if (dealersValue == cardValue){ 
       win = 2; 
      } 
      if(dealersValue < cardValue && cardValue <=21) { 
       win = 1; 
      } 
      if(dealersValue == 21 && dealersValue != cardValue || dealersValue == 21 && dealersValue == cardValue || cardValue > 21){ 
       win = 2; 
      } 
      if(cardValue == 21 && dealersValue != cardValue || dealersValue > 21){ 
       win = 1; 
      } 
      if(win == 1){ 
       System.out.println("You won "+bet*1 + " dollars!");; 
       System.out.println("Your total balance is " + money); 
      } 
      if(win == 2){ 
       System.out.println("You lost "+bet*1 + " dollars!");; 
       System.out.println("Your total balance is " + money); 
      } 
      if(win == 3){ 
       System.out.println("You lost "+bet*1 + " dollars!");; 
       System.out.println("Your total balance is " + money); 
      } 
      if(money <= 0){ 
       System.out.println("You are out of money!"); 
       System.exit(0); 
      } 
      System.out.println("Would you like to play again? y/n "); 
      playAgain = reader.next().charAt(0); 

      if(playAgain == 'n'){ 
       System.exit(0); } 
      win = 0; 

     } while (playAgain == 'y'); 
    } 


    static void shuffleArray(int[] cards){ 
     Random rnd = new Random(); 
     for (int i = cards.length - 1; i > 0; i--) 
     { 
      int index = rnd.nextInt(i + 1); 
      int a = cards[index]; 
      cards[index] = cards[i]; 
      cards[i] = a; 
     } 
    } 
} 

我已經通過代碼多次梳理,尋找在那裏我錯在哪裏,像也許}它不應該的,但我找不到任何東西。我認爲它可以使用一些新鮮的眼睛! 當我得到這個代碼工作後,我打算再次嘗試使用面向對象的方法。

+0

正在使用任何IDE(Eclipse,Netbeans,...)? – 2014-09-04 00:20:08

+0

Eclipse版本4.4.0 – roke 2014-09-04 00:21:42

+0

然後在調試器下逐步運行代碼,您將能夠看到整個控制流程。 – 2014-09-04 00:22:56

回答

2

設置變量「win」的if語句集不正確。

通過給出的示例(經銷商有20個,玩家有17個),代碼變量「win」永遠不會被設置,因此所有打印出「You lost」和「You won」的語句都會被跳過。

它很可能在此行

if (dealersValue > cardValue && dealersValue > 21){ 

你的意思

dealersValue < 21 

那麼這將是取勝的原因被設置爲2,以顯示正確的輸出。

調試提示:使用IDE瀏覽代碼。如果你逐行瀏覽,你應該立即看到它跳過設置「贏」變量。

祝你好運學習編程!這很有趣,但非常令人沮喪:)

+1

謝謝!到目前爲止,我很喜歡編程,雖然我不太擅長它。此外,似乎已經修復它!我幾乎很尷尬,我錯過了這麼簡單,因爲錯誤大於/小於號 – roke 2014-09-04 00:39:19

+1

我錯過了更爲明顯的錯誤:) 讓調試器爲你工作,練習通過代碼檢查和檢查值 - 它將會非常非常有幫助。 在一行代碼中,問問自己「我期望這個值是什麼?接下來會發生什麼?」。如果你錯了,花些時間弄清楚。 – Vlad274 2014-09-04 00:44:51

相關問題