Java练习1

最近在看毕向东的Java视频,想转行做一名Java开发者。

下面是视频中出现的编程练习:

1、用for循环打印输出如下图案
*****
****
***
**
*

代码:

 1 class Demo
 2 {
 3     public static void main(String[] args)
 4     {
 5         for(int i=0;i<5;i++) {
 6             for(int j=0;j<5-i;j++) {
 7                 System.out.print("*");
 8             }
 9             System.out.println();
10         }
11     }
12 }

运行结果:

2、用for循环打印输出如下图案
*
**
***
****
*****

代码:

 1 class Demo
 2 {
 3     public static void main(String[] args)
 4     {
 5         for(int i=0;i<5;i++) {
 6             for(int j=0;j<i+1;j++) {
 7                 System.out.print("*");
 8             }
 9             System.out.println();
10         }
11     }
12 }

运行结果:

3、使用for循环输出如下数字
54321
5432
543
54
5

代码:

 1 class Demo
 2 {
 3     public static void main(String[] args)
 4     {
 5         for(int i=0;i<5;i++) {
 6             for(int j=5;j>i;j--) {
 7                 System.out.print(j);
 8             }
 9             System.out.println();
10         }
11     }
12 }

运行结果:


4、使用for循环输出如下数字
1
22
333
4444
55555

代码:

 1 class Demo
 2 {
 3     public static void main(String[] args)
 4     {
 5         for(int i=1;i<=5;i++) {
 6             for(int j=i;j>0;j--) {
 7                 System.out.print(i);
 8             }
 9             System.out.println();
10         }
11     }
12 }

运行结果:

5、打印输出九九乘法表
1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9

……

代码:

 1 class Demo
 2 {
 3     public static void main(String[] args)
 4     {
 5         for(int i=1;i<=9;i++) {
 6             for(int j=1;j<=i;j++) {
 7                 System.out.print(j+"*"+i+"="+i*j+"\t");
 8             }
 9             System.out.println();
10         }
11     }
12 }

运行结果:

------------------------------------------------------------------2019.6.9-----------------------------------------------------------

猜你喜欢

转载自www.cnblogs.com/langdao/p/10995775.html