【发布时间】:2015-08-18 05:05:08
【问题描述】:
基本上,我正在使用 Java 创建一个基于文本的简单赌场程序。我一直遇到的问题是,每当我的现金变量中减去我的 betMoney 变量时,它总是做错了。看看游戏机:
您好,欢迎来到维加斯!选择从 $0 到 $10000 的金额开始(忽略逗号)
50
好的,现在选择你想玩的游戏:
1) 轮盘赌
2) 黑杰克
1
是时候玩轮盘赌了!
你想赌多少钱?
50
选择您想投注的项目
1) 颜色
2) 数字范围
3) 具体数字
1
您想赌什么颜色?
1) 红色(48.5% 几率)
2) 黑色(48.5% 几率)
3) 绿色(3% 几率)
1
你赌错了颜色!你现在有 $-50.0
你在银行里还有钱!继续玩吗?
这是我的代码的 sn-p,当玩家想在轮盘赌中下注颜色时运行:
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);
}
我对我的程序中的这个错误完全感到困惑。我似乎看不出有什么问题。玩家输入他们的起始资金,选择他们想要下注的金额,以及他们想要下注的内容。如果他们输了,他们就输掉了他们下注的钱,并且从他们开始时的金额中减去;就这么简单,对吧?我想不适合我。我一直面临的另一个问题是,只要现金 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(){
}
}
【问题讨论】:
-
你初始化你的可变现金了吗?我需要更多代码来理解您的问题
-
很难说。现金在哪里申报?它在哪里初始化?您的控制台脚本没有显示它正在输入。某处可能有第二个
cash变量吗?请提供更多代码。请正确缩进(在复制到您的问题之前使用 IDE 的“格式”选项)。
标签: java