打印一个底边为17的杨辉三角形,可以修改参数n=17为其它的值试试效果;还有个例子是三个数按从小到大排序输出。效果和源代码如下:
输出结果:
x=1
x=2
*
***
*****
*
***
*****
*******
*********
***********
*************
***************
*****************
11
21
31
位数:4
3 1 3 2
源代码:
package chapter03.forflow;
class For_sample {
public static void main(String args[]) {
for (int x = 1; x < 3; x++) {
System.out.println("x=" + x);
}
System.out.println(" *");
System.out.println(" ***");
System.out.println("*****");
int n= 17; //打印一个底边为17的杨辉三角形
for (int i=1;i<=n;i=i+2){
for(int k=n;k>i;k=k-2){
System.out.print(' ');
}
for(int j=1; j<=i;j++){
System.out.print('*');
}
System.out.print('\n');
}
int x = 31;
int y = 21;
int z = 11;
int t,tt,ttt;
t = x<y ? x:y;
tt = t < z ? t : z;
ttt = x>y ? x:y;
ttt = ttt>z ? ttt:z;
System.out.println(tt);
System.out.println(t);
System.out.println(ttt); //对三个数进行从小到大排序并打印出来
int num = 3132; //将一个整数分开打印
if( num > 99999 || num < 0){
System.out.println("范围不准确");
}
String s = ""+num;
//String s = String.valueOf(num);
char c[] = s.toCharArray();
System.out.println("位数:"+c.length);
for(int i = 0; i < c.length; i++){
System.out.print(c[i]);
System.out.print(" ");
}
}
}