基本上,我正在用Java创建一个基于文本的简单赌场程序。我一直遇到的问题是,每当我的betMoney变量从我的现金变量中减去时,它总是错误地完成。看一下游戏机:
您好,欢迎来到拉斯维加斯!选择从0美元到10000美元的金额开始(忽略逗号)
50
好的,现在选择你想玩的游戏:
1)ROULETTE
2)BLACK JACK
1
玩轮盘赌的时间!
您想下注多少钱?
50
选择您想要下注的内容
1)颜色
2)NUMBER RANGE
3)具体数字
1
你想打赌什么颜色?
1)RED(48.5%几率)
2)黑人(48.5%几率)
3)绿色(3%几率)
1
你赌错了颜色!你现在有$ -50.0
你还在银行里有钱!继续玩?
这是我的代码片段,当玩家想要在轮盘赌中投注颜色时运行:
if (c == 1){
if ((color % 2) == 0 && color != 0){
cash += betMoney;
pr("You bet on the right color! You now have $"+ cash);
}
else{
cash -= betMoney;
pr("You bet on the wrong color! You now have $"+ cash);
}
我对程序中的这个错误感到非常困惑。我似乎无法发现任何错误。玩家输入他们的起始资金,选择他们想要下注多少钱,以及他们想要下注什么。如果他们输了,他们就会失去他们打赌的钱,并且从他们开始的那么多钱中扣除;就这么简单吧?我想不适合我。我遇到的另一个问题是,无论何时现金,我的计划都不会停止。我现在已经进入负值现金状态,我的程序没有拿起,即使它包含在while循环中,只有现金> 0:
while (cash > 0){
if (i == 0){
if (q == 1){
roulette();
}
else{
blackjack();
}
}
else{
pr("You still got money in the banks! Continue playing?");
String choice = scan.next();
if(choice.equalsIgnoreCase("yes")){
pr("What game would you like to play?");
pr("1) ROULETTE\n2) BLACK JACK");
q = scan.nextInt();
while(q != 1 && q != 2){
pr("Please pick a valid number");
pr("1) ROULETTE\n2) BLACK JACK");
q = scan.nextInt();
}
if(q == 1){
roulette();
}
else{
blackjack();
}
}
else if(choice.equalsIgnoreCase("whip")){
pr("NOW WATCH ME NAE NAE, OK");
cash *= 10;
}
else{
break;
}
}
i++;
}
pr("You ended your streak with $"+ cash +".");
}
这是我的完整代码:
public static Scanner scan = new Scanner(System.in);
public static Random rand = new Random();
public static void pr(String q){
System.out.println(q);
}
public static int i = 0;
public static double cash;
public static void main(String[] args) {
pr("Hello and welcome to Vegas! Choose an amount of money from $0 to $10000 to begin (disregard commas)");
double cash = scan.nextDouble();
pr("Alright, now select the game you would like play:");
pr("1) ROULETTE\n2) BLACK JACK");
int q = scan.nextInt();
//Ensures that the player chooses a valid option//
while(q != 1 && q != 2){
pr("Please pick a valid number");
pr("1) ROULETTE\n2) BLACK JACK");
q = scan.nextInt();
}
while (cash > 0){
if (i == 0){
if (q == 1){
roulette();
}
else{
blackjack();
}
}
else{
pr("You still got money in the banks! Continue playing?");
String choice = scan.next();
if(choice.equalsIgnoreCase("yes")){
pr("What game would you like to play?");
pr("1) ROULETTE\n2) BLACK JACK");
q = scan.nextInt();
while(q != 1 && q != 2){
pr("Please pick a valid number");
pr("1) ROULETTE\n2) BLACK JACK");
q = scan.nextInt();
}
if(q == 1){
roulette();
}
else{
blackjack();
}
}
else if(choice.equalsIgnoreCase("whip")){
pr("NOW WATCH ME NAE NAE, OK");
cash *= 10;
}
else{
break;
}
}
i++;
}
pr("You ended your streak with $"+ cash +".");
}
public static void roulette(){
int color = rand.nextInt(37);
pr("Time to play some roulette boi!");
pr("How much money would you like to bet?");
double betMoney = scan.nextDouble();
pr("Choose what you would like to bet on");
pr("1) COLOR\n2) NUMBER RANGE\n3) SPECIFIC NUMBER");
int q = scan.nextInt();
while(q != 1 && q != 2 && q !=3){
pr("Please pick a valid number");
pr("1) COLOR\n2) NUMBER RANGE\n3) SPECIFIC NUMBER");
q = scan.nextInt();
}
if (q ==1){
pr("What color would you like to bet on? ");
pr("1) RED (48.5% chance)\n2) BLACK (48.5% chance)\n3) GREEN (3% chance)");
int c = scan.nextInt();
while(c != 1 && c != 2 && c !=3){
pr("Please pick a valid number");
pr("1) RED (48.5% chance)\n2) BLACK (48.5% chance)\n3) GREEN (3% chance)");
c = scan.nextInt();
}
if (c == 1){
if ((color % 2) == 0 && color != 0){
cash += betMoney;
pr("You bet on the right color! You now have $"+ cash);
}
else{
cash -= betMoney;
pr("You bet on the wrong color! You now have $"+ cash);
}
}
else if (c == 2){
if((color % 2) == 1 && color != 0){
cash += betMoney;
pr("You bet on the right color! You now have $"+ cash);
}
else{
cash -= betMoney;
pr("You bet on the wrong color! You now have $" + cash);
}
}
else{
if (color == 0){
cash += (betMoney * 36);
pr("You bet on the correct color! You now have $" + cash);
}
else{
cash -= (betMoney);
pr("You bet on the wrong color! You now have $" + cash);
}
}
}
if (q ==2){
pr("What number range would you like to bet on?");
pr("1) 0-12 (33% chance)\n2) 13-24 (33% chance)\n3) 25-36 (33% chance)");
int c = scan.nextInt();
while(c != 1 && c != 2 && c !=3){
pr("Please pick a valid number");
pr("1) 0-12 (33% chance)\n2) 13-24 (33% chance)\n3) 25-36 (33% chance)");
c = scan.nextInt();
}
if (c == 1){
if (color >= 0 && color <= 12){
cash += (betMoney * 2);
pr("You bet on the right number range! The number was " + color + ". You now have $"+ cash);
}
else{
cash -= betMoney;
pr("You bet on the wrong number range! The number was " + color + ". You now have $" + cash);
}
}
else if (c == 2){
if (color >= 13 && color <= 24){
cash += betMoney * 2;
pr("You bet on the right number range! The number was " + color +". You now have $" + cash);
}
else{
cash -= betMoney;
pr("You bet on the wrong number range! The number was " + color +". You now have $" + cash);
}
}
else if (c == 3){
if (color >= 26 && color <= 36){
cash += betMoney * 2;
pr("You bet on the right number range! The number was " + color +". You now have $" + cash);
}
else {
cash -= betMoney;
pr("You bet on the wrong number range! The number was " + color + ". You now have $" + cash);
}
}
}
else if (q ==3){
pr("What number would you like to bet on? (Any integer from 0 - 36)");
int c = scan.nextInt();
while (c < 0 && c > 36){
pr("Please input a valid number from 0 to 36 (inclusive)");
c = scan.nextInt();
}
if (color == c){
cash += betMoney * 36;
pr("The number is " + color + "!. You win $"+ betMoney * 36 +" and now have $"+ cash);
}
else{
cash -= betMoney;
pr("The number was "+ color + " not " + c +". You lose $" + betMoney +" and now have $"+ cash);
}
}
}
public static void blackjack(){
}
}
答案 0 :(得分:0)
您正在创建一个名为cash的新变量。使用全局的。
变化:
double cash = scan.nextDouble();
到
cash = scan.nextDouble();