我想创建一个只使用分支(if-else),嵌套while循环和Method(简单函数和过程)的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;
}
答案 0 :(得分:1)
代码存在两个问题。
umoney = umoney + lw
需要在if-else块之后。你想增加胜负,而不仅仅是胜利:p highLow == 'S'
需要跟if (roll == 7)