如何在java中为名称设置数字
我正在尝试设计一个游戏来玩Hi/Lo卡游戏,直到用户连续得到4个正确答案,尽管代码有问题。。我不知道该怎么做,所以当用户声明“较高”、“较低”或“相等”时弹出的卡号就是他们将上一个“cardGenerated”号码与之进行比较的号码 现在,它将它与一个用户看不到或不知道的数字进行比较,因此他们不知道自己是对还是错。我知道我可以将“nextCard”变量添加到showOptionDialog输出中,尽管我更希望只输出一个数字,因此如果程序打印:如何在java中为名称设置数字,java,computer-science,Java,Computer Science,我正在尝试设计一个游戏来玩Hi/Lo卡游戏,直到用户连续得到4个正确答案,尽管代码有问题。。我不知道该怎么做,所以当用户声明“较高”、“较低”或“相等”时弹出的卡号就是他们将上一个“cardGenerated”号码与之进行比较的号码 现在,它将它与一个用户看不到或不知道的数字进行比较,因此他们不知道自己是对还是错。我知道我可以将“nextCard”变量添加到showOptionDialog输出中,尽管我更希望只输出一个数字,因此如果程序打印: "The Card pulled is the 9
"The Card pulled is the 9
Is the next card Higher, Lower or Equal?"
输出的下一个数字/卡是用户将上一个数字(9)与之进行比较的数字
而且
我已经设置了常数,但我不知道如何使它如此,而不是打印11、12、13、1,它打印杰克、皇后、国王、王牌等等
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" );
}
}
import java.util.Random;
导入javax.swing.JOptionPane;
公共类HiLo{
公共静态最终int插孔=11;
公共静态最终整数=12;
公共静态最终int KING=13;
公共静态最终int ACE=1;
公共静态void main(字符串[]args){
int-correctGuesses=0;
随机生成器=新随机();
int电流卡;
int-nextCard=generator.nextInt(KING+1);
而(正确猜测<4)
{
currentCard=nextCard;
nextCard=generator.nextInt(KING+1);
对象[]选项={“更高”,
“更低”,
“平等”};
int Input=JOptionPane.showOptionDialog(null,
“拔出的卡是”+当前卡+
“\n下一张牌是更高、更低还是相等?”,
“希洛纸牌游戏”,
JOptionPane.YES\u NO\u CANCEL\u选项,
JOptionPane.QUESTION\u消息,
null,选项,选项[0]);
if(nextCard>currentCard&&Input==JOptionPane.YES\u选项)
{
更正猜测++;
}
else if(nextCard>currentCard&&Input==JOptionPane.NO_选项)
{
正确猜测=0;
}
else if(nextCard>currentCard&&Input==JOptionPane.CANCEL_选项)
{
正确猜测=0;
}
else if(nextCard
如果您希望变量不包含在单循环迭代中(并且您的问题希望您在两次迭代中使用nextCard值),则不在循环中声明它。您也不需要每次迭代都使用新的生成器或选项对象
Random generator = new Random();
int currentCard;
int nextCard = generator.nextInt( KING+1 );
while (correctGuesses < 4)
{
currentCard = nextCard;
nextCard = generator.nextInt( KING+1 );
...
}
Random generator=new Random();
int电流卡;
int-nextCard=generator.nextInt(KING+1);
而(正确猜测<4)
{
currentCard=nextCard;
nextCard=generator.nextInt(KING+1);
...
}
至于卡片打印,您可能应该为卡片创建一个枚举,一个包含相关信息(值、套件)的枚举,以及覆盖的用于打印的toString方法。书写起来应该足够简单。嵌套的如果是凌乱的。你应该简化,这样你就不会一遍又一遍地重新评估同一件事。例如,前三层评估
nextCard>cardGenerated
。如果将其提取到自己的If
,代码将更具可读性。您还可以将求值的另一部分(Input==JOptionPane.XX\u选项)
替换为开关()
:
if(nextCard>card生成)
{
开关(输入)
{
case JOptionPane.YES\u选项:
更正猜测++;
打破
case JOptionPane.NO_选项:
case JOptionPane.CANCEL\u选项:
正确猜测=0;
打破
违约:
System.out.println(“不应发生,但应始终包括默认情况”);
}
}
else if(nextCard 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");
}
}