0%
第一题:
打印心形,中心有 I Love Java 字样
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
public class ILoveJava { public static void main(String[] args){
System.out.println(" * * * * * * "); System.out.println(" * * * "); System.out.println(" * I Love Java * "); System.out.println(" * * "); System.out.println(" * * "); System.out.println(" * * "); System.out.println(" * * "); System.out.println(" * * "); System.out.println(" * * "); System.out.println(" * "); } }
|
第二题
打印20*8的矩形
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
public class juxing { public static void main(String[] args){ int i = 8; int j = 20; int k = 0; int l = 0; for(; l < j; l++){ for(; k < i; k++){ System.out.print("*"); } System.out.print("\n"); k = 0; } } }
|
第三题
打印高度为5的等腰三角形
1 2 3 4 5 6
| * *** ***** ******* *********
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
|
public class triangle { public static void main(String[] args){ int i , j , k; for(i = 1; i <= 9 ; i+=2){ for(k = 0;k < (9-i)/2 ; k++ ){ System.out.print(" "); } for(j = 0 ; j < i ; j++){ System.out.print("*"); } System.out.print("\n"); } } }
|
第四题
声明两个double型变量var1和var2并赋值. 然后再用var1保存两者之和, 再用var2保存两者之差, 分析过程
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
public class three { public static void main(String[] args){ double var1 = 357878.1544d; double var2 = 11454.64785d; var1 = var1 + var2; var2 = var1 - var2 * 2; System.out.println("两者之和是:"+var1); System.out.println("两者之差是:"+var2); } }
|