简体   繁体   English

通过除以 2 个变量来计算 c# 中的概率

[英]Calculating probabilities in c# by dividing 2 variables

Okay so I'm trying to make a simple console program that calculates the chance of drawing a specific card from a deck of trading cards.好的,所以我正在尝试制作一个简单的控制台程序,用于计算从一副交易卡中抽取特定卡的机会。 At the moment my code looks something like this:目前我的代码看起来像这样:

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();
        }
    }
}

this throws several errors saying "Fraction could not be found".这会引发几个错误,说“找不到分数”。 How can I fix this and accomplish what I am trying to achieve?我该如何解决这个问题并完成我想要实现的目标?

EDIT: Okay I've made several changes.编辑:好的,我做了几处更改。 Now my code throws no errors, but after running through the program, instead of saying "You have a 1/40 chance of drawing that card on that turn assuming it has not yet been drawn" it says "You have a ConsoleApplication2.Program+Fraction chance of drawing that card on that turn assuming it has not yet been drawn" My new Code goes as follows:现在我的代码没有抛出任何错误,但是在运行程序之后,不是说“假设它还没有被抽出,你有 1/40 的机会在那个回合抽出那张牌”而是说“你有一个 ConsoleApplication2.Program+假设该牌尚未抽到,则在该回合抽到那张牌的几率很小”我的新代码如下:

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();
            }
        }
}

Fraction could not be found找不到分数

That's because of the code line (as below) where you are trying to create a instance of Fraction type.这是因为您尝试创建Fraction类型实例的代码行(如下所示)。 Do you have a class named Fraction in your project.你的项目中有一个名为Fraction的类吗? If so then you might want to import the namespace saying如果是这样,那么您可能想导入命名空间说

using ProjectNamespaceWhereFractionClassHaveBeenDefined

    Fraction frac = new Fraction();

It's just that, compiler not able to resolve the type cause it's not able to find one.只是,编译器无法解析类型,因为它无法找到类型。

You need do some modifications:你需要做一些修改:

        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();
        }

You were doing a / with int, and that can be a problem in some cases.您正在使用 int 执行 /,在某些情况下这可能是一个问题。 And you need to return the string that you've created.并且您需要返回您创建的字符串。

In the other hand, you need to write the message as drawProbability.ToString(), you were just using drawProbability, for that reason the print were: ConsoleApplication2.Program+Fraction另一方面,您需要将消息写为 drawProbability.ToString(),您只是在使用 drawProbability,因此打印为:ConsoleApplication2.Program+Fraction

I'm posting this now, 2020, because I've reached this question looking for some help for a problem that I have now... then I post here the resolution for your code, just for help other people that arrive here from now, in the future.我现在发布这个,2020 年,因为我已经解决了这个问题,正在寻找我现在遇到的问题的一些帮助......然后我在这里发布了你的代码的解决方案,只是为了帮助从现在起到达这里的其他人,在未来。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM