gpt4 book ai didi

java - 修复java逻辑错误,用户钱无论输赢都不减或加

转载 作者:行者123 更新时间:2023-12-01 12:24:35 25 4
gpt4 key购买 nike

我想创建一个仅使用分支(if-else)、嵌套 while 循环和方法(简单函数和过程)的 java 骰子游戏。

代码运行良好,但是当涉及到结果时,确定用户有多少钱,它没有显示任何内容,它只显示默认的钱,即 100 那么如何修复它?非常感谢,我只是一个初学者,所以请不要问我的问题。这是我的代码:

public static void main(String[] args) {

Scanner in = new Scanner(System.in);
int umoney= 100;
int lw;
int roll1;
int roll2;
int bet ;
System.out.println("You have "+ umoney+" dollars");
while (umoney!=0) {
bet = getBet(in, umoney);
char user=getHighLow(in);
roll1=getRoll();
System.out.println("Die 1 rolls: "+roll1);
roll2=getRoll();
System.out.println("Die 2 rolls: "+roll2);
System.out.println("Total of two diece is: " + (roll1+roll2));
lw = determineWinnings(user, bet, (roll1+roll2));
if (lw<0) {
System.out.println("You lost!");
}
else {
System.out.println("You won "+ lw+" dollars!");
umoney=umoney+lw;
}
}
}


// Given a Scanner and a current maximum amount of money, prompt the user for
// an integer representing the number of dollars that they want to bet. This
// number must be between 0 and to maximum number of dollars. If the user enters
// a number that is out of bounds, display an error message and ask again.
// Return the bet to the calling program.
private static int getBet(Scanner inScanner, int currentPool) {
// Fill in the body
System.out.println("Enter an amount to bet (0 to quit): ");
int ubet= inScanner.nextInt();
if (ubet==0) {
System.out.println("Goodbye!");
System.exit(0);
}
while (ubet<0 || ubet > currentPool) {
System.out.println("You must enter between 0 and "+ currentPool +" dollars");
System.out.println("You have "+ currentPool+" dollars");
System.out.println("Enter an amount to bet (0 to quit): ");
ubet=inScanner.nextInt();
if (ubet==0) {
System.out.println("Goodbye!");
System.exit(0);
}
}
return ubet;
}

// Given a Scanner, prompt the user for a single character indicating whether they
// would like to bet High ('H'), Low ('L') or Sevens ('S'). Your code should accept
// either capital or lower case answers, but should display an error if the user attempts
// to enter anything but one of these 3 values and prompt for a valid answer.
// Return the character to the calling program.
private static char getHighLow(Scanner inScanner) {
// Fill in the body
System.out.println("High, Low or Seven (H/L/S): ");
char iuword= inScanner.next().charAt(0);
while (iuword!='h' && iuword!='H' && iuword!='L' && iuword!='l' && iuword!='s' && iuword!='S') {
System.out.println("Invalid commad, Please enter again!");
System.out.println("High, Low or Seven (H/L/S): ");
iuword= inScanner.next().charAt(0);
} return iuword;
}

// Produce a random roll of a single six-sided die and return that value to the calling
// program
private static int getRoll() {

int roll1 =(int)(6*Math.random())+1;
return roll1;
}

// Given the choice of high, low or sevens, the player's bet and the total result of
// the roll of the dice, determine how much the player has won. If the player loses
// the bet then winnings should be negative. If the player wins, the winnings should
// be equal to the bet if the choice is High or Low and 4 times the bet if the choice
// was Sevens. Return the winnings to the calling program.
private static int determineWinnings(char highLow, int bet, int roll) {


int result;
if (highLow=='H' || highLow=='h') {
if (roll<7) {
result =-1*bet;
}
else {
result=bet;
}
}
if (highLow=='L'|| highLow=='l') {
if (roll>7) {
result=-1*bet;
}
else {
result=bet;
}
}
if (highLow=='S' || highLow=='s') {
result =4*bet;
}
else {

result = -1*bet;
}
return result;
}

最佳答案

代码有两个问题。

  1. umoney = umoney + lw 需要位于 if-else block 之后。您想要添加胜利和失败,而不仅仅是胜利:p
  2. 您在确定获胜中错过了一个 if 条件 - highLow == 'S' 后面需要紧跟 if (roll == 7)

关于java - 修复java逻辑错误,用户钱无论输赢都不减或加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26458704/

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