これで、トレーディングカードのデッキから特定のカードを引き出すチャンスを計算する単純なコンソールプログラムを作ろうとしています。現時点では、私のコードは次のようになります。2つの変数を除算して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();
}
}
}
「分数が見つかりませんでした」というエラーが表示されます。これを修正し、達成しようとしていることを達成するにはどうすればよいですか?
編集:はい、いくつかの変更を加えました。今度は私のコードではエラーは発生しませんが、プログラムを実行した後、「あなたはまだそのカードが描かれていないと仮定してそのカードを描く確率は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
はい、 'Fraction'が何であるか(またはそれを意味する)、コンパイラに指示する必要があります。 –