Java 程式範例基礎集合

four bots

Java 程式範例基礎集合

108學年01學期,四筒老師在課堂上課講解、立即上機實作練習。

// Package(套件)之機制。
package com.four.bots.classroom;

/**
 * hello world範例
 * */
public class Example01 {

	public static void main(String[] args) {// 程式進入點
		System.out.println("Hello World!");
	}

}


package com.four.bots.classroom;

import javax.swing.JOptionPane;

/**
 * GUI的hello world範例
 * */
public class Example02 {

	public static void main(String[] args) {
		String data = "Hello World!\nStudents!";
		
		// 在視窗中印出字串
		JOptionPane.showMessageDialog(null, data); 
		System.exit(0);
	}

}


package com.four.bots.classroom;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;

/**
 * DOS命令列輸入範例
 */
public class Example03 {

	public static void main(String[] args) {
		// 宣告變數名稱
		String data = null;
		InputStream is = System.in;
		Reader read = new InputStreamReader(is);
		BufferedReader br = new BufferedReader(read);
		
		System.out.print("請輸入你的名字:");
		try {
			data = br.readLine();
			// 列印顯示
			System.out.println("Hello, " + data + "!");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}


package com.four.bots.classroom;

/**
 * 變數宣告範例
 */
public class Example04 {

	public static void main(String[] args) {
		int num;
		int p1, p2 = 5, p3 = 8;

		double f1 = 0.2467, f2 = 12.48;

		long a1;
		num = 10;
		p1 = 3;

		// 列印顯示
		System.out.println("num=" + num);
		System.out.println("p1=" + p1 + "    p2=" + p2 + "    p3=" + p3);
		System.out.println("f1=" + f1 + "    f2=" + f2);

		// 計算
		f2 = (f1 * num);
		System.out.println("f1*num=" + f2);

		// 長整數
		a1 = 32998399887L;
		System.out.println("long number=" + a1);
	}

}


package com.four.bots.classroom;

/**
 * 變數溢位範例
 */
public class Example05 {

	public static void main(String[] args) {
		int p1;
		p1 = java.lang.Integer.MAX_VALUE;

		// 列印顯示
		System.out.println("p1=" + p1);
		System.out.println("p1+1=" + (p1 + 1));
		System.out.println("p1+2=" + (p1 + 2));
		System.out.println("new p1+2=" + ((long) p1 + 2));
	}

}


package com.four.bots.classroom;

/**
 * 物件導向觀念之變數宣告
 */
public class Example06 {
	// 全域變數宣告
	static int num;
	static int p1, p2 = 5, p3 = 8;
	static double f1 = 0.2467, f2 = 12.48;
	static long a1;

	public static void main(String[] args) {
		num = 10;
		p1 = 3;

		// 列印顯示
		System.out.println("num=" + num);
		System.out.println("p1=" + p1 + "    p2=" + p2 + "    p3=" + p3);
		System.out.println("f1=" + f1 + "    f2=" + f2);

		// 計算
		f2 = (f1 * num);
		System.out.println("f1*num=" + f2);

		// 長整數
		a1 = 32998399887L;
		System.out.println("long number=" + a1);

	}

}


package com.four.bots.classroom;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;

/**
 * 圓形面積計算
 */
public class Example07 {

	public static void main(String[] args) {
		String getbr = null;
		double radius, area;
		double pi = 3.14159;

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

		System.out.print("請輸入圓形半徑:");
		try {
			getbr = br.readLine();
			radius = Double.parseDouble(getbr);
			area = (pi * radius * radius);
			
			// 列印顯示
			System.out.println("圓形面積等於:" + area);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}


package com.four.bots.classroom;

/**
 * 展示型態轉換
 */
public class Example08 {

	public static void main(String[] args) {
		double r;
		int i;
		
		r = 10.3;
		i = (int) r;
		
		// 列印顯示
		System.out.println("科學表示數值是:" + r + " 轉換數值是:" + i);
	}

}


package com.four.bots.classroom;

/**
 * 展示型態轉換與精確度喪失
 */
public class Example09 {

	public static void main(String[] args) {
		double r;
		int i = 13, j = 3;
		float m;

		// 取整數
		r = (i / j);
		System.out.println("1.數值是:" + (int)r);

		// 保證精確度
		r = (double) i / j;
		System.out.println("2.轉換後數值是:" + r);

		m = (float) r;
		System.out.println("3.這也要強制轉換:" + m);
	}

}


package com.four.bots.classroom;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;

/**
 * 展示邏輯運算的用法
 */
public class Example10 {
	public static void main(String args[]) throws Exception {
		// 宣告變數
		int i;
		double r;
		boolean ans;
		String getbr;
		InputStream is = System.in;
		Reader read = new InputStreamReader(is);
		BufferedReader br = new BufferedReader(read);

		r = 10.3;
		
		ans = r > 20;
		System.out.println("r>20? " + ans);
		
		ans = r > 10;
		System.out.println("r>10? " + ans);
		
		/***** 處理複雜邏輯運算 ***********/
		System.out.print("請輸入一個數字:");
		
		getbr = br.readLine();
		i = Integer.parseInt(getbr);
		ans = (i < 20) && (i > 10);
		System.out.println("輸入值在10到20之間:" + ans);
		
		ans = (i > 30) || (i < 5);
		System.out.println("輸入值在大於30或小於5:" + ans);
	}
}


package com.four.bots.classroom;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;

/**
 * if 指令配合邏輯運算的用法
 */
public class Example11 {

	public static void main(String[] args) {
		// 宣告變數
		int i = 0;
		String getbr = null;
		InputStream is = System.in;
		Reader read = new InputStreamReader(is);
		BufferedReader br = new BufferedReader(read);

		/***** 請使用者輸入分數 **********/
		System.out.print("請輸入你的分數:");
		try {
			getbr = br.readLine();
			i = Integer.parseInt(getbr);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		/*========== 開始計算等距 ==========*/
		if (i <= 20) {
			System.out.println("這種分數當定了!");
		}
		if ((i > 20) && (i < 50)) {
			System.out.println("這個要補考!");
		}
		if ((i > 50) && (i < 60)) {
			System.out.println("這個應該會過!");
		}
		if ((i > 60) && (i < 80)) {
			System.out.println("這算是正常分數。");
		}
		if (i > 80) {
			System.out.println("哇!好高分!");
		}
	}

}


package com.four.bots.classroom;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;

/**
 * if - else -if 的用法,算幾種幾何圖形面積
 */
public class Example12 {

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

		double radius, height, width;

		/* ========== 請使用者輸入選項 ========== */
		System.out.print("請輸入 1.計算圓形面積 2.計算三角形面積 3.矩形面積:");

		try {
			getbr = br.readLine();
			i = Integer.parseInt(getbr);
			
			/* ========== 開始計算 ========== */
			if (i == 1) {
				// 圓形
				System.out.print("請輸入半徑:");
				getbr = br.readLine();
				radius = Double.parseDouble(getbr);
				s = "半徑 " + radius + " 之圓形面積是 " + 3.14159 * radius * radius;
				System.out.println(s);
			
			} else if (i == 2) {
				// 三角形
				System.out.print("請輸入底:");
				getbr = br.readLine();
				width = Double.parseDouble(getbr);
				System.out.print("請輸入高:");
				getbr = br.readLine();
				height = Double.parseDouble(getbr);
				s = "底 " + width + " 高 " + height + " 之三角形面積是" + width * height / 2;
				System.out.println(s);
			
			} else if (i == 3) {
				// 矩形
				System.out.print("請輸入長:");
				getbr = br.readLine();
				width = Double.parseDouble(getbr);
				System.out.print("請輸入寬:");
				getbr = br.readLine();
				height = Double.parseDouble(getbr);
				s = "長 " + width + " 寬 " + height + " 之矩形面積是" + width * height;
				System.out.println(s);
			
			} else {
				System.out.println("你輸入超出預期的參數,只請你輸入1到3數字選項");
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}


package com.four.bots.classroom;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;

/**
 * 用switch 重複工作,只接收數值
 */
public class Example13 {

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

		double radius, height, width;

		/* ========== 請使用者輸入選項 ========== */
		System.out.print("請輸入 1.計算圓形面積 2.計算三角形面積 3.矩形面積:");

		try {
			getbr = br.readLine();
			i = Integer.parseInt(getbr);
			
			/* ========== 開始計算 ========== */
			switch (i) {
			case 1: // (圓形)--------------------
				System.out.print("請輸入半徑:");
				getbr = br.readLine();
				radius = Double.parseDouble(getbr);
				s = "半徑 " + radius + " 之圓形面積是 " + 3.14159 * radius * radius;
				System.out.println(s);
				break;
			case 2: // (三角形)--------------------
				System.out.print("請輸入底:");
				getbr = br.readLine();
				width = Double.parseDouble(getbr);
				System.out.print("請輸入高:");
				getbr = br.readLine();
				height = Double.parseDouble(getbr);
				s = "底 " + width + " 高 " + height + " 之三角形面積是" + width * height / 2;
				System.out.println(s);
				break;
			case 3: // (矩形)--------------------
				System.out.print("請輸入長:");
				getbr = br.readLine();
				width = Double.parseDouble(getbr);
				System.out.print("請輸入寬:");
				getbr = br.readLine();
				height = Double.parseDouble(getbr);
				s = "長 " + width + " 寬 " + height + " 之矩形面積是" + width * height;
				System.out.println(s);
				break;
			default:
				System.out.println("只請你輸入1到3?!");
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}


package com.four.bots.classroom;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;

/**
 * 用while 迴圈來重複運算
 */
public class Example14 {

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

		double radius, height, width;

		while (i != 0) {
			try {
				/* ========== 請使用者輸入選項 ========== */
				System.out.print("請輸入 0. 結束 1.計算圓形面積 2.計算三角形面積 3.矩形面積:");
				getbr = br.readLine();
				i = Integer.parseInt(getbr);
				
				/* ========== 開始計算 ========== */
				switch (i) {
				case 0: // (結束)----------------------
					System.out.println("謝謝使用!");
					break;
				case 1: // (圓形)----------------------
					System.out.print("請輸入半徑:");
					getbr = br.readLine();
					radius = Double.parseDouble(getbr);
					s = "半徑 " + radius + " 之圓形面積是 " + 3.14159 * radius * radius;
					System.out.println(s);
					break;
				case 2: // (三角形)----------------------
					System.out.print("請輸入底:");
					getbr = br.readLine();
					width = Double.parseDouble(getbr);
					System.out.print("請輸入高:");
					getbr = br.readLine();
					height = Double.parseDouble(getbr);
					s = "底 " + width + " 高 " + height + " 之三角形面積是" + width * height / 2;
					System.out.println(s);
					break;
				case 3: // (矩形)----------------------
					System.out.print("請輸入長:");
					getbr = br.readLine();
					width = Double.parseDouble(getbr);
					System.out.print("請輸入寬:");
					getbr = br.readLine();
					height = Double.parseDouble(getbr);
					s = "長 " + width + " 寬 " + height + " 之矩形面積是" + width * height;
					System.out.println(s);
					break;
				default:
					System.out.println("重新請你輸入1到3");
				} // switch 結束
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		} // While 結束
	}

}


package com.four.bots.classroom;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;

/**
 * 用do while 迴圈來達成指定的功能
 */
public class Example15 {

	public static void main(String[] args) {
		int i = 0;
		String s = null;
		String getbr = null;
		InputStream is = System.in;
		Reader read = new InputStreamReader(is);
		BufferedReader br = new BufferedReader(read);
		
		double radius, height, width;
		
		do {
			try {
				/* ========== 請使用者輸入選項 ========== */
				System.out.print("請輸入 0. 結束 1.計算圓形面積 2.計算三角形面積 3.矩形面積:");
				getbr = br.readLine();
				i = Integer.parseInt(getbr);
				
				/* ========== 開始計算 ========== */
				switch (i) {
				case 0: // (結束)----------------------
					System.out.println("謝謝使用!");
					break;
				case 1: // (圓形)----------------------
					System.out.print("請輸入半徑:");
					getbr = br.readLine();
					radius = Double.parseDouble(getbr);
					s = "半徑 " + radius + " 之圓形面積是 " + 3.14159 * radius * radius;
					System.out.println(s);
					break;
				case 2: // (三角形)----------------------
					System.out.print("請輸入底:");
					getbr = br.readLine();
					width = Double.parseDouble(getbr);
					System.out.print("請輸入高:");
					getbr = br.readLine();
					height = Double.parseDouble(getbr);
					s = "底 " + width + " 高 " + height + " 之三角形面積是" + width * height / 2;
					System.out.println(s);
					break;
				case 3: // (矩形)----------------------
					System.out.print("請輸入長:");
					getbr = br.readLine();
					width = Double.parseDouble(getbr);
					System.out.print("請輸入寬:");
					getbr = br.readLine();
					height = Double.parseDouble(getbr);
					s = "長 " + width + " 寬 " + height + " 之矩形面積是" + width * height;
					System.out.println(s);
					break;
				default:
					System.out.println("重新請你輸入1到3");
				} // switch 結束
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} while (i != 0); // do While 結束
	}

}


package com.four.bots.classroom;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;

/**
 * for 迴圈計算階乘
 */
public class Example16 {

	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 = 0, ans = 0;
		/* ========== 請使用者輸入選項 ========== */
		System.out.print("請輸入要計算階乘的數字:");

		try {
			getbr = br.readLine();
			x = Integer.parseInt(getbr);
			ans = 1;
			for (i = 1; i <= x; i = i + 1) {
				ans = ans * i;
			} // for 結束
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// 列印顯示
		System.out.println(x + "!=" + ans);
	}

}


package com.four.bots.classroom;

/**
 * 巢狀式,for 迴圈來計算 99 乘法表
 */
public class Example17 {

	public static void main(String[] args) {
		int i, x;
		for (i = 1; i <= 9; i = i + 1) {
			for (x = 1; x <= 9; x = x + 1) {
				System.out.println(i + " x  " + x + " = " + (x * i));
			} // 內圈 for 結束
			
			System.out.println("----------------------");
		} // 外圈for 結束
	}

}


package com.four.bots.classroom;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;

/**
 * 用while 與 break來重複計算階乘
 */
public class Example18 {

	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, ans;

		while (true) {
			try {
				/* ========== 請使用者輸入選項 ========== */
				System.out.print("請輸入要計算階乘的數字:");
				getbr = br.readLine();
				x = Integer.parseInt(getbr);
				if (x <= 0) {
					System.out.println("謝謝!再見!");
					break;
				}

				ans = 1;
				for (i = 1; i <= x; i = i + 1) {
					ans = ans * i;
				} // for 結束
				System.out.println(x + "!=" + ans);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		} // while結束
	}

}


package com.four.bots.classroom;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;

/**
 * 用while與continue來重複計算階乘
 */
public class Example19 {

	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, ans;
		x = 5;

		while (x >= 0) {
			try {
				/* ========== 請使用者輸入選項 ========== */
				System.out.print("請輸入-1以外數字計算階乘:");
				getbr = br.readLine();
				x = Integer.parseInt(getbr);
				if (x < 0) {
					System.out.println("謝謝!再見!");
					continue;
				}
				
				ans = 1;
				for (i = 1; i <= x; i = i + 1) {
					ans = ans * i;
				} // for 結束
				System.out.println(x + "!=" + ans);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} // while結束
	}

}


package com.four.bots.classroom;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;

/**
 * 建立method 來計算階乘
 */
public class Example20 {

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

		x = 5;
		while (x >= 0) {
			try {
				/* ========== 請使用者輸入選項 ========== */
				System.out.print("請輸入要計算階乘的數字:");
				getbr = br.readLine();
				x = Integer.parseInt(getbr);
				if (x < 0) {
					System.out.println("謝謝!再見!");
					continue;
				}
				System.out.println(x + "!=" + fraction(x));
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		} // while結束
	}

	private static int fraction(int y) {
		int i, ans;

		ans = 1;
		for (i = 1; i <= y; i = i + 1) {
			ans = ans * i;
		} // for 結束
		return (ans);
	}

}