通过划分2个变量来计算c#中的概率

时间:2016-06-22 02:36:47

标签: c# probability division fractions integer-division

好的,所以我试图制作一个简单的控制台程序,计算从一副交易卡中抽取特定卡的机会。目前我的代码看起来像这样:

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

namespace ConsoleApplication2
{
    class Program
    {
        static string cardOrHand = "null";
        static int cardsInDeck = 40;


        static void Main(string[] args)
        {
            Console.WriteLine("Would you like to calculate card or hand probabilities? (card or hand)");
            cardOrHand = (Console.ReadLine());
            Console.Clear();
            if (cardOrHand == "card")
            {
                cardProbFunction();
            }
        }

        static void cardProbFunction()
        {

        Fraction frac = new Fraction();

            string cardsInDeckInput = "40";
            string quantCardInQuestionInput = "0";
            string turnToDrawInput = "1";
            int turnToDraw = 1;

            int quantCardInQuestion = 0;
            int drawProbability = 0;

            Console.WriteLine("How many cards are in the deck in question?");
            cardsInDeckInput = (Console.ReadLine());
            cardsInDeck = Int32.Parse(cardsInDeckInput);
            Console.Clear();

            Console.WriteLine("How many copies of the card in question?");
            quantCardInQuestionInput = (Console.ReadLine());
            Console.Clear();

            quantCardInQuestion = Int32.Parse(quantCardInQuestionInput);

            Console.WriteLine("On what turn do you wish to draw this card?");
            turnToDrawInput = (Console.ReadLine());
            turnToDraw = Int32.Parse(turnToDrawInput);
            turnToDraw += -1;
            Console.Clear();

            cardsInDeck += -turnToDraw;
            frac=new Fraction (quantCardInQuestion,cardsInDeck);

            Console.WriteLine("You have a " + drawProbability + " chance of drawing that card on that turn, assuming it has not yet been drawn.");
            Console.ReadLine();
        }
    }
}

这引发了一些错误,说" Fraction无法找到"。我该如何解决这个问题并完成我想要实现的目标?

编辑:好的,我做了几处修改。现在我的代码没有抛出任何错误,但是在完成程序之后,而不是说"你有1/40的机会在该转弯处绘制该卡,假设尚未绘制"它说"你有一个ConsoleApplication2.Program + Fraction机会在该转弯处绘制该卡,假设它尚未被绘制" 我的新守则如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ConsoleApplication2;

namespace ConsoleApplication2
{
    class Program
    {
        public class Fraction
        {
            private int numerator;
            private int denominator;

            public Fraction(int numerator, int denominator){
                this.numerator = numerator;
                this.denominator = denominator;
            }

            public int Numerator
            {
                get { return this.numerator; }
                set { this.numerator = value; }
            }

            public int Denominator
            {
                get { return this.denominator; }
                set
                {
                    if (value == 0)
                    {
                        throw new Exception("0 denominator");
                    }

                    this.denominator = value;
                }
            }

            public override string ToString()
            {
                StringBuilder sb = new StringBuilder();
                sb.Append(this.Numerator + "/" + this.numerator);
                sb.Append(" or ");
                sb.Append(this.Numerator / this.Denominator);

                return base.ToString();
            }
        }
            static string cardOrHand = "null";
            static int cardsInDeck = 40;

            static void Main(string[] args)
            {
                Console.WriteLine("Would you like to calculate card or hand probabilities? (card or hand)");
                cardOrHand = (Console.ReadLine());
                Console.Clear();
                if (cardOrHand == "card")
                {
                    cardProbFunction();
                }
            }
            static void cardProbFunction()
            {


                string cardsInDeckInput = "40";
                string quantCardInQuestionInput = "0";
                string turnToDrawInput = "1";
                int turnToDraw = 1;

                int quantCardInQuestion = 0;

                Console.WriteLine("How many cards are in the deck in question?");
                cardsInDeckInput = (Console.ReadLine());
                cardsInDeck = Int32.Parse(cardsInDeckInput);
                Console.Clear();

                Console.WriteLine("How many copies of the card in question?");
                quantCardInQuestionInput = (Console.ReadLine());
                Console.Clear();

                quantCardInQuestion = Int32.Parse(quantCardInQuestionInput);

                Console.WriteLine("On what turn do you wish to draw this card?");
                turnToDrawInput = (Console.ReadLine());
                turnToDraw = Int32.Parse(turnToDrawInput);
                turnToDraw += -1;
                Console.Clear();

                cardsInDeck += -turnToDraw;
                Fraction drawProbability = new Fraction(quantCardInQuestion, cardsInDeck);

                Console.WriteLine("You have a " + drawProbability + " chance of drawing that card on that turn, assuming it has not yet been drawn.");
                Console.ReadLine();
            }
        }
}

2 个答案:

答案 0 :(得分:2)

  

找不到分数

这是因为您尝试创建Fraction类型的实例的代码行(如下所示)。您的项目中是否有一个名为Fraction的类。如果是这样,那么您可能想要导入名称空间

using ProjectNamespaceWhereFractionClassHaveBeenDefined

    Fraction frac = new Fraction();

就是这样,编译器无法解析该类型导致它无法找到它。

答案 1 :(得分:0)

您需要进行一些修改:

        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(this.numerator + "/" + this.denominator);
            sb.Append(" or ");
            sb.Append((decimal)this.numerator/(decimal)this.denominator);

            return sb.ToString();
        }

您正在使用int做一个/,在某些情况下这可能是个问题。然后,您需要返回创建的字符串。

另一方面,您需要将消息写为drawProbability.ToString(),您只是在使用drawProbability,因此打印的内容为:ConsoleApplication2.Program + Fraction

我现在发布此问题,即2020年,因为我遇到了这个问题,希望为我现在遇到的问题寻求帮助...然后,我在此处发布您的代码的解决方案,以帮助其他到达的人从现在开始,在未来。