...Java课的作业,没有用到什么算法
package edu.fjnu.Harry ;
/**
* @author Harry
*
*/
public class Demo_test2 {
/**
* 编写Java程序,分别使用for循环和while循环打印菱形。
*
*/
/**
* 此函数为用for循环打印菱形
* @param num
* @author Harry
*/
public static void forDiamond (int num) {
for(int i = 1 ;i
for(int j =1;j
System.out.print(" ");
}
for(int k = 0;k< 2*i-1;k++){ //每一行打印几个星号
System.out.print("*");
}
System.out.println();// 进行换行
}
for(int i = 1;i
for(int j = 0;j
System.out.print(" ");
}
for(int k = 0;k
System.out.print("*");
}
System.out.println();
}
}
/**
* 此函数为用while循环打印菱形
* @param num
* @author Harry
*/
public static void whileDiamond (int num) {
int i = 1;
while(i
int j = 1;
while(j
System.out.print(" ");
j++;
}
int k= 0;
while(k<2*i-1) {
System.out.print("*");
k++;
}
i++;
System.out.println();
}
int i1 = 1;
while(i1
int j1 =0;
while(j1
System.out.print(" ");
j1++;
}
int k1 =0;
while(k1
System.out.print("*");
k1++;
}
i1++;
System.out.println();
}
}
public static void main(String[] args) {
System.out.println("_________________________");
int num = 5;
forDiamond(num);
System.out.println("以上是用for循环打印菱形");
System.out.println("_________________________");
System.out.println("_________________________");
whileDiamond(num);
System.out.println("以上是用while循环打印菱形");
System.out.println("_________________________");
}
}