我是C#的初学者,之前曾使用过Python。我一直在编写这个二十一点代码,但我遇到了一些错误。我似乎无法解决这些问题,而且我认为在代码中某处隐藏括号或其他内容是因为它之前工作正常。
我已经努力尝试为自己解决这个问题,但我无法做到。掌握C#格式是Python的真正改变。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Blackjack
{
class Program
{
static void Main(string[] args)
{
// Creating array
string[] suits = { "Clubs", "Diamonds", "Hearts", "Spades" };
string[] cards = new string[13];
cards[0] = "A";
cards[1] = "2";
cards[2] = "3";
cards[3] = "4";
cards[4] = "5";
cards[5] = "6";
cards[6] = "7";
cards[7] = "8";
cards[8] = "9";
cards[9] = "10";
cards[10] = "J";
cards[11] = "Q";
cards[12] = "K";
int dealer_total = 0;
int player_total = 0;
int blackjack = 21;
// Dealing 2 player cards, then calculating and outputting card value
Random r = new Random();
for (int i = 0; i < 2; i++)
{
int suit = r.Next(0, 4);
int card = r.Next(0, 13);
string suit_name = suits[suit];
string card_name = cards[card];
string full_name = card_name + " of " + suit_name;
Console.WriteLine(full_name);
int cardValue = getcardvalue(card_name);
player_total = player_total + cardValue;
Console.WriteLine("You have a total of " + player_total); // Prints player's card value
}
bool hit = true;
while (hit)
{
Console.WriteLine("Would you like to stick or hit?");
string input = Console.ReadLine();
if (input.ToLower() == "stick")
{
hit = false;
// Dealing dealer cards
for (int i = 0; i < 2; i++)
{
int suit = r.Next(0, 4);
int card = r.Next(0, 13);
string suit_name = suits[suit];
string card_name = cards[card];
string full_name = card_name + " of " + suit_name;
int cardValue = getcardvalue(card_name);
dealer_total = dealer_total + cardValue;
Console.WriteLine("Dealer has a total of " + dealer_total);
if (dealer_total > player_total)
{
Console.WriteLine ("Dealer wins, you lose.");
}
else if ((player total) > (dealer_total))
{
Console.WriteLine("You win!");
}
else
{
Console.WriteLine ("Dealer total matches your total, draw.");
}
}
// if player chooses to hit, another card is drawn
else if (input.ToLower() == "hit")
{
int suit = r.Next(0, 4);
int card = r.Next(0, 13);
string suit_name = suits[suit];
string card_name = cards[card];
string full_name = card_name + " of " + suit_name;
Console.WriteLine(full_name);
int cardValue = getcardvalue(card_name);
player_total = player_total + cardValue;
Console.WriteLine("You have a total of " + player_total);
if (player_total > 21)
{
Console.WriteLine("You are bust; you lose.");
hit = false;
}
}
else
{
Console.WriteLine("Invalid response. Please choose either stick or hit.");
// Outputs an error message if anything else is input
}
}
}
private static int getcardvalue(string card_name)
{
int card_value = 0;
if (card_name == "A") // ie if the card is an Ace
{
card_value = 11;
}
else if (card_name == "K" || card_name == "Q" || card_name == "J") // if the card is a face card
{
card_value = 10;
}
else
{
card_value = int.Parse(card_name); //if the card is any other card, then it is worth its pip value
}
return card_value;
}
}
}
我收到以下错误:
main.cs(67,22): error CS1525: Unexpected symbol `total'
main.cs(67,27): warning CS0642: Possible mistaken empty statement
main.cs(67,27): error CS1525: Unexpected symbol `)'
main.cs(67,45): error CS1525: Unexpected symbol `)', expecting `;' or `}'
main.cs(71,5): error CS1525: Unexpected symbol `else'
main.cs(85,4): error CS1525: Unexpected symbol `else'
main.cs(110,3): error CS1525: Unexpected symbol `private'
main.cs(110,34): error CS1525: Unexpected symbol `('
答案 0 :(得分:3)
以下是没有错误的相同代码。
的变化:
第68行:player total
已更改为player_total
第77行:在if
阻止
else
的右括号
您使用的是哪种编辑器/ IDE?这些错误很容易在诸如visual studio等IDE中发现。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Blackjack
{
class Program
{
static void Main(string[] args)
{
// Creating array
string[] suits = { "Clubs", "Diamonds", "Hearts", "Spades" };
string[] cards = new string[13];
cards[0] = "A";
cards[1] = "2";
cards[2] = "3";
cards[3] = "4";
cards[4] = "5";
cards[5] = "6";
cards[6] = "7";
cards[7] = "8";
cards[8] = "9";
cards[9] = "10";
cards[10] = "J";
cards[11] = "Q";
cards[12] = "K";
int dealer_total = 0;
int player_total = 0;
int blackjack = 21;
// Dealing 2 player cards, then calculating and outputting card value
Random r = new Random();
for (int i = 0; i < 2; i++)
{
int suit = r.Next(0, 4);
int card = r.Next(0, 13);
string suit_name = suits[suit];
string card_name = cards[card];
string full_name = card_name + " of " + suit_name;
Console.WriteLine(full_name);
int cardValue = getcardvalue(card_name);
player_total = player_total + cardValue;
Console.WriteLine("You have a total of " + player_total); // Prints player's card value
}
bool hit = true;
while (hit)
{
Console.WriteLine("Would you like to stick or hit?");
string input = Console.ReadLine();
if (input.ToLower() == "stick")
{
hit = false;
// Dealing dealer cards
for (int i = 0; i < 2; i++)
{
int suit = r.Next(0, 4);
int card = r.Next(0, 13);
string suit_name = suits[suit];
string card_name = cards[card];
string full_name = card_name + " of " + suit_name;
int cardValue = getcardvalue(card_name);
dealer_total = dealer_total + cardValue;
Console.WriteLine("Dealer has a total of " + dealer_total);
if (dealer_total > player_total)
{
Console.WriteLine("Dealer wins, you lose.");
}
else if ((player_total) > (dealer_total))
{
Console.WriteLine("You win!");
}
else
{
Console.WriteLine("Dealer total matches your total, draw.");
}
}
}
// if player chooses to hit, another card is drawn
else if (input.ToLower() == "hit")
{
int suit = r.Next(0, 4);
int card = r.Next(0, 13);
string suit_name = suits[suit];
string card_name = cards[card];
string full_name = card_name + " of " + suit_name;
Console.WriteLine(full_name);
int cardValue = getcardvalue(card_name);
player_total = player_total + cardValue;
Console.WriteLine("You have a total of " + player_total);
if (player_total > 21)
{
Console.WriteLine("You are bust; you lose.");
hit = false;
}
}
else
{
Console.WriteLine("Invalid response. Please choose either stick or hit.");
// Outputs an error message if anything else is input
}
}
}
private static int getcardvalue(string card_name)
{
int card_value = 0;
if (card_name == "A") // ie if the card is an Ace
{
card_value = 11;
}
else if (card_name == "K" || card_name == "Q" || card_name == "J") // if the card is a face card
{
card_value = 10;
}
else
{
card_value = int.Parse(card_name); //if the card is any other card, then it is worth its pip value
}
return card_value;
}
}
}
答案 1 :(得分:0)
这是一个更高级别的建议我希望你不介意:不要试图强制格式化 - 你正在使用的任何开发环境(VisualStudio,其他) - 让它控制你的括号。
这就是原因 - 如果让VisualStudio进行格式化,这就是您的代码:
for (int i = 0; i < 2; i++)
{
... bunch of lines of code at this indenting level
}
else if (input.ToLower() == "hit")
...而且很明显:在括号方面有些东西被遗漏了,因为你不能用Else-If子句结束For-Loop。 可能缺少另一个括号,退出for()循环,可能在dealer_total行之后。
如果你正在进行缩进,那么很难发现很多这样的错误,因为它很容易弄乱你的缩进。例如,从您的代码:
if (input.ToLower() == "stick")
{
hit = false;
// Dealing dealer cards
for (int i = 0; i < 2; i++)
...看到问题?你没有缩进'hit = false;'从那一点开始的行。
无论您的IDE是什么,只需让它处理格式化(如果您没有IDE,至少可以获得VisualStudio社区,这是免费的。)它会为您节省很多麻烦。