简体   繁体   English

根据游戏的输赢增加和减少“金额”

[英]Increase and decrease “money amount” based on win or loss of game

I have made a dice roll game and i'm trying to increase your "cash" by whatever the inputted bet is when you win and remove it when you lose.我做了一个掷骰子游戏,我试图在你赢的时候通过输入的赌注来增加你的“现金”,在你输的时候把它移除。

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

public class Joption10 {
    public static void main(String[] args) {

        Random randomNumber = new Random();

        //Variables
        String name, bet;
        int num1, num2;
        int cash = 100;
        int convertbet;

       

        name = JOptionPane.showInputDialog(null, "Enter Your First Name");
        JOptionPane.showMessageDialog(null, "Greetings " + name + ", welcome to roll the dice!" +"\n\nYou will start with " + cash + " euros in your wallet!" + "\n\nThe game ends when you are broke, or when you have doubled your money to 200 euros." + "\n\nGood Luck!");

        while (cash > 0 && cash < 200) {
            //Generate random numbers for player and dealer
            num1 = randomNumber.nextInt(10) + 1; //player
            num2 = randomNumber.nextInt(10) + 1; //dealer

            bet = JOptionPane.showInputDialog(null, "Place your bet (1 - 100): ");
            convertbet = Integer.parseInt(bet);
            //Rolling
            JOptionPane.showMessageDialog(null, "Rolling the dice...");
            
            if (num2 > num1) {
                JOptionPane.showMessageDialog(null, "You Win!" + "\nThe Dealer rolled a " + num1 + "\n" + name + " rolled a "  + num2);
                cash + 10
            } else if (num2 < num1) {
                JOptionPane.showMessageDialog(null, "You Lose!" + "\nThe Dealer rolled a " + num1 + "\n" + name + " rolled a " + num2);
            } else {
                JOptionPane.showMessageDialog(null, "No Winner!" + "\nThe Dealer rolled a " + num1 + "\n" + name + " rolled a " + num2);
            }

            JOptionPane.showMessageDialog(null, "You have " + cash + " euros left...");
        }

        JOptionPane.showMessageDialog(null, "You have won games!");

        System.exit(0);
    }//Close Main
}//Close Class

If I understand it you should add the convertbet amount when you win and substract it when you loose to the cash.如果我理解的话,你应该在赢钱时加上兑换金额,在你输钱时减去它。

if (num2 > num1) {
    JOptionPane.showMessageDialog(null, "You Win!" + "\nThe Dealer rolled a " + num1 + "\n" + name + " rolled a "  + num2);
    cash += convertbet
} else if (num2 < num1) {
    JOptionPane.showMessageDialog(null, "You Lose!" + "\nThe Dealer rolled a " + num1 + "\n" + name + " rolled a " + num2);
    cash -= convertbet
} else {
    JOptionPane.showMessageDialog(null, "No Winner!" + "\nThe Dealer rolled a " + num1 + "\n" + name + " rolled a " + num2);
}

Watch out you where adding 10 to cash but assigning the returned value nowhere.当心你将 10 添加到现金但无处分配返回值的地方。 You can use += operator to add and also assign the value to the variable.您可以使用+=运算符添加并将值分配给变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM