System.Random不包含定义…C#?
我在这个错误上花了将近一个小时,我似乎找不到哪里出了错(换一双眼睛可能会有帮助,哈哈)。这是{array=Random.genRandom();}我也得到了名称rand在我的main类的上下文中不存在。上面写着‘我的构造者也有一个错误,我基本上只是在做一个随机数生成器游戏,在这个游戏中,数字显示在开头,玩家可以得到一个打击、一个球等等。。对于错误的答案。我有两节课。一个主类和一个子类 主要类别:System.Random不包含定义…C#?,c#,C#,我在这个错误上花了将近一个小时,我似乎找不到哪里出了错(换一双眼睛可能会有帮助,哈哈)。这是{array=Random.genRandom();}我也得到了名称rand在我的main类的上下文中不存在。上面写着‘我的构造者也有一个错误,我基本上只是在做一个随机数生成器游戏,在这个游戏中,数字显示在开头,玩家可以得到一个打击、一个球等等。。对于错误的答案。我有两节课。一个主类和一个子类 主要类别: using System; using System.Collections.Generic; u
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NumBaseBall
{
class MainClass
{
static void Main(string[] args)
{
int[] player;
int[] array;
int NumberOfTries = 0;
int Strikes = 0;
int ball = 0;
int input;
int count = 0;
int g = 0;
decimal game = 0;
decimal wins = 0;
decimal percent = 0;
int Ru = 0;
Console.WriteLine("Welcome to Number Baseball Game");
Console.WriteLine("By: ");
while (Ru != -1)
{
Console.WriteLine("The three Digit random number is: \t");
MethodClass = rand = new MethodClass();
array = Random.genRandom();
Console.WriteLine();
NumberOfTries = 0;
game++;
while (NumberOfTries < 5)
{
ball = 0;
Strikes = 0;
Console.Write("Enter three digit number: \t");
input = Convert.ToInt32(Console.ReadLine());
player = MethodClass.splitNumber(input);
for (int i = 0; i < 3; i++)
{
count = 0;
if ((player[i] == array[g]) && (count == 0))
{
if (player[0] == array[0])
{
Strikes++;
}
else
{
ball++;
}
count++;
}
else if ((player[i] == array[g + 1]) && (Strikes == 0))
{
if (player[2] == array[2])
{
Strikes++;
}
else
{
ball++;
}
}
if (Strikes == 3)
{
Console.WriteLine("Goodjob! You guessed the random number!");
wins++;
continue;
}
else if (Strikes == 0 && ball == 0)
{
Console.WriteLine("0 Strikes, 0 ball");
}
else if ((Strikes > 0 && Strikes < 3) || (ball > 0))
{
Console.WriteLine("{0} Strikes and {1}", Strikes, ball);
}
NumberOfTries++;
}
if (Strikes == 0)
{
Console.WriteLine("\nYou lost!");
}
percent = (wins / game) * 100;
Console.WriteLine("Total amount played: {0}, Number of wins: {1}, Winning Percentage: {2:F2}",
game, wins, percent);
Console.WriteLine("Press Enter to exist");
Console.ReadLine();
}
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
名称空间麻木球
{
类主类
{
静态void Main(字符串[]参数)
{
int[]播放器;
int[]数组;
int numberofthries=0;
int=0;
int-ball=0;
int输入;
整数计数=0;
int g=0;
十进制游戏=0;
十进制赢=0;
小数百分比=0;
int-Ru=0;
Console.WriteLine(“欢迎来到数字棒球赛”);
Console.WriteLine(“由:”);
while(Ru!=-1)
{
Console.WriteLine(“三位随机数为:\t”);
MethodClass=rand=new MethodClass();
array=Random.genRandom();
Console.WriteLine();
次数=0;
游戏++;
while(尝试次数<5)
{
ball=0;
罢工=0;
控制台。写入(“输入三位数:\t”);
输入=Convert.ToInt32(Console.ReadLine());
player=MethodClass.splitNumber(输入);
对于(int i=0;i<3;i++)
{
计数=0;
如果((玩家[i]==数组[g])&&(计数==0))
{
如果(玩家[0]==数组[0])
{
罢工++;
}
其他的
{
ball++;
}
计数++;
}
如果((玩家[i]==数组[g+1])&&(攻击==0))
{
如果(玩家[2]==数组[2])
{
罢工++;
}
其他的
{
ball++;
}
}
如果(罢工==3)
{
WriteLine(“干得好!你猜到了随机数!”);
wins++;
继续;
}
否则,如果(打击==0&&ball==0)
{
控制台。写线(“0击,0球”);
}
如果((击数>0&&击数<3)| |(球>0))
{
写线({0}击和{1}),击,球);
}
numberofthries++;
}
如果(罢工==0)
{
Console.WriteLine(“您输了!”);
}
百分比=(赢/局)*100;
WriteLine(“播放总量:{0},获胜次数:{1},获胜百分比:{2:F2}”,
游戏,胜利,百分比);
Console.WriteLine(“按Enter键以存在”);
Console.ReadLine();
}
}
}
}
}
方法类别:
//Mohamed Shire
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NumBaseBall
{
class MethodClass
{
int[] random = new int[3];
public int[] genRandom()
{
int my;
Random rand = new Random();
for (int i = 0; i < random.Length; i++)
{
my = rand.Next(1, 10);
if (isExists(random, my) == false)
{
random[i] = my;
Console.Write(random[i]);
}
else
{
i--;
}
}
return random;
}
public bool isExists(int[] array, int my)
{
for (int i = 0; i < array.Length; i++)
{
if (array[i] == my)
{
return true;
}
}
return false;
}
public static int[] splitNumber(int input)
{
int[] uInput = new int[3];
uInput[0] = input / 100;
uInput[1] = input % 100 / 10;
uInput[2] = input % 100 % 10;
return uInput;
}
public static void outputArray(int[] array)
{
for (int i = 0; i < array.Length; i++)
{
Console.WriteLine(array[i]);
}
}
}
}
//穆罕默德郡
使用制度;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
名称空间麻木球
{
类方法类
{
int[]随机=新的int[3];
公共int[]genRandom()
{
intmy;
Random rand=新的Random();
for(int i=0;i
尝试更改此选项:
MethodClass = rand = new MethodClass();
array = Random.genRandom();
致:
请阅读并尝试将代码裁剪为一个较小的示例,以再现该问题。这样做,您可能会自己解决问题。检查
后,使用确保程序的流畅。请输入。嘿,简单地问一个问题:我想在最后提示,当玩家点击0时,游戏将重新启动,并且
MethodClass rand = new MethodClass();
array = rand.genRandom();