我試圖設計一個遊戲,玩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");
}
}
http://stackoverflow.com/help/how-to-ask – 2014-11-02 22:30:36
有寫'0'比'correctGuesses更簡單的方法 - correctGuesses' – Deltharis 2014-11-02 22:37:35
我知道,它只是我想到的第一個數學方法,我知道我可以讓它糾正錯誤= 0;但我沒有改變它,因爲它不是我目前最大的問題之一 – roughosing 2014-11-02 22:39:50