Java编程思想_示例代码_第04章_控制执行流程_Controlling Execution

ExamleCode_01

package control;

//: control/IfElse.java

import static net.mindview.util.Print.*;

public class IfElse {
	
	static int result = 0;
	
	static void test(int testval, int target) {
		if(testval > target)
			result = +1;
		else if(testval < target)
			result = -1;
		else
			result = 0; // Match
  }
	
	public static void main(String[] args) {
		test(10, 5);
		print(result); //Output: 1
		test(5, 10);
		print(result); //Output: -1
		test(5, 5);
		print(result); //Output: 0
	}
	
} /* Output:
1
-1
0
*///:~

ExamleCode_02

package control;

//: control/WhileTest.java
//  Demonstrates the while loop  //演示while循环

public class WhileTest {
	
	static boolean condition() {
		boolean result = Math.random() < 0.99;
		System.out.print(result + ", ");
		return result;
	}
	
	public static void main(String[] args) {
		while(condition())
			System.out.println("Inside 'while'");
		System.out.println("Exited 'while'");
	}
	
} /* (Execute to see output) *///:~
  /*(执行以查看输出)*/

ExamleCode_03

package control;

//: control/ListCharacters.java
// 	Demonstrates "for" loop by listing all the lowercase ASCII letters  
//	通过列出所有小写ASCII字母来演示“for”循环

public class ListCharacters {
	
	public static void main(String[] args) {
		for(char c = 0; c < 128; c++) {
			if(Character.isLowerCase(c)) {
				System.out.println("value: " + (int)c +" character: " + c);
				//判断字符是否为小写字母
				//其中0到128之间,从97开始才是小写字母,前面的都不会进入if循环体内
			}				
		}
	}
	
} /* Output:
value: 97 character: a
value: 98 character: b
value: 99 character: c
value: 100 character: d
value: 101 character: e
value: 102 character: f
value: 103 character: g
value: 104 character: h
value: 105 character: i
value: 106 character: j
...
*///:~

ExamleCode_04

package control;

//: control/CommaOperator.java //Comma 逗号

public 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);
		}
	}
	
} /* Output:
i = 1 j = 11
i = 2 j = 4
i = 3 j = 6
i = 4 j = 8
*///:~

ExamleCode_05

package control;

//: control/ForEachFloat.java

import java.util.*;

public 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
	}
	
} /* Output:
0.72711575
0.39982635
0.5309454
0.0534122
0.16020656
0.57799757
0.18847865
0.4170137
0.51660204
0.73734957
*///:~

ExamleCode_06

package control;

//: control/ForEachString.java

public class ForEachString {
	
	public static void main(String[] args) {
		for(char c : "An African Swallow".toCharArray() )
			System.out.print(c + " ");
	}
	
} /* Output:
A n   A f r i c a n   S w a l l o w
*///:~

ExamleCode_07

package control;

//: control/ForEachInt.java

import static net.mindview.util.Range.*;
import static net.mindview.util.Print.*;

public class ForEachInt {
	
	public static void main(String[] args) {
  	 
		for(int i : range(10)) // 0..9
			printnb(i + " ");
		print();
  	 
		for(int i : range(5, 10)) // 5..9
			printnb(i + " ");
		print();
		
		for(int i : range(5, 20, 3)) // 5..20 step 3
			printnb(i + " ");
		print();
	}
	
} /* Output:
0 1 2 3 4 5 6 7 8 9
5 6 7 8 9
5 8 11 14 17
*///:~

ExamleCode_08

package control;

//: control/IfElse2.java

import static net.mindview.util.Print.*;

public class IfElse2 {
	
	static int test(int testval, int target) {
		if(testval > target)
			return +1;
		else if(testval < target)
			return -1;
		else
			return 0; // Match
	}	
	
	public static void main(String[] args) {
		print(test(10, 5 ));  //Output: 1
		print(test(5 , 10));  //Output: -1
		print(test(5 , 5 ));  //Output: 0
	}
	
} /* Output:
1
-1
0
*///:~

ExamleCode_09

package control;

//: control/BreakAndContinue.java
//  Demonstrates break and continue keywords.  //演示break和continue关键字

import static net.mindview.util.Range.*;

public class BreakAndContinue {
	
