易学笔记--从0开始学JAVA(个人纯手工笔记共享 免费!免费!免费!)--第4章 控制执行流程
第4章 控制执行流程/4.1 true和false
标签: | 作者:易学笔记 更多资料请联系微信或QQ:1776565180 |
第4章 控制执行流程/4.2 if-else
标签: | 作者:易学笔记 更多资料请联系微信或QQ:1776565180 |
第4章 控制执行流程/4.3 迭代
标签: | 作者:易学笔记 更多资料请联系微信或QQ:1776565180 |
第4章 控制执行流程/4.3 迭代/4.3.3 逗号操作符
标签: | 作者:易学笔记 更多资料请联系微信或QQ:1776565180 |
-
- 主要用于for循环的初始化和步数修改
-
举例:
- 源代码:
- 程序输出结果:
第4章 控制执行流程/4.4 Foreach语法
标签: | 作者:易学笔记 更多资料请联系微信或QQ:1776565180 |
第4章 控制执行流程/4.5 break和continue
标签: | 作者:易学笔记 更多资料请联系微信或QQ:1776565180 |
-
源代码:
//: HelloDate.java import java.util.*; //import static net.mindview.until.Print.*; /** the first Thinking in java example program * display a string and today's date * @author wengyongsheng * @version 4.0 */ public class HelloDate{ /** * @param args array of string arguments */ public static void main(String []args) { System.out.println("Hello,It's: "); System.out.println(new Date()); // print("Hello,It's: "); } } class Tank{ int level; } class Assignment{ public static void main(String []args) { Tank t1 = new Tank(); Tank t2 = new Tank(); t1.level = 9; t2.level = 47; System.out.println("1:t1.level = " + t1.level + " t2.level = " + t2.level); t1 = t2; System.out.println("2:t1.level = " + t1.level + " t2.level = " + t2.level); t1.level = 27; System.out.println("3:t1.level = " + t1.level + " t2.level = " + t2.level); } } class Letter{ char c; } class PassObject{ static void f(Letter y){ y.c = 'z'; } public static void main(String []args) { Letter x = new Letter(); System.out.println("1:x.c= " + x.c); x.c = 'a'; System.out.println("2:x.c= " + x.c); f(x); System.out.println("3:x.c= " + x.c); } } class MathOps{ public static void main(String []args) { Random rand = new Random(47); int i , j , k; j = rand.nextInt(100) + 1; System.out.println("j = " + j); k = rand.nextInt(100) + 1; System.out.println("k = " + k); i = j + k; System.out.println("i = " + i); float u , v , w; u = rand.nextFloat(); System.out.println("u = " + u); v = rand.nextFloat() ; System.out.println("v = " + v); w = u + v; System.out.println("w = " + w); } } class AutoInc{ public static void main(String []args) { int i = 1; System.out.println("i = " + i); System.out.println("++i = " + ++i); System.out.println("i++ = " + i++); System.out.println("i2 = " + i); System.out.println("--i = " + --i); System.out.println("i-- = " + i--); System.out.println("i3 = " + i); } } /* output i = 1 ++i = 2 i++ = 2 i2=3 --i = 2 i-- = 2 i3=1 */ class Equaivalence{ public static void main(String []args) { Integer n1 = new Integer(47); Integer n2 = new Integer(47); System.out.println(n1 == n2); System.out.println(n1 != n2); } } class EqualsMethod{ public static void main(String []args) { Integer n1 = new Integer(47); Integer n2 = new Integer(47); System.out.println(n1.equals(n2)); } } class Value{ int i ; } class EqualsMethod2{ public static void main(String []args) { Value v1 = new Value(); Value v2 = new Value(); System.out.println(v1.equals(v2)); } } class ShortCircuit{ static boolean test1(int val){ System.out.println("test1( " + val + " )"); System.out.println("result: " + (val <1) ); return (val < 1); } static boolean test2(int val){ System.out.println("test2( " + val + " )"); System.out.println("result: " + (val <1) ); return (val < 2); } static boolean test3(int val){ System.out.println("test3( " + val + " )"); System.out.println("result: " + (val <1) ); return (val < 3); } public static void main(String []args) { boolean b = test1(0) && test2(2) && test2(3); System.out.println("expression is : " + b ); } } class CommaOperator{ public static void main(String []args) { for(int i = 1, j = i + 10; i < 5;i++,j = i*2){ System.out.println("i = " + i + " j = " + j); } } } class ForEachFloat{ public static void main(String []args) { Random rand = new Random(47); float f[] = new float[10]; for(int i = 0; i < 10;i++){ f[i] = rand.nextFloat(); } for(float x:f){ System.out.println("x = " + x); } for(char c:"my name".toCharArray()){ System.out.println("c = " + c); } } } class LableedWhile{ public static void main(String []args) { int i = 0; outer: while(true){ System.out.println("Outer while loop"); while(true){ i++; System.out.println("i = " + i); if(i == 1){ System.out.println("continue"); continue; } if(i == 3){ System.out.println("continue Outer"); continue outer; } if(i == 5){ System.out.println("break"); break; } if(i == 7){ System.out.println("break Outer"); break outer; } } } } }
- 输出结果:
第4章 控制执行流程/4.8 switch
标签: | 作者:易学笔记 更多资料请联系微信或QQ:1776565180 |
-
语法
-
- 格式:
- 注意:后面要有break
-
举例:
-
源代码:
//: HelloDate.java import java.util.*; //import static net.mindview.until.Print.*; /** the first Thinking in java example program * display a string and today's date * @author wengyongsheng * @version 4.0 */ public class HelloDate{ /** * @param args array of string arguments */ public static void main(String []args) { System.out.println("Hello,It's: "); System.out.println(new Date()); // print("Hello,It's: "); } } class Tank{ int level; } class Assignment{ public static void main(String []args) { Tank t1 = new Tank(); Tank t2 = new Tank(); t1.level = 9; t2.level = 47; System.out.println("1:t1.level = " + t1.level + " t2.level = " + t2.level); t1 = t2; System.out.println("2:t1.level = " + t1.level + " t2.level = " + t2.level); t1.level = 27; System.out.println("3:t1.level = " + t1.level + " t2.level = " + t2.level); } } class Letter{ char c; } class PassObject{ static void f(Letter y){ y.c = 'z'; } public static void main(String []args) { Letter x = new Letter(); System.out.println("1:x.c= " + x.c); x.c = 'a'; System.out.println("2:x.c= " + x.c); f(x); System.out.println("3:x.c= " + x.c); } } class MathOps{ public static void main(String []args) { Random rand = new Random(47); int i , j , k; j = rand.nextInt(100) + 1; System.out.println("j = " + j); k = rand.nextInt(100) + 1; System.out.println("k = " + k); i = j + k; System.out.println("i = " + i); float u , v , w; u = rand.nextFloat(); System.out.println("u = " + u); v = rand.nextFloat() ; System.out.println("v = " + v); w = u + v; System.out.println("w = " + w); } } class AutoInc{ public static void main(String []args) { int i = 1; System.out.println("i = " + i); System.out.println("++i = " + ++i); System.out.println("i++ = " + i++); System.out.println("i2 = " + i); System.out.println("--i = " + --i); System.out.println("i-- = " + i--); System.out.println("i3 = " + i); } } /* output i = 1 ++i = 2 i++ = 2 i2=3 --i = 2 i-- = 2 i3=1 */ class Equaivalence{ public static void main(String []args) { Integer n1 = new Integer(47); Integer n2 = new Integer(47); System.out.println(n1 == n2); System.out.println(n1 != n2); } } class EqualsMethod{ public static void main(String []args) { Integer n1 = new Integer(47); Integer n2 = new Integer(47); System.out.println(n1.equals(n2)); } } class Value{ int i ; } class EqualsMethod2{ public static void main(String []args) { Value v1 = new Value(); Value v2 = new Value(); System.out.println(v1.equals(v2)); } } class ShortCircuit{ static boolean test1(int val){ System.out.println("test1( " + val + " )"); System.out.println("result: " + (val <1) ); return (val < 1); } static boolean test2(int val){ System.out.println("test2( " + val + " )"); System.out.println("result: " + (val <1) ); return (val < 2); } static boolean test3(int val){ System.out.println("test3( " + val + " )"); System.out.println("result: " + (val <1) ); return (val < 3); } public static void main(String []args) { boolean b = test1(0) && test2(2) && test2(3); System.out.println("expression is : " + b ); } } class CommaOperator{ public static void main(String []args) { for(int i = 1, j = i + 10; i < 5;i++,j = i*2){ System.out.println("i = " + i + " j = " + j); } } } class ForEachFloat{ public static void main(String []args) { Random rand = new Random(47); float f[] = new float[10]; for(int i = 0; i < 10;i++){ f[i] = rand.nextFloat(); } for(float x:f){ System.out.println("x = " + x); } for(char c:"my name".toCharArray()){ System.out.println("c = " + c); } } } class LableedWhile{ public static void main(String []args) { int i = 0; outer: while(true){ System.out.println("Outer while loop"); while(true){ i++; System.out.println("i = " + i); if(i == 1){ System.out.println("continue"); continue; } if(i == 3){ System.out.println("continue Outer"); continue outer; } if(i == 5){ System.out.println("break"); break; } if(i == 7){ System.out.println("break Outer"); break outer; } } } } } class VoewlsAndConson{ public static void main(String []args) { Random rand = new Random(47); for(int i = 0; i < 100;i++){ int c = rand.nextInt(26) + 'a'; System.out.println("c = " + c); switch(c){ case 'a': case 'b': case 'c': System.out.println("vowel");break; case 'y': case 'w': case 'z':System.out.println("consonant");break; default:System.out.println("xxxx"); } } } }
- 输出结果:
-
源代码:
-