6-1 SequenceDemo1.java
class SequenceDemo1 {
	public static void main(String[] args) {
		String name = "ZhangSan";
		String greetings = "Welcome to Java World!";
		System.out.print(name);
		System.out.print(" : ");
		System.out.println(greetings);
	}
}

6-2 IfElseDemo1.javaIfElseTest1.java
class IfElseDemo1 {
	public void method() {
		int balance = 400; 			// п
		int shouldPay = 230; 			// Ӧ
		if (balance > shouldPay) { 		// 
			System.out.println("п");
			balance -= shouldPay; 		// п˸
		} else { 					// 򣺼
			System.out.println("ֽ𸶿");
		}
	}
}
public class IfElseTest1 {
	public static void main(String[] args) {
		IfElseDemo1 ob = new IfElseDemo1();
		ob.method ();
	}
}

6-3 IfElseDemo2.javaIfElseTest2.java
import javax.swing.JOptionPane;
class IfElseDemo2 {
	public void method() {
		int hour;
		String tmpX = JOptionPane.showInputDialog("뵱ǰʱֵ0~23");
		hour = Integer.parseInt(tmpX);
		if (hour < 0) {
			// ֧1ʱֵ<0
			JOptionPane.showMessageDialog(null, "ʱֵΪ");
		} else if (hour < 10) {
			// ֧2ʱֵ>=0  <10
			JOptionPane.showMessageDialog(null, "Ϻã");
		} else if (hour < 19) {
			// ֧3ʱֵ>=10  <19
			JOptionPane.showMessageDialog(null, "հ");
		} else if (hour < 24) {
			// ֧4ʱֵ>=19  <24
			JOptionPane.showMessageDialog(null, "Ϻã");
		} else {
			// ֧5ʱֵ>=24
			JOptionPane.showMessageDialog(null, "ʱӳΧˣ");
		}
	}
}
public class IfElseTest2 {
	public static void main(String[] args) {
		IfElseDemo2 ob = new IfElseDemo2();
		ob.method();
	}
}

6-4 IfElseDemo3.javaIfElseTest3.java
import javax.swing.JOptionPane;
class IfElseDemo3 {
	public void method() {
		int hour;
		String tmpX = JOptionPane.showInputDialog("뵱ǰʱֵ0~23");
		hour = Integer.parseInt(tmpX);
		if (hour >= 0 && hour < 24) {
			// ǰʱֵ0~23֮䣬Ч
			JOptionPane.showMessageDialog(null, "ʱֵЧ");
			if (hour < 10) {
				// ʱֵ>=0  <10
				JOptionPane.showMessageDialog(null, "Ϻã");
			} else { // ʱֵ>=10  <24
				if (hour < 19) { // ʱֵ>=10  <19
					JOptionPane.showMessageDialog(null, "հ");
				} else { // ʱֵ>=19  <24
					JOptionPane.showMessageDialog(null, "Ϻã");
				}
			}
		} else { // ʱֵ <0  >=24Ч
			JOptionPane.showMessageDialog(null, "ʱЧ");
		}
	}
}
public class IfElseTest3 {
	public static void main(String[] args) {
		IfElseDemo3 ob = new IfElseDemo3();
		ob.method();
	}
}

6-5 SwitchDemo1.javaSwitchTest1.java
import javax.swing.JOptionPane;
class SwitchDemo1 {
	public void method() {
		int day;
		String tmpDay = JOptionPane.showInputDialog("ڼ");
		day = Integer.parseInt(tmpDay);
		switch (day) {
		case 1:
			System.out.println("һǹգ");
		case 2:
			System.out.println("ڶǹգ");
		case 3:
			System.out.println("ǹգ");
		case 4:
			System.out.println("ģǹգ");
		case 5:
			System.out.println("壬ǹգ");
		case 6:
			System.out.println("Ϣգ");
		case 7:
			System.out.println("죬Ϣգ");
		default:
			System.out.println("û" + day + "");
		}
	}
}
public class SwitchTest1 {
	public static void main(String[] args) {
		SwitchDemo1 ob = new SwitchDemo1();
		ob.method();
	}
}

6-6 SwitchDemo2.javaSwitchTest2.java
import javax.swing.JOptionPane;
class SwitchDemo2 {
	public void method() {
		int day;
		String tmpDay = JOptionPane.showInputDialog("ڼ");
		day = Integer.parseInt(tmpDay);
		switch (day) {
		case 1:
			System.out.println("һǹգ");
			break;
		case 2:
			System.out.println("ڶǹգ");
			break;
		case 3:
			System.out.println("ǹգ");
			break;
		case 4:
			System.out.println("ģǹգ");
			break;
		case 5:
			System.out.println("壬ǹգ");
			break;
		case 6:
			System.out.println("Ϣգ");
			break;
		case 7:
			System.out.println("죬Ϣգ");
			break;
		default:
			System.out.println("û" + day + ""); // break;
			// пޣʱΪʹṹҲ
		}
	}
}
public class SwitchTest2 {
	public static void main(String[] args) {
		SwitchDemo2 ob = new SwitchDemo2();
		ob.method();
	}
}

