所有条件语句都利用条件表达式的真假来决定执行路径
结构
if(Boolean expression) statement else if(Boolean expression) statement else statement
结构
while(Boolean expression) statement
规则
Test
package one; import static util.StaticPrint.*; public class TestIteration { static boolean condition(){ boolean res = Math.random() < 0.99 ; print(res+""); return res; } public static void main(String[] args) { while (condition()){ print("inside"); } print("exite"); } }
结构
do statement while(Boolean expression)
规则
结构
label: for(initialization;Boolean expression;step) statement
规则
Test
<!--使用循环嵌套和label标签实现输出100以内的素数-->
package four; import static util.StaticPrint.*; public class PrimeNumber { public static void main(String[] args) { outer:for (int i = 1 ; i < 100 ; i++ ){ inter:for (int j = 2 ; j < i ; j++ ){ if ( i%j == 0){ // 退出到指定的循环 continue outer; } } print(i+""); } } }
结构
for(int i = 1, j = 2 ; ;) statement
规则
结构
for(type param : params) statement
规则
Test
package five; import static util.StaticPrintnb.*; public class TestForeach { public static void main(String[] args) { for (char c:"You konw what I mean".toCharArray() ) { printnb(c+""); } } }
用途
规则
Test
<!--直接退出三层循环-->
package seven; public class TestReturn { public static void main(String[] args) { for(;;){ for (;;){ for (int i = 0 ; i < 100 ; i++ ){ System.out.println(i); return; } } } } }
规则
GOTO用法
规则
GOTO用法
Test
package four; import static util.StaticPrint.*; public class PrimeNumber { public static void main(String[] args) { outer:for (int i = 1 ; i < 100 ; i++ ){ inter:for (int j = 2 ; j < i ; j++ ){ if ( i%j == 0){ // 退出到指定的循环 continue outer; } } print(i+""); } } }
简介
switch为选择语句
结构
switch(integral-selector){ case integral-value1:statement;break; case integral-value2:statement;break; case integral-value3:statement;break; case integral-value4:statement;break; default:statement; }
规则
Test
public class RunSwitch { public static void main(String[] args) { int i = 2; switch (i){ case 0: System.out.println(0); case 1: System.out.println(1); case 2: System.out.println(2); case 3: System.out.println(3); } } }/* output 2 3 */
题目
吸血鬼数字是指位数为偶数的数字,可以由一堆数字想乘而得到。而这对数字各包含乘积的一半位数的数字,其中从最初的数字中选取的数字可以任意排序。以两个0结尾的数字是不允许的,例如,下列的数字都是“吸血鬼”数字:
1260=21*60
1827=21*87
2187=27*81
写出一个程序,找出4位数的所有吸血鬼数字
思路
个人思路
官方思路
优势
实现
个人思路
package ten; import java.util.*; public class VampireNumberImp { static int VampireJudgeImp(){ List<Integer> VampireArray = new ArrayList<Integer>(); List<Character> ikList = new ArrayList<Character>(); List<Character> sumList = new ArrayList<Character>(); int point = 0; for (int i = 11 ; i < 100 ; i++ ){ for (int k = i ; k < 100 ; k++){ int sum = i * k; // 判断是否为四位数,判断是否为两个零结尾 if ( sum < 1000 || sum > 9999 || sum % 100 == 0 || VampireArray.contains(sum)){ continue; } point++; // 判断是否为吸血鬼数字 // 将数字添加进list ikList.add((char) (i / 10)); ikList.add((char) (i % 10)); ikList.add((char) (k / 10)); ikList.add((char) (k % 10)); sumList.add((char) (sum / 1000)); sumList.add((char) (sum % 1000 / 100)); sumList.add((char) (sum % 1000 % 100 / 10)); sumList.add((char) (sum % 10)); // 数字排序 Collections.sort(ikList); Collections.sort(sumList); // 判断是否为吸血鬼数字 if (ikList.equals(sumList)){ VampireArray.add(sum); System.out.println(sum); } ikList.clear(); sumList.clear(); } } return point; } public static void main(String[] args) { long startTime=System.currentTimeMillis(); int point = VampireJudgeImp(); // 测试代码执行时间 long endTime=System.currentTimeMillis(); System.out.println("程序运行时间: "+(endTime - startTime)+"ms"); System.out.println("程序运行次数: "+point+"次"); } }/* output 1395 1260 1827 2187 1530 1435 6880 程序运行时间: 19ms 程序运行次数: 3210次 */
官方思路
package ten; public class VampireNumberOfficial { static int a(int i) { return i/1000; } static int b(int i) { return (i%1000)/100; } static int c(int i) { return ((i%1000)%100)/10; } static int d(int i) { return ((i%1000)%100)%10; } static int com(int i, int j) { return (i * 10) + j; } static void productTest (int i, int m, int n) { if(m * n == i) System.out.println(i + " = " + m + " * " + n); } public static void main(String[] args) { long startTime=System.currentTimeMillis(); for(int i = 1001; i < 9999; i++) { productTest(i, com(a(i), b(i)), com(c(i), d(i))); productTest(i, com(a(i), b(i)), com(d(i), c(i))); productTest(i, com(a(i), c(i)), com(b(i), d(i))); productTest(i, com(a(i), c(i)), com(d(i), b(i))); productTest(i, com(a(i), d(i)), com(b(i), c(i))); productTest(i, com(a(i), d(i)), com(c(i), b(i))); productTest(i, com(b(i), a(i)), com(c(i), d(i))); productTest(i, com(b(i), a(i)), com(d(i), c(i))); productTest(i, com(b(i), c(i)), com(d(i), a(i))); productTest(i, com(b(i), d(i)), com(c(i), a(i))); productTest(i, com(c(i), a(i)), com(d(i), b(i))); productTest(i, com(c(i), b(i)), com(d(i), a(i))); } long endTime=System.currentTimeMillis(); System.out.println("程序运行时间: "+(endTime - startTime)+"ms"); } }/*output 1260 = 21 * 60 1395 = 15 * 93 1435 = 41 * 35 1530 = 51 * 30 1827 = 87 * 21 2187 = 27 * 81 6880 = 86 * 80 6880 = 80 * 86 程序运行时间: 54ms */