您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

Java每n行追加换行符

Java每n行追加换行符

只需if在for循环中添加语句,该语句将在每n行输出之后输出新行(我相信这是您想要的,而不是在n次迭代之后)。假设您要每3行换一行伪代码

int newLine = 3;
int multiplicator = 1;
for(int i = 0; i< names.length; i++){
  if( newLine == i){
    // print extra line after every 3rd output        
    field.append("\n");
    newLine = 3; //to reset variable to the correct delimiter on each subsequent n-th iteration
    multiplicator++;
    newLine = newLine * multiplicator;
  }
field.append(names[i] + "\n")
// print regular value on normal line during every iteration
}
java 2022/1/1 18:26:28 有581人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