这是一个简单的Craps游戏,我很困惑为什么代码在最后提示用户两次问题。如果我说(y)设置下注,它只会提示用户两次问题。 的输出 你想玩掷骰子吗? (Y / N) ÿ 您目前的现金罐是:100美元
Would you like to bet? (y/n) n
Press "ENTER" to continue...
Rolling...
Dice 1: 1
Dice 2: 1
Total: 2
You Lost... :(
Your total now is $100
Do you want to play craps? (y/n)
y
Your current cash pot is: $100
Would you like to bet? (y/n) n
Press "ENTER" to continue...
Rolling...
Dice 1: 2
Dice 2: 6
Total: 8
{Your POINT is: 8}
Rolling for the Point: 8
Press "ENTER" to continue...
Dice 1: 3
Dice 2: 2
{Total: 5}
Rolling for the Point: 8
Press "ENTER" to continue...
Dice 1: 1
Dice 2: 5
{Total: 6}
Rolling for the Point: 8
Press "ENTER" to continue...
Dice 1: 6
Dice 2: 1
{Total: 7}
You Lost...
Your total now is $100
Do you want to play craps? (y/n)
y
Your current cash pot is: $100
Would you like to bet? (y/n) y
How much would you like to bet? $5
Your bet stands at $5
Press "ENTER" to continue...
Rolling...
Dice 1: 2
Dice 2: 6
Total: 8
{Your POINT is: 8}
Rolling for the Point: 8
Press "ENTER" to continue...
Dice 1: 5
Dice 2: 4
{Total: 9}
Rolling for the Point: 8
Press "ENTER" to continue...
Dice 1: 5
Dice 2: 3
{Total: 8}
You Won!!!
Your total now is $105
Do you want to play craps? (y/n)
Do you want to play craps? (y/n)
y
Your current cash pot is: $105
Would you like to bet? (y/n) y
How much would you like to bet? $5
Your bet stands at $5
Press "ENTER" to continue...
Rolling...
Dice 1: 5
Dice 2: 6
Total: 11
You Won!! :)
Your total now is $110
Do you want to play craps? (y/n)
Do you want to play craps? (y/n)
y
Your current cash pot is: $110
Would you like to bet? (y/n) y
How much would you like to bet? $5
Your bet stands at $5
Press "ENTER" to continue...
Rolling...
Dice 1: 4
Dice 2: 5
Total: 9
{Your POINT is: 9}
Rolling for the Point: 9
Press "ENTER" to continue...
Dice 1: 6
Dice 2: 3
{Total: 9}
You Won!!!
Your total now is $115
Do you want to play craps? (y/n)
Do you want to play craps? (y/n)
y
Your current cash pot is: $115
Would you like to bet? (y/n) n
Press "ENTER" to continue...
Rolling...
Dice 1: 5
Dice 2: 5
Total: 10
{Your POINT is: 10}
Rolling for the Point: 10
Press "ENTER" to continue...
Dice 1: 5
Dice 2: 3
{Total: 8}
Rolling for the Point: 10
Press "ENTER" to continue...
Dice 1: 1
Dice 2: 1
{Total: 2}
Rolling for the Point: 10
Press "ENTER" to continue...
Dice 1: 4
Dice 2: 2
{Total: 6}
Rolling for the Point: 10
Press "ENTER" to continue...
Dice 1: 5
Dice 2: 1
{Total: 6}
Rolling for the Point: 10
Press "ENTER" to continue...
Dice 1: 2
Dice 2: 5
{Total: 7}
You Lost...
Your total now is $115
Do you want to play craps? (y/n)
n
Good bye.
----Statistics----
Won: 3
Lost: 4
Cash: $115
以下是我正在运行的来源: package project3;
import java.util.Random; // Used for generating a random number dice
import java.util.Scanner; // Used for getting input from user
/**
*
* @author Sartaj Singh
*/
public class Project3 {
/**
*
*/
public static void promptEnterKey() {
System.out.println("Press \"ENTER\" to continue...");
Scanner scanner = new Scanner(System.in);
scanner.nextLine();
}
public static void main(String[] args) {
int pot = 100;
int bet = 0;
int wins = 0;
int losses = 0;
// int games = 0;
// Make an instance of the Random and Scanner Class
Random random = new Random();
Scanner input = new Scanner(System.in);
while (pot >= 1) {
System.out.println();
System.out.println("Do you want to play craps? (y/n) ");
String play_choice = input.nextLine();
if ("N".equals(play_choice) || "n".equals(play_choice)) {
System.out.println("Good bye.");
System.out.println("----Statistics----");
System.out.println("Won: " + wins);
System.out.println("Lost: " + losses);
System.out.println("Cash: $" + pot);
break;
}
else if ("Y".equals(play_choice) || "y".equals(play_choice)) {
System.out.println("Your current cash pot is: $" + pot);
System.out.println();
System.out.print("Would you like to bet? (y/n) ");
String bet_choice = input.nextLine();
if ("y".equals(bet_choice) || "Y".equals(bet_choice)) {
System.out.print("How much would you like to bet? $");
bet = input.nextInt();
while (bet < 1 || bet > pot) {
System.out.print("YOU CANT BET THAT AMOUNT! How much would you like to bet? $");
bet = input.nextInt();
}
System.out.println("Your bet stands at $" + bet);
}
if ("n".equals(bet_choice) || "N".equals(bet_choice)) {
bet = 0;
}
promptEnterKey();
System.out.println("Rolling...");
int dice1;
dice1 = random.nextInt(6) + 1;
int dice2;
dice2 = random.nextInt(6) + 1;
int dice_total = dice1 + dice2;
System.out.println("Dice 1: " + dice1);
System.out.println("Dice 2: " + dice2);
System.out.println("Total: " + dice_total);
switch (dice_total) {
case 7:
case 11:
System.out.println("You Won!! :)");
pot += bet;
wins++;
System.out.println("Your total now is $" + pot);
break;
case 2:
case 3:
case 12:
System.out.println("You Lost... :(");
pot -= bet;
losses++;
System.out.println("Your total now is $" + pot);
break;
default:
int point = dice_total;
System.out.println("{Your POINT is: " + point + "}");
System.out.println("");
while (dice_total != 7) {
System.out.println("Rolling for the Point: " + point);
promptEnterKey();
dice1 = random.nextInt(6) + 1;
dice2 = random.nextInt(6) + 1;
dice_total = dice1 + dice2;
System.out.println("Dice 1: " + dice1);
System.out.println("Dice 2: " + dice2);
System.out.println("{Total: " + dice_total + "}");
if (dice_total == point) {
System.out.println("You Won!!!");
pot += bet;
wins++;
System.out.println("Your total now is $" + pot);
break;
} else if (dice_total == 7) {
System.out.println("You Lost...");
pot -= bet;
losses++;
System.out.println("Your total now is $" + pot);
break;
}
}
}
}
}
}
}
如果您需要任何其他信息,请告诉我。我真的想知道为什么我的代码这样做?整个程序运行正常,但用相同的问题提示用户两次有点多余。
答案 0 :(得分:1)
执行bet = input.nextInt();
后,您还应该input.nextLine();
。
以下是更改后的代码段:
if ("y".equals(bet_choice) || "Y".equals(bet_choice)) {
System.out.print("How much would you like to bet? $");
bet = input.nextInt();
while (bet < 1 || bet > pot) {
System.out.print("YOU CANT BET THAT AMOUNT! How much would you like to bet? $");
bet = input.nextInt();
}
/* Add Here */
input.nextLine();
System.out.println("Your bet stands at $" + bet);
}