简体   繁体   English

如何在每第 5 个数字后开始一个新行?

[英]How can I start a new line after every 5th number?

I'd like get some help to solve the issue with having a new line after every fifth number.我想得到一些帮助来解决每五个数字后有一个新行的问题。 I put random numbers between 1 and 90 into an array list and then write it into a file.我将 1 到 90 之间的随机数放入数组列表中,然后将其写入文件。 (Later I'd like to sort the list and everything like that hence I'm using an ArrayList instead of writing the numbers immediately into the file.) (稍后我想对列表和所有类似的东西进行排序,因此我使用的是 ArrayList 而不是立即将数字写入文件。)

public class Lotto {

    static ArrayList<Integer> list = new ArrayList();


    public static void main(String[] args) throws IOException {


          randomNumber();
          fileReader();
    }

    public static void printHeader() {
        System.out.println("Week"
                + "|Numbers                           "
                + "\n"
                + "----+"
                + "-----------------------+");
    }

    public static void randomNumber() {
        int num;
        int n = 5;

      //  String str = String.valueOf(num);

        list = new ArrayList<>();
        try {


            FileWriter writer = new FileWriter("/Users/xyz/desktop/lotto2010.txt");
            BufferedWriter bw = new BufferedWriter(writer);

            for (int i = 1; i <= 260; i++) {
            double number = Math.random() * 90;

            num = (int) number;


            list.add(num);
            if((i % n) == 0) bw.newLine();

            }

            bw.write(list.toString());
            bw.close();

        } catch (IOException ex) {
            System.out.println("Couldn't write the file or directory doesn't exist" + ex.getMessage());
        }
    }

    public static void fileReader() throws IOException {


        FileReader fileReader = new FileReader("/Users/xyz/desktop/lotto2010.txt");
        String allText;
        try (PrintWriter writer = new PrintWriter("/Users/xyz/desktop/lotto2011.txt")) {
            BufferedReader br = new BufferedReader(fileReader);


            allText = br.readLine();

                writer.print(allText);            

        }
        System.out.println(allText);

    }
}

添加一个loop ,检查i % 5 == 0 ,添加/nbw.newLine();

PsuedoCode :伪代码:

for (int i = 0; i < array.size(); i++{
    println(array(i));
    if(i % 5 == 0){
        println();
    }
}

This will do the needful, what you are doing is that, while generating numbers you are adding it to the list and writing a newline at the same time after every 5 numbers.这将是必要的,您正在做的是,在生成数字时,您将其添加到列表中,并在每 5 个数字后同时写一个换行符 Though you should do it while iterating through the array.尽管您应该在遍历数组时执行此操作。 Before iteration, you can modify your array using sorting or anything在迭代之前,您可以使用排序或任何方式修改数组

FileWriter writer = new FileWriter("/Users/xyz/desktop/lotto2010.txt");
BufferedWriter bw = new BufferedWriter(writer);

for (int i = 1; i <= 260; i++) {
    double number = Math.random() * 90;
    num = (int) number;
    list.add(num);
}

// optional step like sorting, as per your question
Collections.sort(list);

for (int i = 0; i < list.size(); i++) {
     bw.write(list.get(i).toString());
     if(i!=0 && i % n == 0) {
         bw.newLine();
     } else {
         bw.write(", ");
     }
}

bw.close();

getting output like this -得到这样的输出 -

9, 10, 10, 10, 11
11, 11, 12, 12, 13
13, 13, 13, 13, 13
14, 14, 14, 14, 15
16, 16, 17, 17, 17
18, 18, 19, 19, 19
19, 20, 21, 21, 21
22, 23, 23, 23, 23
23, 24, 24, 24, 25
26, 26, 26, 26, 26
.............

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

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