标签:next 菱形 exti div pre == 空格 color height
二重循环之打印图形例解
输出一个等腰三角形
等腰三角形:代码如下
1 package cn.happy.shape; 2 3 import java.util.Scanner; 4 5 public class Day04Equal { 6 7 /** 8 * 等腰三角形 9 * 10 *** 11 ***** 12 ******* 13 ********* 14 15 i(行数) j(空格个数) k(*号个数) 16 1 4 1 17 2 3 3 18 3 2 19 4 1 20 5 0 21 22 i和j 23 i+j=5; 24 j<=5-i 25 26 i和k 27 k<=2*i-1 28 */ 29 public static void main(String[] args) { 30 System.out.println("您输入个数字吧!"); 31 Scanner input=new Scanner(System.in); 32 int num = input.nextInt(); 33 34 for (int i = 1; i <=num; i++) { 35 //第一个循环 控制空格数量 36 for (int j = 1; j <=num-i; j++) { 37 System.out.print(" "); 38 } 39 40 41 //第二个循环,控制 * 个数 42 for (int k = 1; k <=2*i-1; k++) { 43 44 System.out.print("*"); 45 } 46 System.out.println(); 47 48 } 49 50 } 51 52 }
2、打印一个到三角形
1 package cn.happy.shape; 2 3 import java.util.Scanner; 4 5 public class Day03reverseThreeShape { 6 /* 7 * 倒直角三角形 8 ***** 9 **** 10 *** 11 ** 12 * 13 i(行数) j(*个数) 14 1 5 15 2 4 16 3 3 17 4 2 18 5 1 19 关系:j<=6-i 20 21 * 22 * 23 */ 24 public static void main(String[] args) { 25 System.out.println("爷爷,您输入一个数字吧!"); 26 Scanner input=new Scanner(System.in); 27 int num = input.nextInt(); 28 for (int i = 1; i <=num; i++) { 29 //2.内层循环 每一行打印几个 * 30 for (int j = 1; j <=num+1-i; j++) { 31 System.out.print("*"); 32 } 33 System.out.println(); 34 } 35 } 36 }
3、打印一个直角三角形
1 package cn.happy.shape; 2 3 public class Day02triggleThreeShape { 4 5 /** 6 * 直角三角形 7 * 8 * 9 *** 10 ***** 11 ******* 12 ********* 13 i(行数 ) j(*个数) 14 1 1 15 2 3 16 3 5 17 4 7 18 5 9 19 20 */ 21 public static void main(String[] args) { 22 //1.找行数 5行 23 for (int i = 1; i <=5; i++) { 24 //2.内层循环 每一行打印几个 * 25 for (int j = 1; j <=2*i-1; j++) { 26 System.out.print("*"); 27 } 28 System.out.println(); 29 } 30 } 31 32 }
4、打印菱形
1 package cn.happy.shape; 2 3 import java.util.Scanner; 4 5 public class Day05LX { 6 7 /** 8 * 菱形 9 * 10 *** 11 ***** 12 ******* 13 ***** 14 *** 15 * 16 上半部分 17 i(行数) j(空格数) k(*个数) 18 1 3 1 19 2 2 3 20 3 1 5 21 4 0 7 22 23 i+j=4 24 j<=4-i; 25 26 下半部分 27 i和k 28 k<=2*i-1; 29 i(行数) j(空格) k(*个数) 30 1 1 5 31 2 2 3 32 3 3 1 33 34 35 i和j 36 j<=i-1 37 38 i和k关系 39 k<=7-2*i 40 41 42 43 44 */ 45 public static void main(String[] args) { 46 System.out.println("请输入一个奇数"); 47 Scanner input=new Scanner(System.in); 48 int num = input.nextInt(); //7 49 while(num%2==0){ 50 System.out.println("请输入奇数"); 51 num = input.nextInt(); 52 } 53 int topNum=(num+1)/2; 54 for (int i =1; i <=topNum; i++) { 55 //控制空格 56 for (int j = 1; j <=topNum-i; j++) { 57 System.out.print(" "); 58 } 59 for (int k = 1; k <=2*i-1; k++) { 60 System.out.print("*"); 61 } 62 System.out.println(); 63 } 64 int bottomNum=num-topNum; 65 for (int i =1; i <=bottomNum; i++) { 66 //控制空格 67 for (int j = 1; j <=i; j++) { 68 System.out.print(" "); 69 } 70 for (int k = 1; k <=num-2*i; k++) { 71 System.out.print("*"); 72 } 73 System.out.println(); 74 } 75 76 } 77 78 }
标签:next 菱形 exti div pre == 空格 color height
原文地址:http://www.cnblogs.com/xieYueLanShan/p/6682337.html