骰子滚动游戏Java

时间:2016-03-24 00:25:58

标签: java

我试图创建一个骰子滚动游戏,用户可以在高或低的赌注上点数量(起始量= 1000点)。数字1-6为低,7为自动丢失,8-12为高。我在使用积分系统时遇到问题,以及当用户猜对错误时它是如何正确加/减的。此代码完全在单个类GUI中编写。

    int die;//dice1
    int dice;//dice2
    int diceSum;
    int points;
    int guess;
    int userBet;

    points =(1000);//amount of point user starts with

   jTextField4.setText("" + points);//setting user points



    guess = Integer.parseInt(jTextField2.getText()); //determine if user bet high or low (1 = high), (0=low)
   userBet = Integer.parseInt(jTextField3.getText());//determine amount user bet

    die = 1 + (int) ((Math.random() * (7 - 1)));//dice 1
    jLabel8.setText("" + die);//prints out dice #
    dice = 1 + (int) ((Math.random() * (7 - 1)));//dice 2
    jLabel9.setText("" + dice);//prints out dice #
    diceSum = die + dice;//total sum of dice

    {
   if ((diceSum >=8 )&&(guess == 1)) //user guesses high
    jTextField1.setText("High, " + diceSum);//dice tells user its high
    jTextField4.setText(userBet + points + "");//reward player points

    }
    {
    if ((diceSum >=8 )&&(guess ==0)) //user guesses low
    jTextField1.setText("High, " + diceSum);//dice tells user its high
    jTextField4.setText(userBet - points + ""); //subtract amount user bet
    }
    {        
   if((diceSum <= 6) && (guess ==1))//user guesses high
    jTextField1.setText("Low, " + diceSum);//dice tells user its low
    jTextField4.setText(points - userBet+"");//subtract amount user bet

    }
    {
    if((diceSum <= 6)&& (guess ==0))//user guesses low
    jTextField1.setText("Low, " + diceSum);//dice tells user its low
    jTextField4.setText(userBet + points + ""); //reward player points
    }
    {
   if (diceSum ==7)//automatic lose 
    jTextField1.setText("You Lose, " + diceSum);//automatic lose
    jTextField4.setText(points - userBet+"");//subtract points
    }

2 个答案:

答案 0 :(得分:1)

而不是jTextField4.setText(userBet - points + "");尝试做这样的事情:

points -= userBet;
JTextField4.setText(points);

当您尝试在()内执行操作时,它不会存储该值,因此当您下次执行时,点数仍为1000.

另外,为什么要从投注中减去总分数?这意味着如果你有100分并下注4,然后输了,你将得到-96分而不是96分。

答案 1 :(得分:0)

int die;//dice1
int dice;//dice2
int diceSum;
int points;
int guess;
int userBet;

points =(1000);//amount of point user starts with

jTextField4.setText("" + points);//setting user points

guess = Integer.parseInt(jTextField2.getText()); //determine if user bet high or low (1 = high), (0=low)
userBet = Integer.parseInt(jTextField3.getText());//determine amount user bet

die = 1 + (int) ((Math.random() * (7 - 1)));//dice 1
jLabel8.setText("" + die);//prints out dice #
dice = 1 + (int) ((Math.random() * (7 - 1)));//dice 2
jLabel9.setText("" + dice);//prints out dice #
diceSum = die + dice;//total sum of dice

{
if ((diceSum >=8 )&&(guess == 1)) //user guesses high
jTextField1.setText("High, " + diceSum);//dice tells user its high
jTextField4.setText(userBet + points + "");//reward player points

}
{
if ((diceSum >=8 )&&(guess ==0)) //user guesses low
jTextField1.setText("High, " + diceSum);//dice tells user its high
jTextField4.setText(points - userBet + ""); //subtract amount user bet
}
{        
if((diceSum <= 6) && (guess ==1))//user guesses high
jTextField1.setText("Low, " + diceSum);//dice tells user its low
jTextField4.setText(points - userBet+"");//subtract amount user bet

}
{
if((diceSum <= 6)&& (guess ==0))//user guesses low
jTextField1.setText("Low, " + diceSum);//dice tells user its low
jTextField4.setText(userBet + points + ""); //reward player points
}
{
if (diceSum ==7)//automatic lose 
jTextField1.setText("You Lose, " + diceSum);//automatic lose
jTextField4.setText(points - userBet+"");//subtract points
}