6-7 3ҳе߲ʾ
import javax.swing.JOptionPane;
public class Example6_7 {
	public void maxToMin(int a, int b, int c) {
		int temp = 0;
		if (a < b) {
			temp = a;
			a = b;
			b = temp;
		}
		if (a < c) {
			temp = a;
			a = c;
			c = temp;
		}
		if (b < c) {
			temp = b;
			b = c;
			c = temp;
		}
		JOptionPane.showMessageDialog(null, "ɴСΪ" 
+ a + " " + b + " " + c);
	}
}
import javax.swing.JOptionPane;
public class Example6_7_Test {
	public static void main(String[] args) {
		Example6_7 ob = new Example6_7();
		String strA = JOptionPane.showInputDialog("1");
		int a = Integer.parseInt(strA);
		String strB = JOptionPane.showInputDialog("2");
		int b = Integer.parseInt(strB);
		String strC = JOptionPane.showInputDialog("3");
		int c = Integer.parseInt(strC);
		ob.maxToMin(a, b, c);
	}
}

6-8 һֵȻж÷Ӧĳɼȼ
import javax.swing.JOptionPane;
public class Example6_8 {
	public void method(int score) {
		String grade; // ȼϢ
		if (score >= 90) {
			grade = "A";
		} else if (score >= 80) {
			grade = "B";
		} else if (score >= 70) {
			grade = "C";
		} else if (score >= 60) {
			grade = "D";
		} else {
			grade = "E";
		}
		JOptionPane.showMessageDialog(null, "ѧɼĵȼǣ" + grade);
	}
}
import javax.swing.JOptionPane;
public class Example6_8_Test {
	public static void main(String[] args) {
		Example6_8 ob = new Example6_8();
		int score; 
		String strScore = JOptionPane.showInputDialog("ѧɼ1~100");
		score = Integer.parseInt(strScore);
		ob.method(score);
	}
}

6-9 ܣҲԸswitchṹʵ֣ʾ
import javax.swing.JOptionPane;
public class Example6_9 {
	public void method(int score) {
		String grade = ""; // ȼϢ
		switch (score / 10) {
		case 10:
		case 9:
			grade = "A";
			break;
		case 8:
			grade = "B";
			break;
		case 7:
			grade = "C";
			break;
		case 6:
			grade = "D";
			break;
		case 5:
		case 4:
		case 3:
		case 2:
		case 1:
		case 0:
			grade = "E";
			break;
		}
		JOptionPane.showMessageDialog(null, "ѧɼĵȼǣ" + grade);
	}
}
import javax.swing.JOptionPane;
public class Example6_9_Test {
	public static void main(String[] args) {
		Example6_9 ob = new Example6_9();
		int score; 
		String strScore = JOptionPane.showInputDialog("ѧɼ1~100");
		score = Integer.parseInt(strScore);
		ob.method(score);
	}
}


6-10 WhileDemo1.javaDoSum.java
public class WhileDemo1 {
	public int sum() {
		int sum = 0;
		int n = 1;
		while (n <= 100) {
			sum = sum + n;
			n++; //ǳҪ
		}
		return sum;
	}
}
public class DoSum1 {
	public static void main(String[] args) {
		WhileDemo1 ob = new WhileDemo1();
		int sum = ob.sum();
		System.out.println("1+2+...+100=" + sum);
	}
}

6-11 ForDemo1.javaDoSum2.java
public class ForDemo1 {
	public int sum() {
		int sum = 0;
		for (int i = 0; i <= 100; i++) {
			sum = sum + i;
		}
		return sum;
	}
}
public class DoSum2 {
	public static void main(String[] args) {
		ForDemo1 ob = new ForDemo1();
		int sum = ob.sum();
		System.out.println("1+2+...+100=" + sum);
	}
}

6-12 DoWhileDemo1.javaDoSum3.java
public class DoWhileDemo1 {
	public int sum() {
		int sum = 0;
		int i = 1;
		do {
			sum = sum + i;
			i++;		//ǳҪ
		} while (i <= 100);	//Ҫ ֺ
		return sum;
	}
}
public class DoSum3 {
	public static void main(String[] args) {
		DoWhileDemo1 ob = new DoWhileDemo1();
		int sum = ob.sum();
		System.out.println("1+2+...+100=" + sum);
	}
}

