Java 程式範例基礎集合(2)

four bots

Java 程式範例基礎集合(2)

108學年01學期,四筒老師在課堂上課講解、立即上機實作練習。開始比之前難一點點了!

package com.four.bots.step;

import java.io.*;

/**
 * 陣列來排成績名次
 */
public class Example21 {

	public static void main(String[] args) {
		String getbr;
		InputStream is = System.in;
		Reader read = new InputStreamReader(is);
		BufferedReader br = new BufferedReader(read);
		
		int i, x, j, k;
		float score[], inp;
		int flag;
		
		try {
			/* ========== 請使用者輸入選項 ========== */
			System.out.print("請輸入學生數:");
			getbr = br.readLine();
			
			// 取得學生數
			x = Integer.parseInt(getbr);
			
			// 定義分數陣列
			score = new float[x]; 
			for (i = 0; i < x; i = i + 1) {
				// 利用類似insertion sort的方法來算名次
				System.out.print("輸入第" + (i + 1) + "個分數:");
				
				// 取得學生數
				getbr = br.readLine();
				inp = Float.parseFloat(getbr);
				flag = 0;
				for (j = x - 1; j > 0; j = j - 1) {
					if (score[j - 1] < inp) {
						score[j] = score[j - 1];
					} else {
						score[j] = inp;
						flag = 1;
						break;
					} // else
				} // for j 做 排序
				
				if (flag == 0) {
					score[0] = inp;
				}
				
				for (k = 0; k < x; k = k + 1) {
					// 利用類似insertion sort的方法來算名次
					System.out.println("第" + (k + 1) + "名=" + score[k]);
				}
				System.out.println("--------------------");
			}
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}


package com.four.bots.step;

import java.io.*;

/**
 * 利用關鍵詞變更使用者輸入的文字
 */
public class Example22 {

	public static void main(String[] args) {
		StringBuffer x = new StringBuffer();

		String getbr;
		InputStream is = System.in;
		Reader read = new InputStreamReader(is);
		BufferedReader br = new BufferedReader(read);

		int i;
		String s = null;

		try {
			/* ========== 請使用者輸入選項 ========== */
			System.out.print("請輸入您要說的話:");
			getbr = br.readLine(); // 取得使用者的輸入
			
			/* ========== 尋找「我」字的位置 ========== */
			if (getbr.indexOf("我") < 0) {
				System.out.println("沒有出現「我」字");
			} else {
				s = "「我」字出現在第" + (getbr.indexOf("我") + 1) + "個位置";
				System.out.println(s);
				
				/* ========== 利用StringBuffer 來處理字串 ========== */
				s = "『" + getbr + "』這句話說得很有道理!";
				x.append(s);
				System.out.println(x.toString());
				System.out.println("----------------------");
				
				i = x.indexOf("我");
				// 「我」變更「你」
				x.setCharAt(i, '你');
				System.out.println(x.toString());
				System.out.println("----------------------");
				
				// 刪除「你」
				x.deleteCharAt(i); 
				// 「你」變更「在下」
				x.insert(i, "在下"); 
				System.out.println(x.toString());
				System.out.println("----------------------");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}


package com.four.bots.step;

import java.io.*;

/**
 * 指定檔案,利用關鍵詞變更讀取檔案內容
 */
public class Example23 {

	public static void main(String[] args) {
		String getbr, getfr;
		InputStream is = System.in;
		Reader read = new InputStreamReader(is);
		BufferedReader br = new BufferedReader(read);

		int i;
		BufferedReader fr = null;
		try {
			/* ========== 請使用者輸入選項 ========== */
			System.out.print("請輸入檔案名稱:");
			getbr = br.readLine(); // 取得input file

			/* ========== open file ========== */
			Reader in = new FileReader(getbr);
			fr = new BufferedReader(in);
			while (fr.ready()){// 非檔案尾行,則繼續
				
				// 取檔案每行資料
				getfr = fr.readLine();
				StringBuffer x = new StringBuffer(getfr);

				/* ========== 尋找「我」字的位置 ========== */
				if (getfr.indexOf("我") >= 0) {
					
					/* ========== 利用StringBuffer 來處理字串 ========== */
					i = getfr.indexOf("我");
					x.deleteCharAt(i); // 刪掉「我」
					x.insert(i, "在下"); // 把「我」換成「在下」
				}
				
				System.out.println(x.toString());
				System.out.println("----------------------");
				
			} // end while
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (fr != null) {
				try {
					fr.close();
				} catch (IOException e) {
					// 發生錯誤不作任何動作
				}
				fr = null;
			}
		}
	}
}


package com.four.bots.step;

import java.io.*;

/**
 * 使用FileReader 讀取檔案內容
 */
public class Example24 {

	public static void main(String[] args) {
		String getbr, getfr;
		InputStream is = System.in;
		Reader read = new InputStreamReader(is);
		BufferedReader br = new BufferedReader(read);
		
		int i;
		FileReader fr = null;
		char data[] = new char[2048];// 預設讀2K個字元
		
		try {
			/* ========== 請使用者輸入選項 ========== */
			System.out.print("請輸入檔案名稱:");
			getbr = br.readLine(); // 得input file
			
			/*========== open file ==========*/
			fr = new FileReader(getbr);
			while (fr.ready()) {
				// 取檔案每行資料
				i = fr.read(data);
				getfr = new String(data, 0, i);
				
				// 列印顯示
				System.out.print(getfr);
				
			} // end while
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (fr != null) {
				try {
					fr.close();
				} catch (IOException e) {
					// 發生錯誤不作任何動作
				}
				fr = null;
			}
		}
	}

}


package com.four.bots.step;

import java.io.*;

/**
 * 使用FileReader 讀取來源檔案
 * 使用FileWriter 寫入目標檔案
 */
public class Example25 {

	public static void main(String[] args) {
		String getbr, getfr;
		InputStream is = System.in;
		Reader read = new InputStreamReader(is);
		BufferedReader br = new BufferedReader(read);

		int i;
		FileReader fr = null;
		FileWriter fw = null;
		
		// 最多讀取2K個字元
		char data[] = new char[2048];
		
		try {
			/* ========== 請使用者輸入選項 ========== */
			System.out.print("請輸入來源檔案名稱:");
			getbr = br.readLine(); // 取得input file
			
			/* ========== open input file ========== */
			fr = new FileReader(getbr);
			System.out.print("請輸入目標檔案名稱:");
			getbr = br.readLine(); // 取得output file
			
			/* ========== open output file ========== */
			fw = new FileWriter(getbr);

			while (fr.ready()){
				// 取檔案每行資料
				i = fr.read(data);
				getfr = new String(data, 0, i);
				
				// 寫入檔案內
				fw.write(getfr);
			} // end while
			fw.flush();
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (fr != null) {
				try {
					fr.close();
				} catch (IOException e) {
					// 發生錯誤不作任何動作
				}
				fr = null;
			}
			if(fw != null) {
				try {
				fw.close();
				} catch (IOException e) {
					// 發生錯誤不作任何動作
				}
				fw = null;
			}
		}
	}

}


package com.four.bots.step;

import java.io.*;

/**
 * 使用BufferedReader 讀取來源檔案 
 * 使用BufferedWriter 寫入目標檔案
 * 利用關鍵詞替換寫入檔案內容
 */
public class Example26 {

	public static void main(String[] args) {
		String getfr, source, target;
		InputStream is = System.in;
		Reader read = new InputStreamReader(is);
		BufferedReader br = new BufferedReader(read);

		int i;
		BufferedReader fr = null;
		BufferedWriter fw = null;
		
		try {
			/* ========== 請使用者輸入選項 ========== */
			System.out.print("請輸入來源檔案名稱:");
			source = br.readLine(); // 得 input file
			System.out.print("請輸入目標檔案名稱:");
			target = br.readLine(); // 得 output file
			
			/* ========== open file ========== */
			FileReader r = new FileReader(source);
			FileWriter w = new FileWriter(target);
			fr = new BufferedReader(r);
			fw = new BufferedWriter(w);
			
			while (fr.ready()) {
				getfr = fr.readLine();
				StringBuffer x = new StringBuffer(getfr);

				/*========== 尋找「我」字的位置 ==========*/
				if (getfr.indexOf("我") >= 0) {
					
					/*========== 利用StringBuffer 來處理字串 ==========*/
					i = getfr.indexOf("我");
					x.deleteCharAt(i);   // 刪除「我」
					x.insert(i, "在下"); // 「我」變更「在下」
				}
				
				// 寫入檔案
				fw.write(x.toString()); 
				fw.newLine();
			} // end while
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (fr != null) {
				try {
					fr.close();
				} catch (IOException e) {
					// 發生錯誤不作任何動作
				}
				fr = null;
			}
			if(fw != null) {
				try {
				fw.close();
				} catch (IOException e) {
					// 發生錯誤不作任何動作
				}
				fw = null;
			}
		}		
	}
}


package com.four.bots.step;

/**
 * method 參數傳遞
 */
public class Example27 {

	public static void main(String[] args) {
		/* ========== 整數部分 ========== */
		int A, ans;

		A = 10;
		ans = 0;
		System.out.println("呼叫前 A=" + A + " ans=" + ans);
		System.out.println("----------------------");
		
		ans = inc(A);
		System.out.println("呼叫後 A=" + A + " ans=" + ans);
		System.out.println("----------------------");
		
		/* ========== String Buffer 部分 ========== */
		StringBuffer sb = new StringBuffer("這是一個測試");
		StringBuffer xb = sapp(sb);
		System.out.println("處理後 sb=" + sb.toString());
		System.out.println("處理後 xb=" + xb.toString());
		System.out.println("----------------------");
	}

	public static int inc(int n) {
		System.out.println("呼叫中處理前 n=" + n);
		
		n = n + 1;
		System.out.println("呼叫中處理後 n=" + n);
		return (n);
	}

	public static StringBuffer sapp(StringBuffer rc) {
		System.out.println("呼叫中處理前 rc=" + rc.toString());
		
		rc.insert(0, "你說:");
		System.out.println("呼叫中處理後 rc=" + rc.toString());
		return (rc);
	}
}


package com.four.bots.step;

/**
 * 類別的定義
 */
public class Example28 {

	public static void main(String[] args) {
		String s;
		rectangle rec1 = new rectangle();
		rectangle rec2 = new rectangle();
		
		/*========== 設定長寬 ==========*/
		rec1.width = 10;
		rec1.height = 20;
		
		rec2.width = 10.5;
		rec2.height = 20.5;
		
		/*========== 計算矩形面積==========*/
		s = "矩形1面積為 " + rec1.area() + " 週長為 " + rec1.perimeter();
		System.out.println(s);
		
		s = "矩形2面積為 " + rec2.area() + " 週長為 " + rec2.perimeter();
		System.out.println(s);
	}
}

// 類別的定義
class rectangle {
	double width;  // 定義矩形的長
	double height; // 定義矩形的寬

	// 定義矩形的面積計算方法
	double area() {
		double a;
		a = width * height;
		return a;
	}

	// 定義矩形的週長計算方法
	double perimeter() {
		double b;
		b = 2 * width * height;
		return b;
	}
}

package com.four.bots.step;

import java.io.*;

/**
 * 類別中method的使用,計算階乘
 */
public class Example29 {

	public static void main(String[] args) {
		String getbr;
		InputStream is = System.in;
		Reader read = new InputStreamReader(is);
		BufferedReader br = new BufferedReader(read);

		int x;
		/* ========== 請使用者輸入選項 ========== */
		x = 5;
		while (x >= 0) {
			System.out.print("請輸入-1以上來計算階乘的數字:");
			try {
				getbr = br.readLine();
				x = Integer.parseInt(getbr);
			} catch (IOException e) {
				x = 0;
				System.out.println("請輸入數值!");
			}
			
			if (x < 0) {
				System.out.println("謝謝!再見!");
				continue;
			}
			System.out.println(x + "!=" + fraction(x));
		} // end while

	}

	static int fraction(int y) {
		int i, ans;
		ans = 1;
		for (i = 1; i <= y; i = i + 1) {
			ans = ans * i;
		}
		return (ans);
	}

}


package com.four.bots.step;

import java.io.*;

/**
 * 指定class 處理計算階乘
 */
public class Example30 {

	public static void main(String[] args) {
		String getbr;
		InputStream is = System.in;
		Reader read = new InputStreamReader(is);
		BufferedReader br = new BufferedReader(read);

		int x;
		fra fr = new fra();// 指定class 處理計算階乘

		/* ========== 請使用者輸入選項 ========== */
		x = 9;
		while (x >= 0) {
			System.out.print("請輸入要計算階乘的數字:");

			try {
				getbr = br.readLine();
				x = Integer.parseInt(getbr);
			} catch (IOException e) {
				x = 0;
				System.out.println("請輸入數值!");
			}

			if (x < 0) {
				System.out.println("謝謝!再見!");
				continue;
			}
			System.out.println(x + "!=" + fr.fraction(x));
		} // while結束
	}
}

class fra {
	int fraction(int y) {
		int i, ans;
		ans = 1;
		for (i = 1; i <= y; i = i + 1) {
			ans = ans * i;
		}
		return (ans);
	}
}

package com.four.bots.layer;

/**
 * 類別this 的使用方法
 */
public class Example31 {

	public static void main(String[] args) {
		String s;
		rectangle rec1 = new rectangle();
		rectangle rec2 = new rectangle();
		
		/*========== 設定長寬 ==========*/
		rec1.width = 10;
		rec1.height = 20;
		
		rec2.width = 10.5;
		rec2.height = 20.5;
		
		/*========== 計算矩形面積==========*/
		s = "矩形1面積為 " + rec1.area() + " 週長為 " + rec1.perimeter();
		System.out.println(s);
		
		s = "矩形2面積為 " + rec2.area() + " 週長為 " + rec2.perimeter();
		System.out.println(s);
	}
}

// 變數的範圍與使用方法
class rectangle {
	double width;  // 定義矩形的長
	double height; // 定義矩形的寬

	// 定義矩形的面積計算方法
	double area() {
		double a;
		a = this.width * this.height;
		System.out.println("矩形長= " + this.width + " 寬=" + this.height);
		return a;
	}

	// 定義矩形的週長計算方法
	double perimeter() {
		double b;
		b = 2 * this.width * this.height;
		System.out.println("矩形長為 " + this.width + " 寬為 " + this.height);
		return b;
	}
}

package com.four.bots.layer;

import java.io.*;

/**
 * 遞迴方式,呼叫計算楊暉三角塔
 */
public class Example32 {

	@SuppressWarnings("resource")
	public static void main(String[] args) throws IOException {
		String getbr;
		InputStream is = System.in;
		Reader read = new InputStreamReader(is);
		BufferedReader br = new BufferedReader(read);

		int a;
		int i, j;
		
		while (true) {
			do {
				System.out.println("請寫出a階之楊暉三角塔");
				System.out.print("請輸入a:");
				getbr = br.readLine();

				// 判斷結束
				if (getbr.equals("0")) {
					System.out.println("結束!");
					return;
				}
				
				a = Integer.parseInt(getbr);// 取得A階
			} while (a <= 0);

			for (i = 0; i <= a; i = i + 1) {
				for (j = 0; j <= i; j = j + 1) {
					System.out.print(" " + c(i, j) + " ");
				}
				System.out.println();
			}
			System.out.println("--------------------");
		}
	}

	/**
	 * 專門算Cmn 的method
	 * Cmn=Cm-1 n + Cm-1,n-1
	 * if n==0 Cmn=1 
	 * if m==n Cmn=1
	 */
	static int c(int m, int n) {
		if (n == 0 || m == n)
			return 1;
		return c(m - 1, n) + c(m - 1, n - 1);
	}
}


package com.four.bots.layer;

import java.io.*;

/**
 * 遞迴方式,猜數字
 */
public class Example33 {

	@SuppressWarnings("resource")
	public static void main(String[] args) throws Exception {
		int a;// 需猜測數字的範圍
		String getbr;
		InputStream is = System.in;
		Reader read = new InputStreamReader(is);
		BufferedReader br = new BufferedReader(read);

		while (true) {
			do {
				System.out.print("請輸入要猜測的整數範圍:");
				getbr = br.readLine();

				// 判斷結束
				if (getbr.equals("0")) {
					System.out.println("結束!");
					return;
				}
				a = Integer.parseInt(getbr);// 取範圍
			} while (a <= 0);

			guess(a, 0);// 開始猜數字
		}
	}

	/*******************************
	 * 二分逼近法猜數字,每次猜中間的值
	 *******************************/
	static void guess(int large, int small) throws Exception {
		String getbr;
		InputStream is = System.in;
		Reader read = new InputStreamReader(is);
		BufferedReader br = new BufferedReader(read);

		System.out.println("我猜:" + (small + large) / 2);
		System.out.print("請輸入0:猜對了 1:猜的太大  2:猜的太小 :");
		getbr = br.readLine();
		
		if ("0".equals(getbr)) {
			System.out.println("嘿嘿!我猜對了!");
			return;
		} else if (large == small) {
			// large 和small 都已經一樣了,不可能猜不到
			System.out.println("你說謊!");
			return;
		} else if ("1".equals(getbr)) {
			// 太大? 就把大數縮小繼續猜
			large = (large + small) / 2;
			guess(large, small);
		} else if ("2".equals(getbr)) {
			// 太小? 就把小數放大繼續猜
			small = (large + small) / 2;
			guess(large, small);
		}
	}
}


package com.four.bots.layer;

import java.io.*;

/**
 * 多載(Overload)指在一個類別(class)中,
 * 定義多個名稱相同,
 * 但參數(Parameter)不同的方法(Method)。
 */
public class Example34 {

	public static void main(String[] args) throws Exception {
		String getbr;
		InputStream is = System.in;
		Reader read = new InputStreamReader(is);
		BufferedReader br = new BufferedReader(read);

		int x, pt;
		double fx;

		/* ========== 請使用者輸入選項 ========== */
		x = 5;
		while (x >= 0) {
			System.out.print("請輸入要計算階乘的數字:");
			getbr = br.readLine();

			/* ========== 判斷是否是整數 ========== */
			// 輸入字串如果有小數點就是實數
			pt = getbr.indexOf(".");

			// 整數
			if (pt < 0) {
				x = Integer.parseInt(getbr);
				if (x < 0) {
					System.out.println("謝謝!再見!");
					continue;
				}
				
				System.out.println(x + "!=" + fraction(x));
			} else {
				fx = Double.parseDouble(getbr);
				if (fx < 0) {
					System.out.println("謝謝!再見!");
					continue;
				}
				
				System.out.println(fx + "!=" + fraction(fx));
			}
		}
	}

	/**
	 * 整數階乘
	 */
	private static int fraction(int y) {
		int ans;
		ans = 1;
		if (y <= 1) {
			ans = 1;
		} else {
			ans = y * fraction(y - 1);
		}
		return (ans);
	}

	/**
	 * 實數階乘
	 */
	private static int fraction(double y) {
		int ans;
		int newy;
		newy = (int) y; // 換成整數
		System.out.println("哪有人輸入實數的,下次請輸入整數:" + newy);

		ans = 1;
		if (newy <= 1) {
			ans = 1;
		} else {
			ans = newy * fraction(newy - 1);
		}
		return (ans);
	}
}


package com.four.bots.cards;

import java.util.*;

/**
 * 隨機產生亂數的發牌程式
 */
public class Example35 {

	public static void main(String[] args) {

		int A, B;
		int i, j, x;

		// 設定發五張牌
		int num = 5;
		// 記錄該位牌是否已被發出
		int[] card = new int[52];
		// 記錄發到的牌
		int[][] a = new int[4][13];

		String[] cname = { "S", "H", "D", "C" };
		// 產生亂數
		Random randomG = new Random();

		/* ---------- 起始值設定 ---------- */
		for (i = 0; i < 52; i = i + 1) {
			card[i] = 0;
		}
		for (i = 0; i < 4; i = i + 1) {
			for (j = 0; j < 13; j = j + 1) {
				a[i][j] = 0;
			}
		}

		/* ---------- 亂數產生發牌 ---------- */
		for (i = 0; i < 4; i = i + 1) {
			for (j = 0; j < num; j = j + 1) {
				x = randomG.nextInt(52);
				
				while (card[x] != 0) {
					x = randomG.nextInt(52);
				}
				
				a[i][j] = x;
				card[x] = 1;
			}
		}
		
		/* ---------- 列印顯示 ---------- */
		for (i = 0; i < 4; i = i + 1) {
			for (j = 0; j < num; j = j + 1) {
				A = a[i][j] / 13;
				B = (a[i][j] % 13) + 1;
				System.out.print(B + cname[A] + " ");
			}
			System.out.println();
		}
	}
}


package com.four.bots.cards;

import java.util.*;

/**
 * 使用物件來發牌動作
 */
public class Example36 {

	public static void main(String[] args) {
		int i, j, x;
		Random ran = new Random();

		// 記錄該位牌是否已被發出
		int[] dcard = new int[52];
		// 設定發13張牌
		int num = 13;
		// 定義四家玩家
		player p1 = new player();
		player p2 = new player();
		player p3 = new player();
		player p4 = new player();

		/* ---------- 起始值設定 ---------- */
		for (i = 0; i < 52; i = i + 1) {
			dcard[i] = 0;
		}

		/* ---------- 亂數產生發牌 ---------- */
		// 發給第一家
		for (j = 0; j < num; j = j + 1) {
			x = ran.nextInt(52);
			while (dcard[x] != 0) {
				x = ran.nextInt(52);
			}
			p1.set_card(j, x);
			dcard[x] = 1;
		}
		// 發下一家
		for (j = 0; j < num; j = j + 1) {
			x = ran.nextInt(52);
			while (dcard[x] != 0) {
				x = ran.nextInt(52);
			}
			p2.set_card(j, x);
			dcard[x] = 1;
		}
		// 發下一家
		for (j = 0; j < num; j = j + 1) {
			x = ran.nextInt(52);
			while (dcard[x] != 0) {
				x = ran.nextInt(52);
			}
			p3.set_card(j, x);
			dcard[x] = 1;
		}
		// 發下一家
		for (j = 0; j < num; j = j + 1) {
			x = ran.nextInt(52);
			while (dcard[x] != 0) {
				x = ran.nextInt(52);
			}
			p4.set_card(j, x);
			dcard[x] = 1;
		}

		/* ---------- 列印顯示 ---------- */
		System.out.println("第一家的牌如下:");
		for (j = 0; j < num; j = j + 1) {
			System.out.print(p1.show(j) + " ");
		}
		System.out.println();

		System.out.println("第二家的牌如下:");
		for (j = 0; j < num; j = j + 1) {
			System.out.print(p2.show(j) + " ");
		}
		System.out.println();

		System.out.println("第三家的牌如下:");
		for (j = 0; j < num; j = j + 1) {
			System.out.print(p3.show(j) + " ");
		}
		System.out.println();

		System.out.println("第四家的牌如下:");
		for (j = 0; j < num; j = j + 1) {
			System.out.print(p4.show(j) + " ");
		}
		System.out.println();
	}

}

class player {
	private int[] card = new int[13];// 記錄手上的牌
	private String[] cname = { "梅花", "方塊", "紅心", "黑桃" };

	/**
	 * 建構子,首先把牌清乾淨
	 */
	public player() {
		for (int i = 0; i < 13; i = i + 1) {
			card[i] = -1;
		}
	}

	/**
	 * method 用來show出第幾張牌
	 */
	public String show(int id) {
		int A, B;
		if (card[id] == -1) {
			return ("沒有這張牌");
		} else {
			A = card[id] % 4;
			B = card[id] / 4;
			B = B + 2;

			switch (B) {
			case 14:
				return (cname[A] + "A");
			case 13:
				return (cname[A] + "K");
			case 12:
				return (cname[A] + "Q");
			case 11:
				return (cname[A] + "J");
			default:
				return (cname[A] + B);
			}
		}
	}

	/**
	 * method 用來發牌給本玩家
	 */
	public void set_card(int id, int x) {
		if (id < 13 && x < 52) {
			card[id] = x;
		} else {
			String message = ("錯誤 id=" + id + " x=" + x);
			System.out.println(message);
		}
	}
}


package com.four.bots.cards;

import java.util.*;

/**
 * 使用物件的多載執行發牌動作
 */
public class Example37 {

	public static void main(String[] args) {
		int i, j, x;
		Random ran = new Random();

		// 設定發13張牌
		int num = 13;
		// 記錄該位牌是否已被發出
		int[] dcard = new int[52];
		// 定義四家玩家
		player37 p1 = new player37();
		player37 p2 = new player37();
		player37 p3 = new player37();
		player37 p4 = new player37();

		/* ---------- 起始值設定 ---------- */
		for (i = 0; i < 52; i = i + 1) {
			dcard[i] = 0;
		}

		/* ---------- 亂數產生發牌 ---------- */
		// 發給第一家
		for (j = 0; j < num; j = j + 1) {
			x = ran.nextInt(52);
			while (dcard[x] != 0) {
				x = ran.nextInt(52);
			}
			p1.set_card(x);
			dcard[x] = 1;
		}
		// 發下一家
		for (j = 0; j < num; j = j + 1) {
			x = ran.nextInt(52);
			while (dcard[x] != 0) {
				x = ran.nextInt(52);
			}
			p2.set_card(j, x);
			dcard[x] = 1;
		}
		// 發下一家
		for (j = 0; j < num; j = j + 1) {
			x = ran.nextInt(52);
			while (dcard[x] != 0) {
				x = ran.nextInt(52);
			}
			p3.set_card(j, x);
			dcard[x] = 1;
		}
		// 發下一家
		for (j = 0; j < num; j = j + 1) {
			x = ran.nextInt(52);
			while (dcard[x] != 0) {
				x = ran.nextInt(52);
			}
			p4.set_card(j, x);
			dcard[x] = 1;
		}

		/* ---------- 列印顯示 ---------- */
		System.out.println("第一家的牌如下:");
		for (j = 0; j < num; j = j + 1) {
			System.out.print(p1.show(j) + " ");
		}
		System.out.println();

		System.out.println("第二家的牌如下:");
		for (j = 0; j < num; j = j + 1) {
			System.out.print(p2.show(j) + " ");
		}
		System.out.println();

		System.out.println("第三家的牌如下:");
		for (j = 0; j < num; j = j + 1) {
			System.out.print(p3.show(j) + " ");
		}
		System.out.println();

		System.out.println("第四家的牌如下:");
		for (j = 0; j < num; j = j + 1) {
			System.out.print(p4.show(j) + " ");
		}
		System.out.println();
	}
}

class player37 {

	// 記錄手上的牌
	private int[] card = new int[13];
	private String[] cname = { "梅花", "方塊", "紅心", "黑桃" };

	/**
	 * 建構子,首先把牌清乾淨
	 */
	public player37() {
		for (int i = 0; i < 13; i = i + 1) {
			this.card[i] = -1;
		}
	}

	/**
	 * method 用來show出第幾張牌
	 */
	public String show(int id) {
		int A, B;
		if (card[id] == -1) {
			return ("沒有這張牌");
		} else {
			A = card[id] % 4;
			B = card[id] / 4;
			B = B + 2;

			switch (B) {
			case 14:
				return (cname[A] + "A");
			case 13:
				return (cname[A] + "K");
			case 12:
				return (cname[A] + "Q");
			case 11:
				return (cname[A] + "J");
			default:
				return (cname[A] + B);
			}
		}
	}

	/**
	 * method 用來發牌給本玩家
	 */
	public void set_card(int id, int x) {
		if (id < 13 && x < 52) {
			card[id] = x;
		} else {
			String message = ("錯誤 id=" + id + " x=" + x);
			System.out.println(message);
		}
	}

	/**
	 * method 用來發牌給本玩家, 使用overload 來處理沒有設定第幾張的時機
	 */
	public void set_card(int x) {
		int i, flag;

		if (x > 52 || x < 0) {
			System.out.println("錯誤  x=" + x);
			return;
		}

		// flag用來看看有沒有發到牌
		flag = 0;
		for (i = 0; i < 13; i = i + 1) {

			// 那個位置沒有牌
			if (card[i] == -1) {
				card[i] = x;
				flag = 1;
				break; // 發完牌即可跳出
			}
		}

		if (flag == 0) {
			System.out.println("錯誤!本家發超過13張牌" + x);
		}
	}
}

package com.four.bots.cards;

import java.util.*;

/**
 * 使用物件來發牌動作,
 * 用static 來節省空間
 */
public class Example38 {

	@SuppressWarnings("static-access")
	public static void main(String[] args) {
		int i, j, x;
		Random ran = new Random();
		
		// 設定發13張牌
		int num = 13;
		// 記錄該位牌是否已被發出
		int[] dcard = new int[52]; 
		// 定義四家玩家
		player38 p1 = new player38();
		p1.show_cnt();
		player38 p2 = new player38();
		p2.show_cnt();
		player38 p3 = new player38();
		p1.show_cnt();
		player38 p4 = new player38();
		p4.show_cnt();

		/* ---------- 起始值設定 ---------- */
		for (i = 0; i < 52; i = i + 1) {
			dcard[i] = 0;
		}

		/* ---------- 亂數產生發牌 ---------- */
		// 發給第一家
		for (j = 0; j < num; j = j + 1) {
			x = ran.nextInt(52);
			while (dcard[x] != 0) {
				x = ran.nextInt(52);
			}
			p1.set_card(x);
			dcard[x] = 1;
		}
		// 發下一家
		for (j = 0; j < num; j = j + 1) {
			x = ran.nextInt(52);
			while (dcard[x] != 0) {
				x = ran.nextInt(52);
			}
			p2.set_card(j, x);
			dcard[x] = 1;
		}
		// 發下一家
		for (j = 0; j < num; j = j + 1) {
			x = ran.nextInt(52);
			while (dcard[x] != 0) {
				x = ran.nextInt(52);
			}
			p3.set_card(j, x);
			dcard[x] = 1;
		}
		// 發下一家
		for (j = 0; j < num; j = j + 1) {
			x = ran.nextInt(52);
			while (dcard[x] != 0) {
				x = ran.nextInt(52);
			}
			p4.set_card(j, x);
			dcard[x] = 1;
		}

		/* ---------- 列印顯示 ---------- */
		System.out.println("第一家的牌如下:");
		for (j = 0; j < num; j = j + 1) {
			System.out.print(p1.show(j) + " ");
		}
		System.out.println();

		System.out.println("第二家的牌如下:");
		for (j = 0; j < num; j = j + 1) {
			System.out.print(p2.show(j) + " ");
		}
		System.out.println();

		System.out.println("第三家的牌如下:");
		for (j = 0; j < num; j = j + 1) {
			System.out.print(p3.show(j) + " ");
		}
		System.out.println();

		System.out.println("第四家的牌如下:");
		for (j = 0; j < num; j = j + 1) {
			System.out.print(p4.show(j) + " ");
		}
		System.out.println();
	}

}

class player38 {
	// 記錄手上的牌
	private int[] card = new int[13];
	// 共用的對照表可以用static
	private static String[] cname = { "梅花", "方塊", "紅心", "黑桃" };
	// 共用的物件技術器
	private static int obj_cnt = 0;

	/**
	 * 建構子,首先把牌清乾淨
	 */
	public player38() {
		int i;
		for (i = 0; i < 13; i = i + 1) {
			this.card[i] = -1;
		}
		obj_cnt = obj_cnt + 1;// 登記多了一家
	}

	/**
	 * method 用來show出共有幾個物件
	 */
	public static void show_cnt() {
		String message = ("目前共有" + obj_cnt + "家");
		System.out.println(message);
	}

	/**
	 * method 用來show出第幾張牌
	 */
	public String show(int id) {
		int A, B;
		if (card[id] == -1) {
			return ("沒有這張牌");
		} else {
			A = card[id] % 4;
			B = card[id] / 4;
			B = B + 2;

			switch (B) {
			case 14:
				return (cname[A] + "A");
			case 13:
				return (cname[A] + "K");
			case 12:
				return (cname[A] + "Q");
			case 11:
				return (cname[A] + "J");
			default:
				return (cname[A] + B);
			}
		}
	}

	/**
	 * method 用來發牌給本玩家
	 */
	public void set_card(int id, int x) {
		if (id < 13 && x < 52)
			card[id] = x;
		else {
			String message = ("錯誤 id=" + id + " x=" + x);
			System.out.println(message);
		}
	}

	/**
	 * method 用來發牌給本玩家, 使用overload 來處理沒有設定第幾張的時機
	 */
	public void set_card(int x) {
		int i, flag;
		if (x > 52 || x < 0) {
			System.out.println("錯誤  x=" + x);
			return;
		}

		// flag用來看看有沒有發到牌
		flag = 0;
		for (i = 0; i < 13; i = i + 1) {

			// 那個位置沒有牌
			if (card[i] == -1) {
				card[i] = x;
				flag = 1;
				break; // 發完牌即可跳出
			}
		}

		if (flag == 0) {
			System.out.println("錯誤!本家發超過13張牌" + x);
		}
	}
}

package com.four.bots.cards;

import java.util.*;

/**
 * 使用物件來發牌動作,
 * 用物件陣列
 */
public class Example39 {
	
	// 一副牌52張,固定數值。
    private static final int defaultCnt = 52;
	
	@SuppressWarnings("static-access")
	public static void main(String[] args) {
		
		int i, j, x;
		Random ran = new Random();
		
		// 設定發13張牌
		final int num = 13; 
		// 記錄該位牌是否已被發出
		int[] dcard = new int[defaultCnt]; 
		// 定義四家玩家,配置空間給陣列
		player39[] p = new player39[4];
		for (i = 0; i < p.length; i = i + 1) {
			// 建置每個元素配置物件
			p[i] = new player39(); 
			p[i].show_cnt();
		}
		
		/* ---------- 起始值設定 ---------- */
		for (i = 0; i < defaultCnt; i = i + 1) {
			dcard[i] = 0;
		}

		/* ---------- 亂數產生發牌 ---------- */
		for (i = 0; i < p.length; i = i + 1) {
			// 發下一家
			for (j = 0; j < num; j = j + 1) {
				x = ran.nextInt(defaultCnt);
				while (dcard[x] != 0) {
					x = ran.nextInt(defaultCnt);
				}
				p[i].set_card(x);
				dcard[x] = 1;
			}
		}

		/* ---------- 列印顯示 ---------- */
		for (i = 0; i < p.length; i = i + 1) {
			System.out.println("第 " + (i + 1) + " 家的牌如下:");
			for (j = 0; j < num; j = j + 1) {
				System.out.print(p[i].show(j) + " ");
			}
			System.out.println();
		}
	}
}

class player39 {
	// 記錄手上的牌
	private int[] card = new int[13];
	// 共用的對照表可以用static
	private static String[] cname = { "梅花", "方塊", "紅心", "黑桃" };
	// 共用的物件技術器
	private static int obj_cnt = 0;
	// 一副牌52張,固定數值。
	private static final int defaultCnt = 52;
	
	/**
	 * 建構子,首先把牌清乾淨
	 */
	public player39() {
		int i;
		for (i = 0; i < 13; i = i + 1) {
			this.card[i] = -1;
		}
		obj_cnt = obj_cnt + 1;// 登記多了一家
	}

	/**
	 * method 用來show出共有幾個物件
	 */
	public static void show_cnt() {
		String message = ("目前共有" + obj_cnt + "家");
		System.out.println(message);
	}

	/**
	 * method 用來show出第幾張牌
	 */
	public String show(int id) {
		int A, B;
		if (card[id] == -1) {
			return ("沒有這張牌");
		} else {
			A = card[id] % 4;
			B = card[id] / 4;
			B = B + 2;

			switch (B) {
			case 14:
				return (cname[A] + "A");
			case 13:
				return (cname[A] + "K");
			case 12:
				return (cname[A] + "Q");
			case 11:
				return (cname[A] + "J");
			default:
				return (cname[A] + B);
			}
		}
	}

	/**
	 * method 用來發牌給本玩家
	 */
	public void set_card(int id, int x) {
		if (id < 13 && x < defaultCnt) {
			card[id] = x;
		} else {
			String message = ("錯誤 id=" + id + " x=" + x);
			System.out.println(message);
		}
	}

	/**
	 * method 用來發牌給本玩家, 使用overload 來處理沒有設定第幾張的時機
	 */
	public void set_card(int x) {
		int i, flag;
		if (x > defaultCnt || x < 0) {
			System.out.println("錯誤  x=" + x);
			return;
		}

		// flag用來看看有沒有發到牌
		flag = 0;
		for (i = 0; i < 13; i = i + 1) {

			// 那個位置沒有牌
			if (card[i] == -1) {
				card[i] = x;
				flag = 1;
				break; // 發完牌即可跳出
			}
		}

		if (flag == 0) {
			System.out.println("錯誤!本家發超過13張牌" + x);
		}
	}
}

package com.four.bots.cards;

import java.util.*;

/**
 * 使用物件來發牌動作, 
 * 用物件陣列,
 * 利用物件當method 的參數
 */
public class Example40 {

	// 一副牌52張,固定數值。
	private static final int defaultCnt = 52;

	@SuppressWarnings("static-access")
	public static void main(String[] args) {
		int i, j, x;
		Random ran = new Random();

		// 設定發13張牌
		final int num = 13;
		// 記錄該位牌是否已被發出
		int[] dcard = new int[defaultCnt];
		// 定義四家玩家,配置空間給陣列
		player40[] p = new player40[4];
		for (i = 0; i < p.length; i = i + 1) {
			// 建置每個元素配置物件
			p[i] = new player40();
			p[i].show_cnt();
		}

		/* ---------- 起始值設定 ---------- */
		for (i = 0; i < defaultCnt; i = i + 1) {
			dcard[i] = 0;
		}

		/* ---------- 亂數產生發牌 ---------- */
		for (i = 0; i < p.length; i = i + 1) {
			// 發下一家
			for (j = 0; j < num; j = j + 1) {
				x = ran.nextInt(defaultCnt);
				while (dcard[x] != 0) {
					x = ran.nextInt(defaultCnt);
				}
				p[i].set_card(x);
				dcard[x] = 1;
			}
		}

		/* ---------- 列印顯示 ---------- */
		for (i = 0; i < p.length; i = i + 1) {
			show_user(p[i]);
		}
	}

	public static void show_user(player40 p) {
		System.out.println("牌如下:");
		for (int j = 0; j < 13; j = j + 1)
			System.out.print(p.show(j) + " ");
		System.out.println();
	}
}

class player40 {
	// 記錄手上的牌
	private int[] card = new int[13];
	// 共用的對照表可以用static
	private static String[] cname = { "梅花", "方塊", "紅心", "黑桃" };
	// 共用的物件技術器
	private static int obj_cnt = 0;
	// 一副牌52張,固定數值。
	private static final int defaultCnt = 52;

	/**
	 * 建構子,首先把牌清乾淨
	 */
	public player40() {
		int i;
		for (i = 0; i < 13; i = i + 1) {
			this.card[i] = -1;
		}
		obj_cnt = obj_cnt + 1;// 登記多了一家
	}

	/**
	 * method 用來show出共有幾個物件
	 */
	public static void show_cnt() {
		String message = ("目前共有" + obj_cnt + "家");
		System.out.println(message);
	}

	/**
	 * method 用來show出第幾張牌
	 */
	public String show(int id) {
		int A, B;
		if (card[id] == -1) {
			return ("沒有這張牌");
		} else {
			A = card[id] % 4;
			B = card[id] / 4;
			B = B + 2;

			switch (B) {
			case 14:
				return (cname[A] + "A");
			case 13:
				return (cname[A] + "K");
			case 12:
				return (cname[A] + "Q");
			case 11:
				return (cname[A] + "J");
			default:
				return (cname[A] + B);
			}
		}
	}

	/**
	 * method 用來發牌給本玩家
	 */
	public void set_card(int id, int x) {
		if (id < 13 && x < defaultCnt) {
			card[id] = x;
		} else {
			String message = ("錯誤 id=" + id + " x=" + x);
			System.out.println(message);
		}
	}

	/**
	 * method 用來發牌給本玩家, 使用overload 來處理沒有設定第幾張的時機
	 */
	public void set_card(int x) {
		int i, flag;
		if (x > defaultCnt || x < 0) {
			System.out.println("錯誤  x=" + x);
			return;
		}

		// flag用來看看有沒有發到牌
		flag = 0;
		for (i = 0; i < 13; i = i + 1) {

			// 那個位置沒有牌
			if (card[i] == -1) {
				card[i] = x;
				flag = 1;
				break; // 發完牌即可跳出
			}
		}

		if (flag == 0) {
			System.out.println("錯誤!本家發超過13張牌" + x);
		}
	}
}