我被指派为我的java课程介绍做一个高或低的骰子游戏。我已完成所有方法,但是,我需要使用while循环以便我可以继续玩游戏,直到我的现金达到0或者我下注0美元。如果我不需要,我不想休息。所以我的问题是我能做什么(如果可能的话)只使用while循环?这是我的计划:
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int cash = 100;
int bet = 1
while(cash > 0 || bet != 0)
{
bet = getBet(in, cash);
char choice = getHighLow(in);
int roll1 = getRoll();
System.out.println("Dice 1 rolls: " + roll1);
int roll2 = getRoll();
System.out.println("Dice 2 rolls: " + roll2);
int total = (roll1 + roll2);
System.out.println("The total roll is: " + total);
int winnings = determineWinnings(choice, bet, total);
cash = cash + winnings;
}
System.out.println("You have " + cash + " dollars left. Goodbye!");
}
// 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) {
System.out.println("You have " + currentPool + " dollars");
System.out.println("Enter and amount to bet (0 to quit): ");
int bet = inScanner.nextInt();
while (0 > bet || currentPool < bet )
{
System.out.println("Your bet must be between 0 and " + currentPool + " dollars ");
bet = inScanner.nextInt();
}
return bet;
}
// 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 lowercase 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) {
System.out.println("High, low or sevens (H/L/S): ");
inScanner.nextLine();
char choice = inScanner.nextLine().charAt(0);
choice = Character.toUpperCase(choice);
while (choice != 'H' && choice != 'L' && choice != 'S')
{
System.out.println("You must choose between high, low or sevens (H/L/S): ");
choice = Character.toUpperCase(inScanner.nextLine().charAt(0));
}
return choice;
}
// Produce a random roll of a single six-sided die and return that value to the calling
// program
private static int getRoll() {
Random generate = new Random();
int roll = generate.nextInt(6) + 1;
return roll;
}
// 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) {
if(roll <= 6)
{
if(highLow == 'L')
{
System.out.println("You won " + bet + " dollars! ");
return bet;
}
else
System.out.println("You lose! ");
return -bet;
}
else if (roll == 7)
{
if(highLow == 'S')
{
System.out.println("You won " + (bet * 4) + " dollars! ");
return (bet * 4);
}
else
System.out.println("You lose! ");
return -bet;
}
else
{
if(highLow == 'H')
{
System.out.println("You won " + bet + " dollars! ");
return bet;
}
else
System.out.println("You lose! ");
return -bet;
}
}
}
答案 0 :(得分:1)
将while(cash > 0 || bet != 0)
更改为while(cash > 0 && bet != 0)
这是因为,如果现金等于0或者下注等于0,您想要结束游戏。如果您使用||
,那么只有当两个变量都为0时,循环才会停止。如果使用&&
,那么如果其中一个变量为0,则循环将停止。
使用&&
时,对于执行的循环,两个条件必须为true。如果一个条件为真而另一个条件为假,那么循环将停止。
使用||
时,任何一个
对于执行的循环,条件必须为true。如果两个条件都为真,则循环将运行。如果其中一个条件为假而另一个条件为真,那么它仍然会运行。如果两个条件 false ,则循环将停止。
修改强>
如果你希望程序在下注变量为0时立即结束,那么只需在bet = getBet(in, cash);
之后添加这些行:
if(bet<=0){
System.out.println("You are out of cash!");
break;
}
希望这有助于:)