-1

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:

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 Answers2

2

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. Do you have a class named Fraction in your project. 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.

Rahul
  • 76,197
  • 13
  • 71
  • 125
  • Unfortunately, I'm to much of a programing noob for this answer to help me much at the moment. I honestly don't know anything about classes. – Ian Finnigan Jun 22 '16 at 21:16
0

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. 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

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.

Fabrizio
  • 11
  • 6