Java基本控制流程的理解之输出三角形和乘法表
我是一个刚刚接触JAVA的小白,对它比较感兴趣,就花了两天时间看看书,所以非常希望和他人交流,更好的去理解它。
首先说说对作业的理解:
页面上有什么(what):
对象:空格、星号和换行符;
对象在什么时候出现和结束(where):根据行和列确定
怎样来实现对象的定位(how):
1、遍历每一行----从而找到行
2、遍历每一列----从而找到列
在不同列根据对象出现的位置,来确定输出什么对象
如果要看答案的话其实直接看answer1、answer2、answer3、answer4、answer5这几个方法就行了,主要是理解注释和思考模式,因为每个人都有自己的想法,我把想法分享给大家,希望大家多评论,这样大家都会有启发,共同进步嘛!代码可以复制到自己的IDE里面编译运行。
1 /** 2 * 这里是文档注释 3 */ 4 package task; 5 6 import java.util.Scanner; 7 8 /** 9 * @author AsceticJ 10 * @date 2017年3月3日 下午7:48:29 11 * @version 1.1 12 13 * @TODO Description 14 */ 15 public class Day001 16 { 17 public static void myTest(Test a,int n) 18 { 19 a.answer1(n); 20 System.out.println("--------我是华丽的分割线--------"); 21 a.answer2(n); 22 System.out.println("--------我是华丽的分割线--------"); 23 a.answer3(n); 24 System.out.println("--------我是华丽的分割线--------"); 25 a.answer4(n); 26 System.out.println("--------我是华丽的分割线--------"); 27 } 28 public static void main(String[] args) 29 { 30 Scanner scan = new Scanner(System.in);//用户输入值 31 int n = scan.nextInt();//获取用户输入的整型数 32 Test quetion = new Test(); 33 TestOther queOther = new TestOther(); 34 myTest(quetion, n); 35 myTest(queOther, n); 36 quetion.answer5(); 37 scan.close(); 38 System.out.println("这是我的答案和解题思想,欢迎大家和我一起交流学习。"); 39 } 40 } 41 class Test{ 42 // question 1 43 public void answer1(int n){ 44 for (int i = 0; i < n; i++)//换行符插入:一共有n行,每行末尾插入换行符 45 { 46 for (int j = 0; j <= i; j++)//*插入的:第i行有i列,每行开始就插入,第i列结束 47 { 48 System.out.print("*"); 49 } 50 System.out.println("");//一行结束,输出换行 51 } 52 } 53 // question 2 54 public void answer2(int n){ 55 // 1、当参数表示行号 56 if(n<2){ 57 System.out.println("你是要一颗星的三角形吗? 喏~ 给你:*"); 58 return; 59 } 60 for (int i = 1; i <= n; i++) 61 { 62 for (int j = 1; j <= 2*n-1; j++)//最多有2n-1列,第i行有2i-1列 63 { 64 if(j<=n-i){//计算空格前面空格数,(2n-1-(2i-1))除以2---即总列数减*数除2 65 System.out.print(" ");//空格插入:从每行开始插入,到n-i结束 66 }else if(j < n+i){//计算*结束位置,n-i+(2i-1)----即前面空格数加上*数 67 System.out.printf("*"); 68 } 69 } 70 System.out.println("");//一行结束,输出换行 71 } 72 73 // 2、当参数表示最后一行星的个数 里面的计算方法一样故不做注释了 74 // if (1==n%2&&1!=n) 75 // { 76 // for (int i = 1; i <= (n+1)/2; i++) 77 // { 78 // for (int j = 1; j <= n; j++) 79 // { 80 // if (j<=(n+1-2*i)/2) 81 // { 82 // System.out.print(" "); 83 // }else if(j<=(n-1+2*i)/2){ 84 // System.out.print("*"); 85 // } 86 // } 87 // System.out.println(""); 88 // } 89 // }else { 90 // System.out.println("您输入的数字不能构成等腰三角形哟^_^"); 91 // } 92 } 93 // question 3 94 public void answer3(int n){ 95 // 1、当参数表示行号 96 if(n<2){ 97 System.out.println("你是要一颗星的三角形吗? 喏~ 给你:*"); 98 return; 99 } 100 for (int i = n; i > 0 ; i--) 101 { 102 for (int j = 1; j <= 2*n-1; j++) 103 { 104 if(j<=n-i){ 105 System.out.print(" "); 106 }else if(j < n+i){ 107 System.out.printf("*"); 108 } 109 } 110 System.out.println(""); 111 } 112 // 2、同answer2类似 就不编写了 113 } 114 // question 4 115 public void answer4(int n){ 116 //这里的n指的是总行数的一半 117 //方式1 这样存在一个问题,中间存在两行底边 你可以将前面的代码修改少输出一行就是了,然后拼接 我就不拼了 118 answer2(n); 119 answer3(n); 120 System.out.println("------------------------------------------"); 121 //方式2 122 if(n<3||0==n%2){ 123 System.out.println("泥揍开,你丫的根本不是想要菱形!"); 124 return; 125 } 126 // 下面继续 原理一样 127 for (int i = 1; i <= n; i++) 128 { 129 if(i<=(n+1)/2){ 130 for (int j = 1; j <= n; j++) 131 { 132 if(j<=(n+1-2*i)/2){ 133 System.out.print(" "); 134 }else if(j < (n+2*i+1)/2){ 135 System.out.printf("*"); 136 } 137 } 138 }else { 139 for (int j = 1; j <= n; j++) 140 { 141 if(j<=(2*i-n-1)/2){ 142 System.out.print(" "); 143 }else if(j <= (3*n-2*i+1)/2){ 144 System.out.printf("*"); 145 } 146 } 147 } 148 System.out.println(""); 149 } 150 } 151 // question 5 同样是行列操作的问题 152 public void answer5(){ 153 for(int i = 1; i <=9; i++) { 154 for (int j = 1; j <=i; j++) { 155 if (i<5&&3==j) 156 { 157 System.out.print(" "+j+"*"+i+"="+i*j+" "); //为了解决前面数值均为个位数占位没对齐的问题 158 }else { 159 System.out.print(j+"*"+i+"="+i*j+" "); 160 } 161 } 162 System.out.println(""); 163 } 164 } 165 166 } 167 168 //鉴于有人说之前的和要求不符,便写了新的 其实后面将*和空格连在一起就是了 里面我就懒得注释了 169 class TestOther extends Test 170 { 171 public void answer1(int n){ 172 for (int i = 0; i <= n; i++) 173 { 174 for (int j = 0; j < i; j++) 175 { 176 System.out.print("* "); 177 } 178 System.out.println(""); 179 } 180 } 181 182 public void answer2(int n){ 183 if(n<2){ 184 System.out.println("你是要一颗星的三角形吗? 喏~ 给你:*"); 185 return; 186 } 187 for (int i = 1; i <= n; i++) 188 { 189 for (int j = 1; j <= 2*n-1; j++) 190 { 191 if(j<=n-i){ 192 System.out.print(" "); 193 }else if(j <= n){ 194 System.out.printf("* "); 195 } 196 } 197 System.out.println(""); 198 } 199 } 200 201 public void answer3(int n){ 202 if(n<2){ 203 System.out.println("你是要一颗星的三角形吗? 喏~ 给你:*"); 204 return; 205 } 206 for (int i = n; i >0; i--) 207 { 208 for (int j = 1; j <= 2*n-1; j++) 209 { 210 if(j<=n-i){ 211 System.out.print(" "); 212 }else if(j <= n){ 213 System.out.printf("* "); 214 } 215 } 216 System.out.println(""); 217 } 218 } 219 220 public void answer4(int n){ 221 if(n<3||0==n%2){ 222 System.out.println("泥揍开,你丫的根本不是想要菱形!"); 223 return; 224 } 225 for (int i = 1; i <= n; i++) 226 { 227 if(i<=(n+1)/2){ 228 for (int j = 1; j <= n; j++) 229 { 230 if(j<=(n+1-2*i)/2){ 231 System.out.print(" "); 232 }else if(j <= (n+1)/2){ 233 System.out.printf("* "); 234 } 235 } 236 }else { 237 for (int j = 1; j <= n; j++) 238 { 239 if(j<=(2*i-n-1)/2){ 240 System.out.print(" "); 241 }else if(j <= (n+1)/2){ 242 System.out.printf("* "); 243 } 244 } 245 } 246 System.out.println(""); 247 } 248 } 249 250 }