简体   繁体   English

我怎样才能让这个打印所有的数字?

[英]How can I make this print all the numbers?

I'm trying to get this program to put all numbers between 1 and n in the file it creates, separated by a tab between each number.我试图让这个程序将 1 和 n 之间的所有数字放在它创建的文件中,每个数字之间用制表符分隔。 Right now it's only printing the number it asks to input, but I don't know what to add to make it do the rest.现在它只打印它要求输入的数字,但我不知道要添加什么才能使它执行 rest。 Any ideas how I can edit this and make it work?有什么想法可以编辑它并使其工作吗? Thanks!谢谢!

{
        Console.WriteLine("Please input value of n: ");
        char[] array = Console.ReadLine().ToCharArray();
        string output = string.Empty;
        Console.WriteLine("1 through n: ");
        foreach (char ch in array)
        {
            output += ch;
        }
        Console.WriteLine("{0}", output);

        File.WriteAllText("1ton.txt", output);  


    }

The number input should be saved as an integer so it can be counted up to:输入的数字应保存为 integer 以便最多可计数:

Console.WriteLine("Please input value of n: ");
int myawesomeinteger = Convert.ToInt32(Console.ReadLine());
string output = string.Empty;
Console.WriteLine("1 through n: ");
for (int i = 1; i < myawesomeinteger + 1; i++)
{
  output += i.ToString() + "\t";
}
Console.WriteLine("{0}", output);
File.WriteAllText("1ton.txt", output);

You should also check the user inputs an integer and not some rubbish but this works if you type in any number up to 2,147,483,647.您还应该检查用户输入的 integer 而不是一些垃圾,但如果您输入不超过 2,147,483,647 的任何数字,这将有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM