Day4 循环结构While语句的练习2017-02-22
class Test1_While { public static void main(String[] args) { //求1-100的和 int sxhh = 0; int i = 0; while (i <= 100) { sxhh = sxhh + i; i++; System.out.println(sxhh); } System.out.println(sxhh + "======================================="); //统计100-999内水仙花有多少 个 int ok = 0; int a =100; while (a<=999) { int ge = a % 10; int shi = a / 10 % 10; int bai = a /10 /10 % 10; if (ge*ge*ge+shi*shi*shi+bai*bai*bai == a) { ok++; System.out.println(ok); } a++; } System.out.println("水仙花一共有:" + ok ); } } |
Day4 循环结构 While的语句格式和基本使用2017-02-22
class Demo1_While { public static void main(String[] args) { int i = 0; while (i <= 10) { System.out.println("Hello World!" + i); i++; } } } /* while循环的基本格式: while(判断条件语句) { 循环体语句; } 完整格式: 初始化语句; while(判断条件语句) { 循环体语句; 控制条件语句; } a:执行初始化语句 b:执行判断条件语句,看其返回值是true还是false 如果是true,就继续执行 如果是false,就结束循环 c:执行循环体语句; d:执行控制条件语句 e:回到B继续。 */ |
Day4 笔记 循环结构For语句的练习之统计思想(计数器)2017-02-21
class Test3_Flower { public static void main(String[] args) { int huas = 0; for (int i = 100;i <= 999 ;i++ ) { //获取100到999之间的数 int ge = i % 10; //123 % 10 int shi = i /10 % 10; //12 % 10 int bai = i /10 / 10 %10; // 1 % 10 if (ge * ge * ge + shi * shi * shi + bai* bai *bai == i ) { System.out.println(i); huas = huas + 1 ; } } System.out.println("水仙花数有" + huas); System.out.println("Hello World!"); } } |
Day4 笔记 循环结构 for语句的练习之水仙花2017-02-21
/* 案例演示 需求:在控制台输出所有的”水仙花数” 所谓的水仙花数是指一个三位数,其各位数字的立方和等于该数本身。 举例:153就是一个水仙花数。 153 = 111 + 555 + 333 = 1 + 125 + 27 = 153 分析: 1,100 - 999 2,获取每一个位数的值,百位,十位,个位 3.判断各个位上的立方和是否等于这个数,如果等于打印 */ class Test3_Flower { public static void main(String[] args) { for (int i = 100;i <= 999 ;i++ ) { //获取100到999之间的数 int ge = i % 10; //123 % 10 int shi = i /10 % 10; //12 % 10 int bai = i /10 / 10 %10; // 1 % 10 if (ge * ge * ge + shi * shi * shi + bai* bai *bai == i ) { System.out.println(i); } } System.out.println("Hello World!"); } } |
Day4 练习 循环结构for语句的练习之求和思想2017-02-20
class Test2_For { public static void main(String[] args) { /* A:案例演示 需求:求出1-10之间数据之和 int abc = 0; for (int i = 1;i <= 10 ;i++ ) { abc = abc + i; System.out.println("计算中:" + abc); } System.out.println("和:" + abc);*/ /* B:学生练习 需求:求出1-100之间偶数和 int abc = 0; for (int i = 1;i <= 100 ;i++ ) { if (i % 2 == 0) { abc = abc + i; } System.out.println("计算中:" + abc); } System.out.println("偶数和:" + abc);*/ /*需求:求出1-100之间奇数和 */ int abc = 0; for (int i = 1;i <= 100 ;i++ ) { if (i % 2 != 0) { abc = abc + i; } System.out.println("计算中:" + abc); } } } |
下一页
上一页