C# 1> Program.cs(59,21,59,25):错误CS0103:名称“”在当前上下文中不存在
这是我写的练习课程的代码。我是一个完全的初学者,我只学了一天,我能发现我做错了什么,在其他帖子上,我只在同一个班级里看到过类似的问题。在代码的末尾,它告诉我,上下文中不存在狗C# 1> Program.cs(59,21,59,25):错误CS0103:名称“”在当前上下文中不存在,c#,C#,这是我写的练习课程的代码。我是一个完全的初学者,我只学了一天,我能发现我做错了什么,在其他帖子上,我只在同一个班级里看到过类似的问题。在代码的末尾,它告诉我,上下文中不存在狗 using System; using System.Collections.Generic; namespace cs_tut1 //a felhasználó megadja :állat suly, magasság. a program megnézi hogy azonos életkor(1év kül me
using System;
using System.Collections.Generic;
namespace cs_tut1 //a felhasználó megadja :állat suly, magasság. a program megnézi hogy azonos életkor(1év kül megengedett) és azonos suly (4kg megengedett) van e olyan állat majd kiirja a nevét és adatait.
{
class Animal
{
public void RandomDataGen()
{
int[,] Dogs = new int[1, 2];
int[] Weight = new int[2];//contains the random weight of dogs
int[] Height = new int[2];//contains the random height of dogs
Random weight = new Random();
for (int j = 0; j < 2; j++)
{
Weight[j] = weight.Next(4, 60);
}
Random height = new Random();
for (int j = 0; j < 2; j++)
{
Height[j] = height.Next(30, 85);
}
//insert numbers to 2D array
for (int j = 0; j < 2; j++)
{
Dogs[0, j] = Weight[j];
}
for (int j = 0; j < 2; j++)
{
Dogs[1, j] = Height[j];
}
}
}
class MainClass
{
public static void Main(string[] args)
{
Animal dataAnimal = new Animal();
dataAnimal.RandomDataGen();
int UserWeight;
int UserHeight;
//Asks for the user's dog's data
Console.Write("Please enter weight:");
UserWeight = Convert.ToInt32(Console.ReadLine());
Console.Write("Please enter height:");
UserHeight = Convert.ToInt32(Console.ReadLine());
//Looks for data similar to user's
for(int i = 0; i <= 2; i++)
{
if (Dogs[0, i] != 1)
{
}
}
Console.ReadKey();
}
}
}
将方法RandomDataGen放在类MainClass中,然后将Main中使用的变量的变量声明从方法中取出,并将它们放在类MainClass中,方法之外。为此,您不需要类Animal。问题是Dogs[,]是在方法RandomDataGen中定义的。这意味着该方法是局部的。没有其他类可以访问它;事实上,同一类中的任何方法都无法访问它。为了从另一个类或方法访问它,您需要让RandomDataGen方法将数组返回给调用者
以下是如何返回数组:
将RandomDataGen方法的返回类型从void更改为int[,]
在RandomDataGen方法的末尾,添加一行:ReturnDogs;
然后,要使用该数组,可以在Main方法中定义一个新的数组,并将RandomDataGen的返回值分配给它:
例如:
class Animal
{
public int[,] RandomDataGen()
{
int[,] Dogs = new int[1, 2];
// And the rest of the code is the same here
return Dogs;
}
}
class MainClass
{
public static void Main(string[] args)
{
Animal dataAnimal = new Animal();
int[,] Dogs = dataAnimal.RandomDataGen();
// And the rest of the code is the same here
}
}
class Animal
{
// In general, you only need one instance of `Random` per class
private Random rnd = new Random();
public int[,] RandomDataGen()
{
int[,] Dogs = new int[2, 2];
int weightIndex = 0;
int heightIndex = 1;
//insert numbers to 2D array
for (int dog = 0; dog < 2; dog++)
{
Dogs[weightIndex, dog] = rnd.Next(4, 60);
Dogs[heightIndex, dog] = rnd.Next(30, 85);
}
return Dogs;
}
}
关于RandomDataGen方法中代码的旁注:您只需要为一个类声明一个随机实例,它可以在任何地方使用。这将有助于使随机数更加随机,因为默认情况下,类是由系统时间播种的,因此,如果在足够短的时间内创建多个随机数,它们都将生成相同的随机序列
另一个问题是,为第一个索引分配的项目比定义的多。看起来数组的第一个索引包含权重和高度,但您已将其大小定义为1。应该改为2:int[,]Dogs=newint[2,2]
您还可以减少正在执行的循环次数,因为您在同一索引中循环了4次,一次选择随机权重,一次选择随机高度,一次设置权重值,第四次设置高度值。相反,您可以将每个循环的操作组合到单个循环中
例如:
class Animal
{
public int[,] RandomDataGen()
{
int[,] Dogs = new int[1, 2];
// And the rest of the code is the same here
return Dogs;
}
}
class MainClass
{
public static void Main(string[] args)
{
Animal dataAnimal = new Animal();
int[,] Dogs = dataAnimal.RandomDataGen();
// And the rest of the code is the same here
}
}
class Animal
{
// In general, you only need one instance of `Random` per class
private Random rnd = new Random();
public int[,] RandomDataGen()
{
int[,] Dogs = new int[2, 2];
int weightIndex = 0;
int heightIndex = 1;
//insert numbers to 2D array
for (int dog = 0; dog < 2; dog++)
{
Dogs[weightIndex, dog] = rnd.Next(4, 60);
Dogs[heightIndex, dog] = rnd.Next(30, 85);
}
return Dogs;
}
}
您在RandomDataGen中声明了Dogs,它不能超出该方法。您不需要声明多个随机实例。只需创建一个通常命名为random或rnd的,并将其用于体重和身高