6-13 NestedLoopDemo1.javaNestedLoopTest.java
public class NestedLoopDemo1 {
	public void doSearch() {
		int a, b, c, d;
		String output = "AC+BD=DA4λУ\n";
		for (a = 1; a < 10; a++) {
			for (b = 0; b < 10; b++) {
				for (c = 0; c < 10; c++) {
					for (d = 0; d < 10; d++) {
						if ((a * 10 + c) + (b * 10 + d) == d * 10 + a) {
							output += a + "" + "" + b + "" + c + "" +
   d + "" + a + c + "+" + b + d
 + " = " + d + a + "\n";
						} //end of if
					} //end of 4ѭ
				} //end of 3ѭ
			} //end of 2ѭ
		} //end of ѭ
		System.out.println(output);
	}
}
public class NestedLoopTest {
	public static void main(String[] args) {
		NestedLoopDemo1 ob = new NestedLoopDemo1();
		ob.doSearch();
	}
}

6-14 1!+2!++20!ĺֵʾ
public class Example6_14 {
	public long method() {
		int i;
		long f = 0; 		// ׳˵ĺֵ
		long tn = 1; 		// ĳһ׳
		for (i = 1; i <= 20; i++) {
			tn *= i;
			f += tn;
		}
		return f;
	}
}
import javax.swing.JOptionPane;
public class Example6_14_Test {
	public static void main(String[] args) {
		Example6_14 ob = new Example6_14();
		long f = ob.method();
		JOptionPane.showMessageDialog(null, "1! + ... + 20! = " + f);
	}
}

6-15 æ/41-1/3+1/5-1/7+ʽеĽֱֵĳһľֵС10-6Ϊֹʾ
public class Example6_15 {
	public double calculatePI() {
		double pi = 0;
		int fz = 1; 				// 
		double fm = 1.0; 		// ĸ
		double tn = 1.0; 		// ĳһ
		while (Math.abs(tn) > 1E-6) {
			pi += tn;
			fm += 2;
			fz = -fz;
			tn = fz / fm;
		}		
		pi *= 4;
		return pi;
	}
}
import javax.swing.JOptionPane;
public class Example6_15_Test {
	public static void main(String[] args) {
		Example6_15 ob = new Example6_15();
		double pi = ob.calculatePI();
		JOptionPane.showMessageDialog(null, "PIֵΪ" + pi);
	}
}

6-16 Ǯټ
public class Example6_16 {
	public void buy() {
		int hen;
		int cock;
		int chicken;
		String output = "ܵ򼦷¼:\n";
		for (hen = 1; hen < 20; hen++)
			for (cock = 1; cock < 33; cock++) {
				chicken = 100 - hen - cock;
				
				if ((hen * 5 + cock * 3 + chicken / 3) == 100
						&& chicken % 3 == 0) {
					output += "hen : " + hen + "  cock : " + cock
							+ "  chicken : " + chicken + "\n";
				}
			}
		System.out.println(output);
	}
}
public class Example6_16_Test {
	public static void main(String[] args) {
		Example6_16 ob = new Example6_16();
		ob.buy();
	}
}

6-17	BreakInSwitch.javaBreakDemo1.java
public class BreakInSwitch {
	public int days(int year, int month) {
		int days = 0;
		switch (month) {
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:
			days = 31;
			break;
		case 4:
		case 6:
		case 9:
		case 11:
			days = 30;
			break;
		case 2:
			if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
				days = 29;
			} else {
				days = 28;
			}
			break;
		}
		return days;
	}
}
import javax.swing.JOptionPane;
public class BreakDemo1 {
	public static void main(String[] args) {
		BreakInSwitch ob = new BreakInSwitch();
		String strYear = JOptionPane.showInputDialog("һݣ");
		String strMonth = JOptionPane.showInputDialog("һ·ݣ");
		int year = Integer.parseInt(strYear);
		int month = Integer.parseInt(strMonth);
		int days = ob.days(year, month);
		JOptionPane.showMessageDialog(null, year + "" + month + "" + days
				+ "죡");
	}
}

6-18	BreakInLoop.javaBreakDemo2.java
public class BreakInLoop {
	public void area() {
		double area = 0;
		int radius;
		for (radius = 1; radius <= 10; radius++) {
			area = Math.PI * radius * radius;
			if (area > 100) {
				break;
			}
			System.out.println("radius=" + radius + "  area=" + area);
		}
	}
}
public class BreakDemo2 {
	public static void main(String[] args) {
		BreakInLoop ob = new BreakInLoop();
		ob.area();
	}
}

6-19	ContinueDemo1.javaContinueTest.java
public class ContinueDemo1 {
	public void method() {
		int n = 200;
		System.out.println("200~300ܱ֮7У");
		for (; n <= 300; n++) {
			if (n % 7 != 0) {
				continue;
			}
			System.out.print(n + " ");
		}
	}
}
public class ContinueTest {
	public static void main(String[] args) {
		ContinueDemo1 ob = new ContinueDemo1();
		ob.method ();
	}
}


