我正在努力学习C#并做一些问题我googeld。这是要做的任务:
*“初级: 任务是做 骰子游戏,用户投掷3 每个12面骰子(数字 应随机选择并存储在数组/字段或列表中)。 添加骰子的总数并在屏幕上显示。 创建一个接受图形的函数/方法 (骰子的总和)。功能/方法 如果数字应该返回文本“Good throw” 高于或等于20。 在所有其他情况下,文本 “抱歉”被退回。 在main方法中调用函数/方法 并打印总数和文本。
高级水平: 这是任务的扩展,您必须使用类来模拟骰子。用户可以选择自己编写x侧面骰子。 如果掷骰子的总和产生的分数>最高得分的50%,显示“好扔”字样。 这个逻辑可以在你的主要方法中。 创建类图中描述的类并使用适当的类 你的代码中的方式。“*
问题是我无法让它工作,我班上的数组不会保存我的数字我输入...我只得到了重新开始0.我想我刚做了一些很大的错误,我无法看到。 ..
这是主要代码:
static void Main(string[] args)
{
List<Dice> _Dice = new List<Dice>();
int a = 0;
int times = int.Parse(Interaction.InputBox("Write how many times you want to repeat the game:"));
while (a != times)
{
int antThrow = int.Parse(Interaction.InputBox("Write how many times you want each dice to get thrown:"));
int xChoice = int.Parse(Interaction.InputBox("Write how many dice you want to throw:"));
int yChoice = int.Parse(Interaction.InputBox("Write how many sides you want each dice should have:"));
_Dice.Add(new Dice(xChoice,yChoice, antThrow));
a++;
}
int e = 1;
foreach (var item in _Dice)
{
Interaction.MsgBox(string.Format("Result of game {0}: {1}", e++, item.Tostring()));
}
}
这是Dice类:
class Dice
{
static int _xChoice, _yChoice, _throw;
static List<int> sum = new List<int>();
static int w = 0;
static int _sum;
static int[,] dice = new int[_xChoice, _yChoice];
public string Tostring()
{
int half = _sum / 2;
if (half <= _sum/2)
{
return "Good throw!" + _sum;
}
else
{
return "Bad throw!";
}
}
void random()
{
Random rnd = new Random();
while (w != _throw)
{
for (int i = 0; i < dice.GetLength(0); i++)
{
for (int j = 0; j < dice.GetLength(1); j++)
{
dice[i, j] = rnd.Next(1, _yChoice);
_sum += dice[j, i];
sum.Add(_sum);
}
}
w++;
}
}
public Tarning(int Xchoice, int Ychoice, int throw)
{
_throw = thorw;
_xChoice = Xchoice;
_yChoice = Ychoice;
}
}
答案 0 :(得分:2)
您的主要问题在静态关键字中。 静态字段表示仅限于此
所有实例的一个字段,这不是您的情况:您需要Dice
的每个实例都有自己的字段&#39;值。
class Dice {
// no static here
private int _xChoice, _yChoice, _throw;
// no static here
private List<int> sum = new List<int>();
// no static here
private int w = 0;
// no static here
private int _sum;
// no static here
private int[,] dice = new int[_xChoice, _yChoice];
// BUT, you want a random generator for all the instances, that's why "static"
private static Random rnd = new Random();
// When overriding method mark it with "override"
// And Be Careful with CAPitalization:
// the method's name "ToString" not Tostring
public override string ToString() {
...
}
void random() {
// Do not create Random generator each time you call it:
// It makes the random sequences skewed badly!
// Istead use one generator for all the calls, see the code above
// private static Random rnd = new Random();
// Random rnd = new Random();
...
}
...
答案 1 :(得分:1)
class Program
{
static void Main(string[] args)
{
var dice = new List<DiceLogic>();
int a = 0;
int times = GetTimes();
while (a != times)
{
int antThrow = GetAntThrow();
int xChoice = GetXChoice();
int yChoice = GetYChoice();
dice.Add(new DiceLogic(xChoice, yChoice, antThrow));
a++;
}
int e = 1;
foreach (var item in dice)
{
Console.WriteLine("Result of game {0}: {1}", e++, item.Tostring());
}
Console.ReadLine();
}
private static int GetTimes()
{
while (true)
{
Console.WriteLine("Write how many times you want to repeat the game:");
int times;
var result = int.TryParse(Console.ReadLine(), out times);
if (result) return times;
Console.WriteLine("Value must be a number.");
}
}
private static int GetAntThrow()
{
while (true)
{
Console.WriteLine("Write how many times you want each dice to get thrown:");
int antThrow;
var result = int.TryParse(Console.ReadLine(), out antThrow);
if (result) return antThrow;
Console.WriteLine("Value must be a number.");
}
}
private static int GetXChoice()
{
while (true)
{
Console.WriteLine("Write how many dice you want to throw:");
int getXChoice;
var result = int.TryParse(Console.ReadLine(), out getXChoice);
if (result) return getXChoice;
Console.WriteLine("Value must be a number.");
}
}
private static int GetYChoice()
{
while (true)
{
Console.WriteLine("Write how many sides you want each dice should have:");
int getXChoice;
var result = int.TryParse(Console.ReadLine(), out getXChoice);
if (result) return getXChoice;
Console.WriteLine("Value must be a number.");
}
}
}
public class DiceLogic
{
public string Tostring()
{
int maxScore = _diceSides*_dices;
if (_result >= maxScore / 2)
{
return "Good throw! " + _result;
}
return "Bad throw! " + _result;
}
private readonly int _dices;
private readonly int _diceSides;
private readonly int _throwDice;
private int _result;
private void CalculateResult()
{
var rnd = new Random();
for (int i = 0; i < _dices; i++)
{
int currentResult = 0;
for (int j = 0; j < _throwDice; j++)
{
currentResult = rnd.Next(0, _diceSides);
}
_result += currentResult;
}
}
public DiceLogic(int dices, int diceSides, int throwEachDice)
{
_dices = dices;
_diceSides = diceSides;
_throwDice = throwEachDice;
CalculateResult();
}
}
这是一个如何实现他们所要求的实例的示例,通过调试器逐行完成代码,以便您了解正在发生的事情。
答案 2 :(得分:0)
您永远不会调用方法random()
。因此,您的成员变量_sum
的值永远不会更改,并且仍为0
。您需要在某处调用方法random()
。您可能应该设置public
并在设置完所有骰子后从主方法中调用它。
此外,Dice
类中的成员变量都是static
!这意味着不同的Dice
实例将共享相同的值。我认为这不是故意的。您应该通过删除static
修饰符来创建变量实例变量。
答案 3 :(得分:0)
你的方法没有调用学习,它需要一个保留字“ throw ”[我相信它应该是钍]。该方法不是void,因此必须返回一个类型。
不调用Random()并显示任何内容。
Dice没有在其大括号内有3个参数的构造函数,但它被声明为 _Dice.Add(new Dice(xChoice,yChoice,antThrow));