给出一些背景。对于这个项目,我有一个类变量数组,cd1。这是AudioCD的变量,它是一个数组,因为如果用户想要在数组中输入更多CD。
我得到的问题是,当我在类数组中输入多个cd时,在我添加字符串或艺术家的部分周围是我得到问题的地方。当我去打印类变量的特定索引时,它们应该是分开的,因为一些奇怪的原因,除了艺术家数组,大多数东西都显示了最后输入的行。我想弄清楚它为什么这样做。
p。对不起,我的解释不是最好的。
如图所示。输出应该是当我输入1作为选择,1作为CD时,数组应该使用索引0的数组,但它使用的是上次输入CD的数组。输出应该是:
男士一男士二男3
但它是:
男4man5man6
class AudioCD
{
// Private Variables for CDclass
public string cdTitle { get; private set; }
private string[] artists = new string[4];
public int releaseYear { get; private set; }
public string genre { get; private set; }
public float condition { get; private set; }
// Constructor for the CDclass - initializes all variables used in the CDclass
public AudioCD()
{
cdTitle = "";
artists = new string[] {"","","",""};
releaseYear = 1980;
genre = "";
condition = 0.0f;
}
// Overload Constructor for the CDclass - initializes all the variables to user input variables
public AudioCD(string cdt, string[] art, int reY, string gen, float con)
{
cdTitle = cdt;
if (artists.Length < art.Length)
{
Console.WriteLine("Your Array size is bigger then 4 for the Artist so the first 4 names will be used!");
}
artists = art;
if (reY < 1980)
{
releaseYear = 1980;
}
else
{
releaseYear = reY;
}
genre = gen;
if (con < 0.0f || con > 5.0f)
{
condition = 0.0f;
}
else
{
condition = con;
}
}
public void printAudioCD()
{
Console.Write(cdTitle + ", " + releaseYear + "n" );
for (int i = 0; i < artists.Length; i++)
{
if (artists[i] != "" )
{
Console.WriteLine("Artist (#" + (i + 1) + "): " + artists[i]);
}
}
Console.WriteLine("Genre: " + genre);
Console.WriteLine("Condition: " + condition);
}
}
和Program
类:
class Program
{
static void Main(string[] args)
{
// variables
string uI, cdtitle, genre;
int size = 0, releaseYear, choice, arrInd;
string[] artistArray = new string[4] {"", "", "", "" };
float condition;
//
AudioCD remote = new AudioCD();
Console.Write("How many CDs do you have lying around your car? ");
uI = Console.ReadLine();
size = Convert.ToInt32(uI);
AudioCD[] cd1 = new AudioCD[size];
for (int i = 0; i < size; i++)
{
Console.WriteLine("CD #" + (i + 1));
Console.Write("*Enter Title: ");
uI = Console.ReadLine();
cdtitle = uI;
Console.WriteLine("*Enter Artists (type -1 when finished):");
int j = 0;
do
{
uI = Console.ReadLine();
if (uI != "-1")
artistArray[j] = uI;
j++;
// Resize the array by One Element
if (j >= 4 && uI != "-1")
{
Array.Resize(ref artistArray, artistArray.Length + 1);
artistArray[j] = "";
}
} while (uI != "-1" );
Console.Write("*Enter Genre: ");
uI = Console.ReadLine();
genre = uI;
Console.Write("*Enter Release Year: ");
uI = Console.ReadLine();
releaseYear = Convert.ToInt32(uI);
Console.Write("*Enter Condition: ");
uI = Console.ReadLine();
condition = float.Parse(uI);
Console.Write("n");
// switch to select which class of cd to put information in
cd1[i] = new AudioCD(cdtitle, artistArray, releaseYear, genre, condition);
}
bool isPlaying = true;
while(isPlaying)
{
Console.Write("n");
Console.WriteLine("[Main Menu]");
Console.WriteLine("1) Album Info");
Console.WriteLine("2) Find a CD");
Console.WriteLine("3) Find an artist");
Console.WriteLine("4) Log off");
Console.Write("Choice: ");
uI = Console.ReadLine();
choice = Convert.ToInt32(uI);
switch(choice)
{
case 1:
{
Console.Write("nWhich CD do you want? ");
uI = Console.ReadLine();
arrInd = Convert.ToInt32(uI);
if (arrInd >= 1 || arrInd <= size)
{
Console.Write(arrInd + ". ");
cd1[(arrInd - 1)].printAudioCD();
}
break;
}
case 2:
{
break;
}
case 3:
{
break;
}
case 4:
{
isPlaying = false;
break;
}
default:
{
break;
}
}
}
}
}
正如你在图片中看到的。输出应该是当我输入1作为选择,1作为CD时,数组应该使用索引0的数组,但它使用的是上次输入CD的数组。输出应该是man1、man2和man3,但它是man4、man5和man6。
您在Main()
中每次迭代使用相同的数组,这就是为什么您从最后输入的CD中获得艺术家的原因。我的意思是这个代码片段:
string[] artistArray = new string[4] {"", "", "", "" };
在这里,我们可以使用List<T>
来避免编写数组大小调整的代码,我们可以使用Clear()方法来避免存储以前CD中的艺术家。
整个代码看起来像这样。我用List:
重构了你的代码AudioCD
类:
public class AudioCD
{
// Private Variables for CDclass
public string cdTitle { get; private set; }
private List<string> artists = new List<string>();
public int releaseYear { get; private set; }
public string genre { get; private set; }
public float condition { get; private set; }
// Constructor for the CDclass - initializes all variables used in the CDclass
public AudioCD()
{
cdTitle = "";
releaseYear = 1980;
genre = "";
condition = 0.0f;
}
// Overload Constructor for the CDclass - initializes all the variables to user input variables
public AudioCD(string cdt, List<string> art, int reY, string gen, float con)
{
cdTitle = cdt;
artists.AddRange(art);
if (reY < 1980)
{
releaseYear = 1980;
}
else
{
releaseYear = reY;
}
genre = gen;
if (con < 0.0f || con > 5.0f)
{
condition = 0.0f;
}
else
{
condition = con;
}
}
public void printAudioCD()
{
Console.Write(cdTitle + ", " + releaseYear + "n");
for (int i = 0; i < artists.Count; i++)
{
if (artists[i] != "")
{
Console.WriteLine("Artist (#" + (i + 1) + "): " + artists[i]);
}
}
Console.WriteLine("Genre: " + genre);
Console.WriteLine("Condition: " + condition);
}
}
和Main
方法看起来像这样:
// variables
string uI, cdtitle, genre;
int size = 0, releaseYear, choice, arrInd;
List<string> artistArray = new List<string>();
float condition;
Console.Write("How many CDs do you have lying around your car? ");
uI = Console.ReadLine();
size = Convert.ToInt32(uI);
AudioCD[] cd1 = new AudioCD[size];
for (int i = 0; i < size; i++)
{
Console.WriteLine("CD #" + (i + 1));
Console.Write("*Enter Title: ");
uI = Console.ReadLine();
cdtitle = uI;
Console.WriteLine("*Enter Artists (type -1 when finished):");
int j = 0;
do
{
uI = Console.ReadLine();
if (uI != "-1")
artistArray[j] = uI;
j++;
} while (uI != "-1");
Console.Write("*Enter Genre: ");
uI = Console.ReadLine();
genre = uI;
Console.Write("*Enter Release Year: ");
uI = Console.ReadLine();
releaseYear = Convert.ToInt32(uI);
Console.Write("*Enter Condition: ");
uI = Console.ReadLine();
condition = float.Parse(uI);
Console.Write("n");
// switch to select which class of cd to put information in
cd1[i] = new AudioCD(cdtitle, artistArray, releaseYear, genre, condition);
artistArray.Clear();
}
bool isPlaying = true;
while (isPlaying)
{
Console.Write("n");
Console.WriteLine("[Main Menu]");
Console.WriteLine("1) Album Info");
Console.WriteLine("2) Find a CD");
Console.WriteLine("3) Find an artist");
Console.WriteLine("4) Log off");
Console.Write("Choice: ");
uI = Console.ReadLine();
choice = Convert.ToInt32(uI);
switch (choice)
{
case 1:
{
Console.Write("nWhich CD do you want? ");
uI = Console.ReadLine();
arrInd = Convert.ToInt32(uI);
if (arrInd >= 1 || arrInd <= size)
{
Console.Write(arrInd + ". ");
cd1[(arrInd - 1)].printAudioCD();
}
break;
}
case 2:
{
break;
}
case 3:
{
break;
}
case 4:
{
isPlaying = false;
break;
}
default:
{
break;
}
}
}