练习:嵌套循环 for-for、for循环、while循环
package ShangXueTang;
/*
* 练习:嵌套循环 for-for、for循环、while循环
*
*/
public class Test_01 {
public static void main(String[] args) {
//练习1:
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 5; j++) {
// System.out.print(j +"\t");
// [ \t隔开,\n换行 ]
System.out.print(i + "\t");
}
System.out.println();// print-ln换行
}
System.out.println("======================================================================");
// 练习2:九九乘法表
for (int c = 1; c <= 9; c++) {
for (int d = 1; d <= c; d++) {
System.out.print(d + "*" + c + "=" + (d * c) + "\t");
}
System.out.println();
}
System.out.println("======================================================================");
// 练习3:用for循环分别计算100以内的奇数及偶数的和,并输出
// 解决办法:分三步【1.如何获取100以内的数 2.奇数偶数的判读 3.奇数和/偶数和】
int sum01 = 0;
int sum02 = 0;
for (int i = 1; i <= 100; i++) {
if (i % 2 == 0) {
sum01 += i;
} else {
sum02 += i;
}
}
System.out.println("偶数和" + sum01);
System.out.println("奇数和" + sum02);
System.out.println("======================================================================");
// 练习4:用while循环或其它循环输出1-100之间能被5整除的数,且每行输出5个
// 方法二:计时器
int h = 0;
for (int i = 1; i <= 100; i++) {
if (i % 5 == 0) {
System.out.print(i + "\t");
h++;
}
if (h == 5) {
System.out.println();
h = 0;//控制每行输出5个
}
/*方法一:每次被25整除,就换行
if(i%25==0){
System.out.println();
*/
}
}
}
