【发布时间】:2011-12-07 08:37:13
【问题描述】:
我需要显示获得 6 所需的平均掷骰数,以及该平均值所基于的 6 数。 我认为我遇到的问题是这部分代码?所以我想要我认为我拥有的平均卷数作为变量 AVGroll。平均值所基于的六位数应该是 loopcount 变量。
AVGroll = AVGroll + loopcount;
average = AVGroll / loopcount;
尝试尽可能地注释我的代码以使其可读。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace CE0721a
{
class Tut4_7
{
public void run()
{
// Random number generator
Random rndm = new Random();
//declaring number for Random Number Generator
int number;
// average number of runs
int average;
//declaring loopcount starts at 1
int loopcount = 1;
//Average roll starts at 0
int AVGroll = 0;
//Variable if it continues
int progcontinue;
//Start lable
Start:
do
{
number = rndm.Next(6) + 1;
Console.WriteLine(number);
Console.ReadKey();
if (number < 6)
{
loopcount++;
}
} while (number != 6);
AVGroll = AVGroll + loopcount;
average = AVGroll / loopcount;
Console.WriteLine("The roll count is:");
Console.WriteLine(loopcount);
Console.WriteLine("average");
Console.WriteLine(AVGroll);
Console.WriteLine("press 1 to continue or 0 to exit");
progcontinue = (int.Parse(Console.ReadLine()));
if (progcontinue > 0)
{
loopcount = 1;
goto Start;
}
else
{
}
}
}
}
【问题讨论】:
-
您的变量名难以阅读,请尝试下划线或第二个单词大写,例如
AVG_roll或AvgRoll`。 -
您的程序特别遇到什么问题?错误信息?行为不正确?如果它的行为不正确,您希望它如何表现?问题中应该包含的所有内容。我建议避免使用
goto。改为使用while(progcontinue) { /* ... */ }循环。 -
你有没有考虑过平均卷数可能不是整数值?
-
平均值是数组部分的总和除以部分的数量,您已经完成了一半。
-
@HenkHolterman 我已经发布了我尝试过的内容以及我认为我遇到困难的地方