For循环将7个项目写入一行 [英] For loop to write 7 items to a line

查看:156
本文介绍了For循环将7个项目写入一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理代码的最后一部分,我似乎无法在一行中写入7个项目.现在我的输出一行有10个项目.我愿意接受所有建议.
我得到10行和10列,然后是一个空行,后跟那些
的项目 在我的输出中大于500.
非常感谢.代码在下面

Im working on the last portion of my code and I cant seem to write 7 items to a line. Right now my output has 10 items to a line. I am open to all suggestions.
I get 10 row and 10 columns, then a blank line followed by items that
are greater than 500 in my output.
Thanks so much. Code is below

class ReadArray
    {
       
        static void Main()
        {
            int[] numberArray = new int[100];   //A 100 array element
            string number;                      //String input
            int item;                           //Array index
            
            int flag = 0;
            
            FileStream f = null;
            StreamReader s = null;
            try  
            {
                f = new FileStream("d:\\L01data.txt", FileMode.Open);
                s = new StreamReader(f);
            }
            catch 
            {
                Console.WriteLine("Does not exist");
                flag = 1; //set flag to indicate file failed to open
            }
            if (flag == 0) // File opened as expected
            {
                item = 0;
                while ((number = s.ReadLine()) != null)
                {
                    numberArray[item] = Convert.ToInt32(number);
                    item++;
                }
                for (item = 0; item < numberArray.Length; item++)
                {
                   Console.Write(numberArray[item]);
                   Console.Write('\t');
                }
                Console.WriteLine();
                //foreach loop to only write out the elements in numberArray
                //greater than 500
                foreach (int i in numberArray)
                {
                    if (i > 500)
                    {
                        Console.Write(i);
                    }
                    
                    Console.Write('\t');
                }
                
                s.Close();
                f.Close();
            }
}
}

推荐答案

在上一个循环之外具有一个count变量.
在循环内将其递增.如果等于7,则编写Environment.NewLine并重置计数,否则编写制表符
Have a count variable outside of your last loop.
Inside the loop increment it. If it equals 7 then write Environment.NewLine and reset count, else write the tab


for (item = 0; item < numberArray.Length; item++)                {                   
  Console.Write(numberArray[item]);                   
  Console.Write(''\t'');                
}                
Console.WriteLine();



这部分代码实际上在一行中写了numberArray.Length项,而控制台的自动自动换行恰好给您10行,每行10项.制表符宽度为8个字符,并且当控制台缓冲区宽度为默认的80个字符时,将在第10个制表符处进行换行.

您应该做的是在每N个项目之后开始一个新行,其中N是所需的列数.

艾伦.



This section of code actually writes numberArray.Length items in one line and it is the automatic text wrapping of the console that just happens to give you 10 rows of 10 items. The tab width is 8 characters and when the console buffer width is the default 80 characters then wrapping will occur at every 10th tab.

What you should do is start a new line after every N items, where N is the desired number of columns.

Alan.


这篇关于For循环将7个项目写入一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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