gpt4 book ai didi

java - 如果 while 循环重复,则尝试保持新的期末余额

转载 作者:行者123 更新时间:2023-11-30 07:58:18 25 4
gpt4 key购买 nike

我必须创建一个游戏,提示用户输入初始余额,然后下注。然后他们赢了或输了,他们的余额相应地增加或减少。我的问题是,如果玩家想再次玩游戏,我不知道如何让程序在 while 循环中重新开始之前记住最终余额。当提示用户时,我的余额保持为最初输入的余额。如果这是重复,我很抱歉,但我找不到这样的问题,而且我已经搜索了一个多小时。提前致谢。

import static java.lang.Math.*;
import java.util.Scanner;
public class BetGame
{
public static void main(String [] args)
{
Scanner in = new Scanner(System.in);

int die1, die2;

boolean looping = true;

System.out.println("Please enter your starting balance in whole dollars:");
double balance = in.nextDouble();
System.out.println("Your beginning balance is " + balance + ", good luck!!");
int guess = 0;
double bet2 = 0;
double endingBalance = 0;
while(looping) {
while (guess < 3) {//to allow the user to do this only 3 times
System.out.println("Enter a valid bet:");
double bet = in.nextDouble();
if (bet >= balance || bet >= endingBalance){
System.out.println("That is more than your balance.");
}
else if (bet < balance || bet < endingBalance) {
bet2 = 0 + bet;
break;
}
guess++;
if (guess == 3) {
looping = false;
System.out.println("You have entered three invalid bets in a row. Please leave the casino.");
}
}

die1 = RollDie();
die2 = RollDie();
int sum = die1 + die2;

if (2 <= sum && sum <= 6) {
System.out.println("The roll is " + die1 + " and " + die2 + " for a " + sum + " for a win!");
endingBalance = balance + bet2;
}
else if (7 <= sum && sum <= 12) {
System.out.println("The roll is " + die1 + " and " + die2 + " for a " + sum + " for a lose!");
endingBalance = balance - bet2;
}
System.out.println("Your balance is " + endingBalance);
System.out.println("Do you want to roll again?");
String answer = in.next();
if ((answer.equals("n")) || (answer.equals("N"))) {
looping = false;
}
}
System.out.println("Your balance is " + endingBalance + ". Better luck next time! Have a wonderful evening!");
}

static int RollDie()
{
int min = 1;
int max = 6;

return myRandomInteger(min, max);
}

static int myRandomInteger(int min, int max)
{
double range = max - min + 1.0;
int randomNum = (int)(range * Math.random()) + min;

return randomNum;
}

}

最佳答案

您可以将余额存储在变量中

    double balance = in.nextDouble();
double originalBalance = balance;

关于java - 如果 while 循环重复,则尝试保持新的期末余额,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40578115/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com