在Java中,如何每5行打印一个空行?

在Java中,如何每5行打印一个空行?,java,loops,Java,Loops,这是基本的编程,但我仍然不能完全确定如何每5行创建一个空行。请帮忙!谢谢大家! public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("please enter an integer for the length"); int theLength = input.nextInt(); System.out.printl

这是基本的编程,但我仍然不能完全确定如何每5行创建一个空行。请帮忙!谢谢大家!

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);

    System.out.println("please enter an integer for the length");
    int theLength = input.nextInt();
    System.out.println("Please enter an integer for the height");
    int theHeight = input.nextInt();
    System.out.println("Please enter a character");
    String character1 = input.next();


    //  int character = Integer.toString(character1);

    for (int i = 0; i < theHeight; i++) {     //the outer loop controls the row


        for (int j = 0; j < theLength; j++) {// inner loop control 
            if (j % 6 != 0) { //creates a space for ever 5 character
                System.out.print(character1 + " ");
            } else System.out.print(" ");

        }
        System.out.println();
    }
}
publicstaticvoidmain(字符串[]args){
扫描仪输入=新扫描仪(System.in);
System.out.println(“请输入长度的整数”);
int theLength=input.nextInt();
System.out.println(“请输入高度的整数”);
int theHeight=input.nextInt();
System.out.println(“请输入字符”);
String character1=input.next();
//int character=Integer.toString(character1);
对于(inti=0;i
如果我理解你的问题,你可以将你的
else
打印(“”
)更改为
打印ln
;像

if(j%6!=0){ //creates a space for ever 5 character
    System.out.print(character1 + " ");
} else {
    System.out.println();
}
println(“…”)
方法打印字符串并将光标移动到新行,但是
print(“…”)
方法只打印字符串,而不将光标移动到新行


希望有帮助

您的循环是以零为基础的,因此第0行也可以被5整除:如果满足您的条件,则按如下方式修改for循环:

for (int i = 0; i < theHeight; i++) { // the outer loop controls the row
            for (int j = 0; j < theLength; j++) {// inner loop control
                if (i % 5 != 0) { // creates a space for ever 5 character
                    System.out.print(character1 + " ");
                } else {
                    System.out.print(" ");
                }

            }
            System.out.println();
        }
for(inti=0;i
如何每第五行打印一个空行

根据您的代码,在我看来,您希望在每个n字符之后创建一个空行,而不是在n行之后

无论您想在n行或n列之后打印空格或换行符,您只需要一个循环


每5个字符打印一行换行:

A A A A A 
A A A A A 
A A A A A 

不清楚你在问什么。请提供预期输出与实际输出、您遇到的确切问题以及问题的清晰描述。不清楚您是要在每第5行还是每第6行创建一个空格。@Paula您可以看看我的解决方案。
A A A A A 
A A A A A 
A A A A A