【发布时间】:2018-09-13 01:35:36
【问题描述】:
所以我正在制作一个二十一点程序。除了一个错误类型的东西外,我几乎完全成功地制作了游戏。用户可以像通常在二十一点中那样选择“命中”或“停留”,但是当它最终告诉他们他们的总数时,即使他们说停留两次,它也会添加 2 个“命中”。例如,如果我得到一个 4 和一个 6,总共 10。然后我只停留两次以保持 10。无论如何,程序都会再滚动 2 个数字,最后会说总数是 20,而不是我最初的 10得到。如果需要,您可以运行我的程序以查看更多信息,这里是代码;
/////////////////////////////////////
// Name: Mackenzie Cutler
// Class: CP 12
// Date: March 28th, 2018
/////////////////////////////////////
import java.util.Scanner;
import java.util.Random;
public class MCproject3
{
public static void main(String[] args)
{
Scanner k = new Scanner(System.in);
Random ran = new Random();
//Welcoming user & choosing their initial cards
System.out.println("Welcome to the Cutler Casino Program. Currently playing Blackjack!");
int a1 = ran.nextInt(11) + 1;
int a2 = ran.nextInt(10) + 1;
int a3 = ran.nextInt(11) + 1;
int a4 = ran.nextInt(11) + 1;
System.out.println ("\nYou get a " + a1 + " and a " + a2);
System.out.println ("Your total is " + (a1+a2));
//Choosing dealers initial cards and telling user
int b1 = ran.nextInt(11) + 1;
int b2 = ran.nextInt(10) + 1;
int b3 = ran.nextInt(11) + 1;
int b4 = ran.nextInt(11) + 1;
System.out.println("\nThe dealer has a " + b1 + " showing, and a hidden card.");
System.out.println("His total is hidden, too.");
//User chooses to 'Hit' or 'Stay'
System.out.print("\nWould you like to 'Hit' or 'Stay'?");
String choice = k.nextLine();
if(choice.equalsIgnoreCase ("hit"))
{
System.out.println("You drew a " + a3);
System.out.println("Your total is " + (a1+a2+a3));
if(a1+a2+a3 > 21)
{
System.out.println("You busted! Since you exceeded 21 the dealer wins, sorry.");
return;
}
}
else if(choice.equalsIgnoreCase ("stay"))
{
System.out.println(" ");
}
else
{
System.out.println("Error. Make sure you typed either 'Stay' or 'Hit'. Please re-run the program :)");
}
//Second time user chooses to 'Hit' or 'Stay'
System.out.print("\nWould you like to 'Hit' or 'Stay'?");
String choice2 = k.nextLine();
if(choice2.equalsIgnoreCase ("hit"))
{
System.out.println("You drew a " + a4);
System.out.println("Your total is " + (a1+a2+a3+a4));
if(a1+a2+a3+a4 > 21)
{
System.out.println("You busted! Since you exceeded 21 the dealer wins, sorry.");
return;
}
}
else if(choice2.equalsIgnoreCase ("stay"))
{
System.out.println(" ");
}
else
{
System.out.println("Error. Make sure you typed either 'Stay' or 'Hit'. Please re-run the program :)");
}
//Dealers reveal and is his turn to choose 'Hit' and 'Stay'
System.out.println("\nOkay, Dealers turn.");
System.out.println("His hidden card was " + b2);
System.out.println("His total was " + (b1+b2));
int dchoice = ran.nextInt(2) + 1;
if(dchoice == 1)
{
System.out.println("\nDealder chooses to hit.");
System.out.println("He draws a " + b3);
System.out.println("His total is now " + (b1+b2+b3));
if(b1+b2+b3 > 21)
{
System.out.println("Dealer busted! Since he exceeded 21 you WIN!!");
return;
}
}
else if(dchoice == 2)
{
System.out.println("\nDealer chooses to stay.");
}
else
{
System.out.println("Error 404. Program Failed, We are sorry. Please restart.");
}
//Dealers second 'Hit' or 'Stay' random choice
int dchoice2 = ran.nextInt(2) + 1;
if(dchoice2 == 1)
{
System.out.println("\nDealder chooses to hit.");
System.out.println("He draws a " + b4);
System.out.println("His total is now " + (b1+b2+b3+b4));
if(b1+b2+b3+b4 > 21)
{
System.out.println("Dealer busted! Since he exceeded 21 you WIN!!");
return;
}
}
else if(dchoice == 2)
{
System.out.println("\nDealer chooses to stay.");
}
else
{
System.out.println(" ");
}
//Ending
int totala = (a1+a2+a3+a4);
int totalb = (b1+b2+b3+b4);
System.out.println("\nDealers total is " + (b1+b2+b3+b4));
System.out.println("Your total is " + (a1+a2+a3+a4));
if(totala > totalb)
{
if(totala <= 21)
{
System.out.println("\nYou WIN!");
}
else if(totala > 21)
{
System.out.println("\nYou busted so you wont win :(");
}
}
else if(totala < totalb)
{
if(totalb <= 21)
{
System.out.println("\nSorry, Dealer Wins.");
}
else if(totalb > 21)
{
System.out.println("Dealer busted so you win!");
}
}
else
{
System.out.println("\nError 405. Program Failed, We are sorry. Please restart.");
}
}
}
我只是想知道你是否认为我做错了什么或者应该做不同的事情以使其正常工作。
【问题讨论】: