這是一個簡單的骰子游戲,我很困惑爲什麼代碼在最後提示用戶兩次提問。如果我說(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
這裏下面是我運行源: 包項目3;
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;
}
}
}
}
}
}
}
如果您需要任何其他信息,請讓我知道。我真的很想知道爲什麼我的代碼是這樣做的?整個程序工作正常,但用相同的問題提示用戶兩次有點多餘。
其中的情況下被promted什麼問題兩次? – Stultuske
這是提示兩次, 你想玩胡扯嗎? (y/n) 你想玩胡扯嗎? (y/n) –
似乎你沒有提供任何輸入,這意味着鍋的價值沒有改變。 – Stultuske