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

我想获得一些帮助来解决此问题,方法是每五个数字后换一行。我将1到90之间的随机数放入数组列表中,然后将其写入文件中。 (稍后我想对列表和类似的东西进行排序,因此我使用的是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);

    }
}
CS_DN_CS_DN 回答:如何在每第5个数字后开始新的一行?

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

,

PsuedoCode:

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

这将是必要的,您正在做的是,在生成数字的同时,您将其添加到列表中,并每隔5个数字同时写一个换行符。虽然您应该在遍历数组时执行此操作。在迭代之前,您可以使用排序或其他任何方法

修改阵列
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();

获得这样的输出-

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

您可以使用如下所示的嵌套循环以更好的方式对其进行管理:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class Lotto {
    static List<Integer> list=new ArrayList<Integer>();    
    public static void main(String[] args) throws IOException {
        randomNumber();
        fileReader();
    }    
    public static void randomNumber() {
        int n = 5;
        List<Integer> tempList;
        try {
            BufferedWriter bw = new BufferedWriter(new FileWriter("lotto2010.txt"));
            for (int i = 1; i <= 260/n; i++) {
                tempList = new ArrayList<Integer>();
                for(int j=1;j<=n;j++) {
                    tempList.add((int)(Math.random() * 90));                        
                }
                bw.write(tempList.toString());
                bw.newLine();
                list.addAll(tempList);
            }
            //System.out.println(list);
            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 {
        StringBuffer allText=new StringBuffer();
        String strCurrentLine;
        try {
            BufferedReader br = new BufferedReader(new FileReader("lotto2010.txt"));
            while ((strCurrentLine = br.readLine()) != null) 
                allText.append(strCurrentLine).append("\n");
            br.close();
        }catch(Exception e) {
            System.out.println("Error reading file");
        }
        System.out.println(allText);
    }
}

输出:

[14,72,41,59,59]
[45,50,79,79]
[5,77,74,62,75]
[45,65,31,51,79]
[69,52,43,52]
[64,2]
[56,49,71,66,66]
[88,7,36,23]
[54,34,2,50]
[62,44,48,73,89]
[72,3,64,21]
[3,85,35,5]
[19,1,5]
[58,29,58,39]
[0,38,85]
[86,32,79]
[25,36]
[63,55,88,89]
[24,22,57,15]
[40,48]
[32,8,37,35]
[46,62]
[50,28,67,35]
[21,4,81,71]
[15,46,65]
[12,56,67]
[77,40,4]
[26,27,23]
[45,16]
[83,6,63,81]
[69,32]
[63,54,65]
[47,15,30,68]
[28,35]
[44,89,42,44]
[49,5,26]
[10,15]
[52,70,47]
[63,53,75]
[55,60,55]
[44,68]
[2,1]
[25,72]
[58,33,74]
[12,29]
[34,62]
[89,35]
[42,35]
[30,45,83,31]
[60,61]
[68,84]
[72,15]
本文链接:https://www.f2er.com/2854720.html

大家都在问