C#Casino Program Homework

时间:2010-10-31 22:57:06

标签: c#

下面列出的是我的程序。我应该拿两张经销商和玩家的牌,并评估谁拥有更高的牌。玩家在该赌注下投注一定数量的现金,当然,如果他赢了他赢了赌注,如果他输了,他就会丢失现金。我似乎正确地遵循了伪代码,但我一直收到错误。有人想引导我做错的事吗?请谢谢你的回复。错误后面是代码。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Casino_War
{
    class Program
    {
        static void Main(string[] args)
        {

            string playerName;
            double cash, betAmount; 
            int player, dealer;

            Console.Write("Please Enter Player's Name: ");
            playerName = Console.ReadLine();


            Console.WriteLine("Please Enter {0}'s Cash Amount", playerName);
            cash = double.Parse(Console.ReadLine());

         Boolean more = "yes";

            do
         {
             betAmount = 0;
                Console.WriteLine("Please Enter The Bet Amount: ");
                betAmount = double.Parse(Console.ReadLine());

                if (betAmount > cash)
                {
                    Console.WriteLine("You Do Not Have Enough Cash For That Bet Amount.");
                    Console.WriteLine("Cash: {0:C}", cash);
                }

         while (betAmount > cash)
         {

          Console.WriteLine("Press Enter To Draw A Card.");
          Console.ReadKey();

          Random r = new Random();
                player = r.Next(1, 15);
          DisplayCard(player, "Player");

                Console.WriteLine("Press Enter To Draw A Dealer Card.");
          Console.ReadKey();

          Random r = new Random();
                dealer = r.Next(1, 15);
          DisplayCard(dealer, "Dealer");
            }



        private static void EvaluateCash(ref double betAmount, double cash, Boolean winner, string playerName)
                {
                    if (winner)
                    {
                        Console.WriteLine("{0} You Won {1:C}!", playerName, betAmount);
                        cash = betAmount + cash;
                    }

                    else 
                    {
                        Console.WriteLine("{0} You Lose {1:C}!", playerName, betAmount);
                        cash = cash - betAmount;
                    }

                    Console.WriteLine("You Now Have {0:C}", cash);
                }

        private static Boolean PlayAgain(double cash, double betAmount, string playerName, Boolean more)
            {
                if (cash > 0)
                {
                    Console.WriteLine("You Have {0} Cash In Hand.", cash);
                    Console.WriteLine("\nPlay Again (Y/N)? ");
                    more = Console.ReadLine().ToUpper();
                    Console.WriteLine("\n\n");
                } 
                while (more == "Y");
                    Console.WriteLine("Thanks For Playing!");
            }

            public static void DisplayCard(int card)
            {
                int card = r.Next(2, 14);
                if (card == 14)
                    Console.WriteLine("Ace");
                else if (card == 13)
                    Console.WriteLine("King");
                else if (card == 12)
                    Console.WriteLine("Queen");
                else if (card == 11)
                    Console.WriteLine("Jack");
                else if (card >= 2 && card <= 10)
                    return card;
                else
                    Console.WriteLine("Error: {0} Is Not Valid.", card);
                return card;

            }

        static void Evaluate(int player, int dealer)
        {
            if (player > dealer)
                winner = true;
                    else if (dealer > player)
                        winner = false;
                            else
                            Console.WriteLine("Tie. We Go To WAR!");
                            Console.WriteLine("Press Enter To Draw A Card.");
                            Console.ReadKey();

                      Random r = new Random();
                            player = r.Next(1, 15);
                      DisplayCard(player, "Player");

                            Console.WriteLine("Press Enter To Draw A Dealer Card.");
                      Console.ReadKey();

                      Random r = new Random();
                            dealer = r.Next(1, 15);
                      DisplayCard(dealer, "Dealer");

                            return Evaluate(player, dealer);

        }
    }
}

enter image description here

1 个答案:

答案 0 :(得分:4)

我将您的代码复制并粘贴到Visual Studio中。

在您担心{括号语法错误之前,您需要考虑并修复:

  • 布尔更多=“是”; (没有意义)
  • 随机r = new Random(); (已经定义)
  • 在DisplayCard()中返回卡,因为它是一个无效函数
  • 在Evaluate()
  • 中定义获胜者