我正在为课业编写一个程序,它应该生成一个10000部“电影”列表。单个“电影”由“电影名年导演”形式中的字符串组成。我说“电影”是因为电影的名字和导演应该是随机产生的字母从a。
我编写了以下逻辑来生成这样的“电影”。电影名称和导演都是随机字母组合,长度介于4-10字符之间.代码在visual studio中不会出现错误,将执行,但显示为空白。如果我写得正确,那么这段代码应该生成一个这样的字符串并打印出来,但是控制台是空的。
Do while循环用于检查列表中是否有双项(这是用于执行10000版本的)。
总之,我不明白我做错了什么?
using System;
using System.Collections.Generic;
using System.Linq;
namespace Experiment
{
class Program
{
static void Main(string[] args)
{
Movies();
Console.ReadKey();
}
public static void Movies()
{
List<string> movieList = new List<string>();
bool check = false;
do
{
string movie = "";
for (int i = 0; i < GetNum(); i++)
{
movie.Insert(0, Convert.ToString(GetLetter()));
}
movie.Insert(0, " ");
movie.Insert(0, Convert.ToString(GetYear()));
movie.Insert(0, " ");
for (int i = 0; i < GetNum(); i++)
{
movie.Insert(0, Convert.ToString(GetLetter()));
}
if (movieList.Contains(movie))
{
check = false;
}
else
{
movieList.Add(movie);
check = true;
}
} while (check == false);
Console.WriteLine(movieList[0]);
}
public static Random _random = new Random();
public static char GetLetter()
{
int num = _random.Next(0, 26);
char let = (char)('a' + num);
return let;
}
public static int GetNum()
{
int num = _random.Next(4, 11);
return num;
}
public static int GetYear()
{
int num = _random.Next(1920, 2020);
return num;
}
}
}发布于 2019-11-24 14:56:38
字符串是不可变的,因此调用电影字符串上的Insert()方法不会对当前的电影变量做任何事情。相反,它返回新字符串。
但是,最好将电影类型从string更改为StringBuilder,这是一个动态分配的字符缓冲区,因此您的示例如下:
using System;
using System.Text;
using System.Collections.Generic;
namespace sotest
{
class Program
{
static void Main(string[] args)
{
Movies();
Console.ReadKey();
}
public static void Movies()
{
List<string> movieList = new List<string>();
bool check = false;
do
{
StringBuilder movie = new StringBuilder();
for (int i = 0; i < GetNum(); i++)
{
movie.Insert(0, Convert.ToString(GetLetter()));
}
movie.Insert(0, " ");
movie.Insert(0, Convert.ToString(GetYear()));
movie.Insert(0, " ");
for (int i = 0; i < GetNum(); i++)
{
movie.Insert(0, Convert.ToString(GetLetter()));
}
if (movieList.Contains(movie.ToString()))
{
check = false;
}
else
{
movieList.Add(movie.ToString());
check = true;
}
} while (check == false);
Console.WriteLine(movieList[0]);
}
public static Random _random = new Random();
public static char GetLetter()
{
int num = _random.Next(0, 26);
char let = (char)('a' + num);
return let;
}
public static int GetNum()
{
int num = _random.Next(4, 11);
return num;
}
public static int GetYear()
{
int num = _random.Next(1920, 2020);
return num;
}
}
}发布于 2019-11-24 14:58:45
问题是您使用movie.Insert不正确。
如果您阅读了String.Insert的文档,它说
https://learn.microsoft.com/en-us/dotnet/api/system.string.insert?view=netframework-4.8
返回一个新字符串,其中在此实例中指定的索引位置插入指定的字符串。
公共字符串插入(int startIndex,string值);
因此,它返回一个新字符串,它不修改现有字符串。所以你需要这么做。
movie = movie.Insert(0, Convert.ToString(GetYear()));但是,我也必须说,以这种方式使用String.Insert并不是最好的方法。相反,您应该考虑使用StringBuilder类。当您想要修改字符串(它们是不可变的对象)时,它是非常有效的。
您可能需要阅读其中的部分,以帮助您理解。如果您向下滚动,那么它也建议StringBuilder。
https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/strings/
发布于 2019-11-24 14:59:33
Insert()方法用于在指定索引位置从指定字符串返回新字符串。在您的示例中,您没有捕获更新的字符串。
解决这一问题的最佳方法是使用StringBuilder对象。请注意,StringBuilder对象比使用不可变字符串要高效得多。
https://stackoverflow.com/questions/59018885
复制相似问题