	public static void main(String[] args) {
		for(int i = 0; i < 100; i++) {
			if(i == 74) 
				break; // Out of for loop
			if(i % 9 != 0)
				continue; // Next iteration
			System.out.print(i + " ");
    }
		
    System.out.println();
    
    // Using foreach:
    for(int i : range(100)) {
    	if(i == 74)
    		break; // Out of for loop
    	if(i % 9 != 0) 
    		continue; // Next iteration
      System.out.print(i + " ");
    }
    
    System.out.println();
    
    // An "infinite loop":
    int i = 0;
    while(true) {
    	i++;
    	int j = i * 27;
    	if(j == 1269) 
    		break;    // Out of loop
    	if(i % 10 != 0) 
    		continue; // Top of loop
    	System.out.print(i + " ");
    }
    
  }
} /* Output:
0 9 18 27 36 45 54 63 72
0 9 18 27 36 45 54 63 72
10 20 30 40
*///:~

ExamleCode_10

package control;

//: control/LabeledFor.java  //labeled 标记
//	For loops with "labeled break" and "labeled continue."
//	有“标记中断”和“标记为继续”的循环。

import static net.mindview.util.Print.*;

public class LabeledFor {
	
	public static void main(String[] args) {
		int i = 0;
		outer: // Can't have statements here //这里不能有语句
			for(; true ;) { // infinite loop
				inner: // Can't have statements here //这里不能有语句
					for(; i < 10; i++) {
						print("i = " + i);
						if(i == 2) {
							print("continue");
							continue;
						}
						if(i == 3) {
							print("break");
							i++; // Otherwise i never
							// gets incremented.
							break;
						}
						if(i == 7) {
							print("continue outer");
							i++; // Otherwise i never
							// gets incremented.
							continue outer;
						}
						if(i == 8) {
							print("break outer");
							break outer;
						}
						for(int k = 0; k < 5; k++) {
							if(k == 3) {
								print("continue inner");
								continue inner;
							}
						}
					}
			}
		// Can't break or continue to labels here
	}
	
} /* Output:
i = 0
continue inner
i = 1
continue inner
i = 2
continue
i = 3
break
i = 4
continue inner
i = 5
continue inner
i = 6
continue inner
i = 7
continue outer
i = 8
break outer
*///:~

ExamleCode_11

package control;

//: control/LabeledWhile.java
// 	While loops with "labeled break" and "labeled continue."
//	带有“标记为中断”和“标记为继续”的while循环

import static net.mindview.util.Print.*;

public class LabeledWhile {
	
	public static void main(String[] args) {
		int i = 0;
		outer:
			while(true) {
				print("Outer while loop");
				while(true) {
					i++;
					print("i = " + i);
					if(i == 1) {
						print("continue");
						continue;
					}
					if(i == 3) {
						print("continue outer");
						continue outer;
					}
					if(i == 5) {
						print("break");
						break;
					}
					if(i == 7) {
						print("break outer");
						break outer;
					}
				}
			}
	}
	
} /* Output:
Outer while loop
i = 1
continue
i = 2
i = 3
continue outer
Outer while loop
i = 4
i = 5
break
Outer while loop
i = 6
i = 7
break outer
*///:~

ExamleCode_12

package control;

//: control/VowelsAndConsonants.java   //vowe 元音            //consonants辅音
//	Demonstrates the switch statement  //演示switch语句。
//	本程序随机生成字母并判断它们是元音字母还是辅音字母

import java.util.*;
import static net.mindview.util.Print.*;

public class VowelsAndConsonants {
	
	public static void main(String[] args) {
		Random rand = new Random(47);
		for(int i = 0; i < 100; i++) {
			int c = rand.nextInt(26) + 'a';
			printnb((char)c + ", " + c + ": ");
			switch(c) {
				case 'a':
				case 'e':
				case 'i':
				case 'o':
				case 'u': 
					print("vowel");
					break;
				case 'y':
				case 'w': 
					print("Sometimes a vowel");
					break;
				default: 
					print("consonant");
			}
		}
	}
	
} /* Output:
y, 121: Sometimes a vowel
n, 110: consonant
z, 122: consonant
b, 98: consonant
r, 114: consonant
n, 110: consonant
y, 121: Sometimes a vowel
g, 103: consonant
c, 99: consonant
f, 102: consonant
o, 111: vowel
w, 119: Sometimes a vowel
z, 122: consonant
...
*///:~

猜你喜欢

转载自blog.csdn.net/weixin_43002838/article/details/88917851