

代码:
public class Test6 { public static void main(String[] args) {
printX(10); } /* 2.定义printX方法,参数为(int m) , 返回值无 3.printX方法中,使用for循环,初始化变量x = 1,如果x<=m进入循环,步进表达式x++ 4.for循环内部,再嵌套定义一套for循环,初始化变量y = 1,如果y<=m进入循环,步进表达式y++ 5.在内循环内部,判断x==y 或者 x+y==1+m ,则打印"O",否则打印"*" */ public static void printX(int m) {
for (int x = 1; x <= m; x++) {//循环7行 for (int y = 1; y <= m; y++) {//循环7列 if (x == y || x + y == m + 1) {//对角线打印O System.out.print("O"); } else { System.out.print("*");//其他位置打印. } } System.out.println();//换行 } } } |