生成可能的字符串 [英] Generate Possible Strings

查看:100
本文介绍了生成可能的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作一个程序,在该程序中,如果我在4个位置上有并且在前3个位置上有符号,而在第4个位置上是数字,则我将生成所有可能的组合.并将其写入记事本中.知道我该如何开始吗?

I want to make program where i would generate all possible combinations if i have on 4 places and in first 3 places there is sign and at the 4th place is number. And to it will write it in notepad. Any idea how i can start with this?

推荐答案

您可以嵌套for循环,并让它们遍历所需的字符,如下所示:
You can nest for-loops and have them iterate through the desired characters like this:
for (char symbol1 = 'a'; symbol1 <= 'z'; symbol1++)
{
    for (char symbol2 = 'a'; symbol2 <= 'z'; symbol2++)
    {

    }
}


以及如果您希望打印到文本文件:


and if You wish to print to a text file:

StreamWriter output = new StreamWriter("output.txt");
output.WriteLine("Hello! This is the first line in my text file.");
output.WriteLine("And this is the second one.");
output.Close();


不要忘记调用output.Close()或output.Flush(),以确保在程序终止之前将文本写入文件中.

或简单地使用:


don''t forget to call output.Close() or output.Flush() to make sure that the text is written to the file before your program terminates

or simply use:

File.WriteAllText("output.txt", "Hello!");


您将需要添加"using System.IO;".在您程序的顶部


You will need to add "using System.IO;" at the top of your program


在一开始就添加此内容:

ADD this at the begining:

using System.IO;





StreamWriter sw = new StreamWriter("File name.txt");


for (char symbol1 = ''a''; symbol1 <= ''z''; symbol1++)

    for (char symbol2 = ''a''; symbol2 <= ''z''; symbol2++)

         for (char symbol3 = ''a''; symbol2 <= ''z''; symbol2++)

            for (int num = 0; num <= 10; num++)

                 sw.WriteLine("{},{},{},{}",symbol1,symbol2,symbol3,num );
                 sw.close();




祝您好运:)




Good Luck :)


这可能会有所帮助,

It might be helpful,

using System;
using System.IO;
using System.Linq;

class Program
{
    static void Main(string[] args)
    {
        int counter = -1;

        using (StreamWriter writer = new StreamWriter(@"C:\Temp\DataFile.txt", true))
        {
            Array.ForEach(GetData(), item =>
            {
                if (counter >= 9) counter = -1;
                writer.WriteLine(string.Concat(item, item, item, ++counter));
            });
        }
    }

    static char[] GetData()
    {
        return Enumerable.Range('a', ('z' - 'a') + 1).Select(c => (char)c).ToArray();
    }
}



:)



:)


这篇关于生成可能的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