2014-11-02 56 views
-2

我試圖設計一個遊戲,玩Hi/Lo紙牌遊戲,直到用戶在連續獲得4個正確的答案,儘管theres與代碼的問題..我不知道如何一旦用戶指出「較高」,「較低」或「相等」是他們將最後'cardGenerated'數字與之比較的數字,那麼彈出卡號。如何在java中設置數字名稱

現在,它將它與用戶不知道或未知的數字進行比較,因此他們不知道他們是對還是錯。我知道我可以只添加「nextCard」可變進showOptionDialog輸出雖然我寧願只是有一個號碼被輸出,所以如果程序打印:

"The Card pulled is the 9 
Is the next card Higher, Lower or Equal?" 

輸出的下一個號/卡號是多少用戶正在比較以前的數字(9)和。

此外,

我已經設置了常量,但我不知道如何使它所以不是打印11,12,13,1,它打印JACK,後,王,ACE,和什麼不。

import java.util.Random; 
import javax.swing.JOptionPane; 

public class HiLo { 

public static final int JACK = 11; 
public static final int QUEEN = 12; 
public static final int KING = 13; 
public static final int ACE = 1; 

    public static void main(String[] args) { 

     int correctGuesses = 0; 

     Random generator = new Random(); 
     int currentCard; 
     int nextCard = generator.nextInt(KING+1); 

     while (correctGuesses < 4) 
     {   
      currentCard = nextCard; 
      nextCard = generator.nextInt(KING+1); 

      Object[] options = {"Higher", 
       "Lower", 
       "Equal"}; 
      int Input = JOptionPane.showOptionDialog(null, 
       "The Card pulled is the " + currentCard + 
       " \nis the next card Higher, Lower or Equal?", 
       "HiLo Card Game", 
      JOptionPane.YES_NO_CANCEL_OPTION, 
      JOptionPane.QUESTION_MESSAGE, 
      null, options, options[0]); 

      if (nextCard > currentCard && Input == JOptionPane.YES_OPTION) 
      { 
       correctGuesses++; 
      } 
      else if (nextCard > currentCard && Input == JOptionPane.NO_OPTION) 
      { 
       correctGuesses = 0; 
      } 

      else if (nextCard > currentCard && Input == JOptionPane.CANCEL_OPTION) 
      { 
       correctGuesses = 0; 
      } 

      else if (nextCard < currentCard && Input == JOptionPane.YES_OPTION) 
      { 
       correctGuesses = 0; 
      } 

      else if (nextCard < currentCard && Input == JOptionPane.NO_OPTION) 
      { 
       correctGuesses++; 
      } 

      else if (nextCard < currentCard && Input == JOptionPane.CANCEL_OPTION) 
      { 
       correctGuesses = 0; 
      } 

      else if (nextCard == currentCard && Input == JOptionPane.YES_OPTION) 
      { 
       correctGuesses = 0; 
      } 

      else if (nextCard == currentCard && Input == JOptionPane.NO_OPTION) 
      { 
       correctGuesses = 0; 
      } 

      else if (nextCard == currentCard && Input == JOptionPane.CANCEL_OPTION) 
      { 
       correctGuesses++; 
      }  
     } 

     JOptionPane.showMessageDialog(null, "Congratulations, You guessed correctly 4 times" 
       + "\nthe Last Card was the " + nextCard + " resart to play again"); 
    } 

} 
+1

http://stackoverflow.com/help/how-to-ask – 2014-11-02 22:30:36

+0

有寫'0'比'correctGuesses更簡單的方法 - correctGuesses' – Deltharis 2014-11-02 22:37:35

+0

我知道,它只是我想到的第一個數學方法,我知道我可以讓它糾正錯誤= 0;但我沒有改變它,因爲它不是我目前最大的問題之一 – roughosing 2014-11-02 22:39:50

回答

-1

如果你想變量不包含單個循環迭代中(和你的問題希望你能在兩次迭代使用nextCard值),你不要在循環中聲明它。每次迭代您也不需要新的生成器或選項對象。

Random generator = new Random(); 
int currentCard; 
int nextCard = generator.nextInt(KING+1); 
while (correctGuesses < 4) 
{ 
    currentCard = nextCard; 
    nextCard = generator.nextInt(KING+1); 
    ... 
} 

至於卡印刷 - 你或許應該創建一個卡枚舉,一個包含需要打印的護理相關信息(值,套件)以及被覆蓋的toString方法。寫作應該很簡單。

+0

謝謝男人,認爲這是類似的東西,雖然我試圖在循環內做到這一點我的問題:P – roughosing 2014-11-02 22:37:51

+0

@GregPenrose永遠不要將問題的核心改爲完全不同的東西,特別是在獲得答案之後!事實上,我解決您的主要問題的正確答案似乎無關緊要。 StackOverflow有一個單一問題的單一問題策略。 – Deltharis 2014-11-02 23:29:59

+0

哦對不起,我應該現在刪除問題還是恢復更改? – roughosing 2014-11-02 23:32:10

-1

嵌套,如果是凌亂。你應該簡化,以免你一遍又一遍地重新評估同樣的事情。例如,你的前三層評估nextCard > cardGenerated。如果您將其提取到自己的if,代碼將更具可讀性。你也可以用switch()代替評價(Input == JOptionPane.XX_OPTION)的另一部分:

 if(nextCard > cardGenerated) 
     { 
      switch(input) 
      { 
       case JOptionPane.YES_OPTION: 
        correctGuesses++; 
        break; 
       case JOptionPane.NO_OPTION: 
       case JOptionPane.CANCEL_OPTION: 
        correctGuesses = 0; 
        break; 
       default: 
        System.out.println("Should never happen, but default case should always be included"); 
      } 
     } 
     else if(nextCard < cardGenerated) 
     { 
      switch(input) 
      { 
       case JOptionPane.NO_OPTION: 
        correctGuesses++; 
        break; 
       case JOptionPane.YES_OPTION: 
       case JOptionPane.CANCEL_OPTION: 
        correctGuesses = 0; 
        break; 
       default: 
        System.out.println("Should never happen, but default case should always be included"); 
      } 
     } 
     else 
     { 
      switch(input) 
      { 
       case JOptionPane.CANCEL_OPTION: 
        correctGuesses++; 
        break; 
       case JOptionPane.YES_OPTION: 
       case JOptionPane.NO_OPTION: 
        correctGuesses = 0; 
        break; 
       default: 
        System.out.println("Should never happen, but default case should always be included"); 
      } 
     } 
+0

好的建議,但我知道它也是如此很大的評論,這不是一個答案 – Deltharis 2014-11-02 23:35:06

相關問題