如何在java中创建一个循环,在每行打印一个稍微不同的字符串?
在我的第一节计算机科学课上,我试图制作一个程序,打印加拿大国旗的ASCII设计。我已经有了一个有效的程序,但它太庞大了,我希望能简化它。有没有办法使循环打印相同的输出如何在java中创建一个循环,在每行打印一个稍微不同的字符串?,java,loops,Java,Loops,在我的第一节计算机科学课上,我试图制作一个程序,打印加拿大国旗的ASCII设计。我已经有了一个有效的程序,但它太庞大了,我希望能简化它。有没有办法使循环打印相同的输出 public class Flag{ /* * Uses fixed combinations of ASCII characters to produce * the Canadian flag. */ public static void main(String args[]){
public class Flag{
/*
* Uses fixed combinations of ASCII characters to produce
* the Canadian flag.
*/
public static void main(String args[]){
//Components of flag
String topAndBottom ="|---------------------------------------|";
String leftSide = "|**********";
String rightSide = "**********|";
String flagRow1 =" ";
String flagRow2 =" ^ ";
String flagRow3 =" ^ /*\\ ^ ";
String flagRow4 =" /*\\|*|/*\\ ";
String flagRow5 =" . --*********-- . ";
String flagRow6 =" \\*********/ ";
String flagRow7 =" >*******< ";
String flagRow8 =" *********** ";
String flagRow9 =" --------- ";
String flagRow10 =" | | ";
//Print flag
System.out.println(" "+"\n"+
topAndBottom+"\n"+
leftSide+flagRow1+rightSide+"\n"+
leftSide+flagRow2+rightSide+"\n"+
leftSide+flagRow3+rightSide+"\n"+
leftSide+flagRow4+rightSide+"\n"+
leftSide+flagRow5+rightSide+"\n"+
leftSide+flagRow6+rightSide+"\n"+
leftSide+flagRow7+rightSide+"\n"+
leftSide+flagRow8+rightSide+"\n"+
leftSide+flagRow9+rightSide+"\n"+
leftSide+flagRow10+rightSide+"\n"+
leftSide+flagRow1+rightSide+"\n"+
topAndBottom+"\n");
公共类标志{
/*
*使用ASCII字符的固定组合生成
*加拿大国旗。
*/
公共静态void main(字符串参数[]){
//国旗的组成部分
字符串topAndBottom=“|------------------------------------------|”;
字符串leftSide=“|**********”;
字符串右侧=“*********|””;
字符串flagRow1=“”;
字符串flagRow2=“^”;
字符串flagRow3=“^/*\\^”;
字符串flagRow4=“/*\\\\\*\\\”;
字符串flagRow5=“.-->********-.”;
String flagRow6=“\\**********/”;
字符串flagRow7=“>*******<”;
字符串flagRow8=“*************”;
字符串flagRow9=“-----------”;
字符串flagRow10=“| |”;
//打印标志
System.out.println(“+”\n”+
顶部和底部+“\n”+
左侧+标志行1+右侧+“\n”+
左侧+标志行2+右侧+“\n”+
左侧+标志行3+右侧+“\n”+
左侧+标志行4+右侧+“\n”+
左侧+标志行5+右侧+“\n”+
左侧+标志行6+右侧+“\n”+
左侧+标志行7+右侧+“\n”+
左侧+标志行8+右侧+“\n”+
左侧+标志行9+右侧+“\n”+
左侧+标志行10+右侧+“\n”+
左侧+标志行1+右侧+“\n”+
顶部和底部+“\n”);
您可以使用数组。在Java8+中,还可以使用流
// Components of flag
String topAndBottom = "|---------------------------------------|";
String leftSide = "|**********";
String rightSide = "**********|";
String[] rows = { " ", //
" ^ ", //
" ^ /*\\ ^ ", //
" /*\\|*|/*\\ ", //
" . --*********-- . ", //
" \\*********/ ", //
" >*******< ", //
" *********** ", //
" --------- ", //
" | | ", //
" " };
System.out.println(topAndBottom);
Stream.of(rows).forEachOrdered(r -> System.out.println(leftSide + r + rightSide));
System.out.println(topAndBottom);
//标志的组件
字符串topAndBottom=“|------------------------------------------|”;
字符串leftSide=“|**********”;
字符串右侧=“*********|””;
字符串[]行={“”//
" ^ ", //
" ^ /*\\ ^ ", //
" /*\\|*|/*\\ ", //
" . --*********-- . ", //
" \\*********/ ", //
" >*******< ", //
" *********** ", //
" --------- ", //
" | | ", //
" " };
System.out.println(顶部和底部);
(行)的流.forEachOrdered(r->System.out.println(leftSide+r+righside));
System.out.println(顶部和底部);
您可以使用数组。在Java8+中,还可以使用流
// Components of flag
String topAndBottom = "|---------------------------------------|";
String leftSide = "|**********";
String rightSide = "**********|";
String[] rows = { " ", //
" ^ ", //
" ^ /*\\ ^ ", //
" /*\\|*|/*\\ ", //
" . --*********-- . ", //
" \\*********/ ", //
" >*******< ", //
" *********** ", //
" --------- ", //
" | | ", //
" " };
System.out.println(topAndBottom);
Stream.of(rows).forEachOrdered(r -> System.out.println(leftSide + r + rightSide));
System.out.println(topAndBottom);
//标志的组件
字符串topAndBottom=“|------------------------------------------|”;
字符串leftSide=“|**********”;
字符串右侧=“*********|””;
字符串[]行={“”//
" ^ ", //
" ^ /*\\ ^ ", //
" /*\\|*|/*\\ ", //
" . --*********-- . ", //
" \\*********/ ", //
" >*******< ", //
" *********** ", //
" --------- ", //
" | | ", //
" " };
System.out.println(顶部和底部);
(行)的流.forEachOrdered(r->System.out.println(leftSide+r+righside));
System.out.println(顶部和底部);
欢迎使用Stackoverflow!您可能想检查哪一个更适合代码改进建议欢迎使用Stackoverflow!您可能想检查哪一个更适合代码改进建议为什么要麻烦流?每个循环的好旧版本有什么问题?为什么要麻烦流?好旧版本有什么问题对于每个循环?