Итак, я пытаюсь создать простую консольную программу, которая вычисляет вероятность рисования определенной карты из колоды торговых карт. На данный момент мой код выглядит примерно так:Вычисление вероятностей в C# путем деления двух переменных
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();
}
}
}
это бросает несколько ошибок, говоря «Фракция не может быть найден». Как я могу исправить это и выполнить то, что я пытаюсь достичь?
EDIT: Хорошо, я внес несколько изменений. Теперь мой код не вызывает ошибок, но после запуска программы вместо того, чтобы сказать: «У вас есть 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();
}
}
}
Что такое фракция? Компилятор не знает. Это внешний класс или ...? – Li357
Да, вам нужно сообщить компилятору, что означает «Фракция» (или означает это). –