如何在每次打印电话后打印新行

时间:2017-09-19 15:27:27

标签: java

我希望在功能中使用换行符来获得尽可能多的星星。但是我无法通过换行获得它们。

public class prac11 {

    public static void main(String[] args) {
        //printStars(1);
        printStars(2);
        printStars(3);
    }

    public static void printStars(int x) {
        int i=1;
        while(i<=x) {
            System.out.print("*");
            i++;
        }
    }
}

My output

Desired Output

2 个答案:

答案 0 :(得分:4)

您必须添加println语句,以便在循环后添加换行符:

public class prac11 {

    public static void main(String[] args) {
        printStars(5);
        printStars(3);
        printStars(9);
    }

    public static void printStars(int x) {
        int i=1;
        while(i<=x) {
            System.out.print("*");
            i++;
        }
        System.out.println(); // this will produce a linebreak
    }
}

输出:

*****
***
*********

答案 1 :(得分:1)

在while循环后添加char sep = PRFile.separatorChar; String exportPath= tools.getProperty("pxProcess.pxServiceExportPath").getStringValue(); DateTimeUtils dtu = ThreadContainer.get().getDateTimeUtils(); String fileNameParam = tools.getParamValue("FileName"); if(fileNameParam.equals("")){ fileNameParam = "RecordsToCSV"; } //append a time stamp Boolean appendTimeStamp = tools.getParamAsBoolean(ImmutablePropertyInfo.TYPE_TRUEFALSE,"AppendTimeStampToFileName"); FileName += fileNameParam; if(appendTimeStamp) { FileName += "_"; String currentDateTime = dtu.getCurrentTimeStamp(); currentDateTime = dtu.formatDateTime(currentDateTime, "HH-mm-ss_dd.MM.yyyy", "", ""); FileName += currentDateTime; } //append a file format FileName += ".csv"; String strSQLfullPath = exportPath + sep + FileName; PRFile f = new PRFile(strSQLfullPath); PROutputStream stream = null; PRWriter out = null; try { // Create file stream = new PROutputStream(f); out = new PRWriter(stream, "UTF-8"); // Bug with Excel reading a file starting with 'ID' as SYLK file. If CSV starts with ID, prepend an empty space. if(CSVString.startsWith("ID")){ CSVString=" "+CSVString; } out.write(CSVString); } catch (Exception e) { oLog.error("Error writing csv file: " + e.getMessage()); } finally { try { // Close the output stream out.close(); } catch (Exception e) { oLog.error("Error of closing a file stream: " + e.getMessage()); } } 以获取换行符。