2011年11月30日 星期三

JSP 之 SQL連線

如果你信誓旦旦的回家測試

卻看到以下畫面

曹山表示:沒有錯,你又進入了黃泉路666號

 

以上閒聊


以下正題

 

因為SQL安裝的時候預設是用WINDOWS驗證

(某軟公司自認WINDOWS驗證最安全)

所以請改成SQL Server 及 Windows驗證模式

以下圖解


以上

2011年11月24日 星期四

JSP m3_lab2

package com.web;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class m3_lab2
 */
@WebServlet("/m3_lab2")
public class m3_lab2 extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public m3_lab2() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws 

ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws 

ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html; charset=big5");
		java.io.PrintWriter out = response.getWriter();
		String [] list = {"大雄","宜靜","技安","阿福"};
		out.println(list[(int)(Math.random()*list.length)].toString());
	}

}

2011年11月19日 星期六

網頁設計 關於DIV置中的CSS設定

其實關於網頁置中排版的方法多的是

老實說瀏覽器百家爭鳴

也只能祈禱W3C那天真的能硬起來

像秦始皇一樣統一天下

車同軌 書同文 說置中就置中

 

所以說這鳥蛋問題早存在很久了

現代人學程式很簡單

不會的參照古人就好了

因為你會遇到的蠢問題 早在八百年前就有人遇過了

所以遇到問題 1.問咕狗大神 2.翻書 3.問人 4.擲杯

(用4的恭喜,因為你的問題前無古人後無來者只能問神,但是你搞得定可能就是下個假勃濕)

 

以上閒聊


以下正題

 

其實你可愛的DREAMWEAVER早就幫你設想好了

新增文件裡面的版面就很強大了

產生的程式碼還很貼心的寫了一堆註解

而且還是中文的!!

全國電子表示:揪甘心~

 

 

 

 

簡單來說置中的大原則是

body {margin: 0;}

把所有內容放進主容器container(當然你要叫啥都可)

.container {width: 960px; margin: 0 auto;}

以上

2011年11月7日 星期一

JAVA Mr.楊同學的 質數 計次法

 

 

 

 

public class 質數_計次法 {
	public static void main(String[] args) {
		for (int target = 3; target <= 100; target++) {
			int count = 0;
			for (int i = 2; i < target; i++) {
				if ((target%i) != 0) {
					count++;
				}    
			}
			if (count == (target - 2)) {
				System.out.print(target + ",");
			}   
		}
	}
}

 

 

 

 

概念是對的,原本跑不出來,是因為

int count = 0;必須在第一層迴圈之內

大家參考參考

JAVA 大樂透 使用vector

 

 

 

 

public class 魏功豪_vector {
	public static void main(String[] args) {
		int betTimes = 10;
		int balls = 49;
		int temp;
		for (int bt = 1; bt <= betTimes; bt++) {
			java.util.Vector<Integer> allBalls = new java.util.Vector<Integer>();
			for (int i = 0; i <= balls - 1; i++) {
				allBalls.addElement(i + 1);
			}
			int [] arrSixBall = new int [6];
			System.out.print("第" + bt + "注\t");
			for (int times = 0; times < 6; times++) {
				int randomTakeOneBall = (int)(Math.random()*allBalls.size());
				arrSixBall[times] = (int)allBalls.get(randomTakeOneBall);
				allBalls.remove(randomTakeOneBall);
			}

			for (int i = 0; i < 6; i++) {
				for (int j = i; j < 6; j++) {
					if (arrSixBall[i] < arrSixBall[j]) {
						temp = arrSixBall[j];
						arrSixBall[j] = arrSixBall[i];
						arrSixBall[i] = temp;
					}
				}
			}
			for (int x = 0; x < 6; x++) {
				System.out.print((arrSixBall[x] < 10) ? " " + arrSixBall[x] + ",\t" : arrSixBall[x] + ",\t");
			}   
			System.out.print("\n");
		}
	}
}

JAVA 大樂透 呼叫靜態方法

 

 

 

 

public class 魏功豪_callststic {
	public static void main(String[] args) {
		int betTimes = 10;
		int allBalls = 49;

		int [] getAllBalls;
		int [] getSixBalls;
		int [] getOrderByDESC;

		for (int bt = 1; bt <= betTimes; bt++) {
			getAllBalls = 魏功豪_callststic.functiondoArrBoxAll(allBalls);//長度[49]陣列傳進getAllBalls
			getSixBalls = 魏功豪_callststic.functionRandomGetBall(getAllBalls);//長度[6]陣列傳進getSixBalls
			getOrderByDESC = 魏功豪_callststic.functionOrderByDESC(getSixBalls);//長度[6]陣列傳進getOrderByDESC

			System.out.print("第" + bt + "注\t");
			for (int i = 0; i <= 5; i++) {
				System.out.print((getOrderByDESC[i] < 10) ? " " + getOrderByDESC[i] + ",\t" : getOrderByDESC[i] + ",\t");
			}
			System.out.print("\n");
		}
	}

	public static int [] functiondoArrBoxAll(int howManyAllBalls) {
		int [] doArrBoxAll = new int [howManyAllBalls];
		for (int i = 0; i <= howManyAllBalls - 1; i++) {
			doArrBoxAll[i] = i + 1;
		}
		return doArrBoxAll;//製造長度[49]陣列傳回,最初的49顆球陣列
	}

	public static int [] functionRandomGetBall(int [] arrOriginal) {
		int getballs = 6;
		int [] doArrSixBall = new int [getballs];

		for (int times = 1; times <= getballs; times++) {
			int randomTakeOneBall = (int)(Math.random()*arrOriginal.length);
			doArrSixBall[times-1] = arrOriginal[randomTakeOneBall];
			int [] arrNew = new int [arrOriginal.length - 1];
			for (int i = 0; i <= (arrOriginal.length - 1) - 1; i++) {
				arrNew[i] = (i < randomTakeOneBall) ? arrOriginal[i] : arrOriginal[i + 1];
			}
			arrOriginal = arrNew;
		}

		return doArrSixBall;//製造長度[6]陣列傳回,隨機取6球,未排列
	}

	public static int [] functionOrderByDESC(int [] arrRandomSixBall) {
		int temp;
		for (int i = 0; i < 6; i++) {
			for (int j = i; j < 6; j++) {
				if (arrRandomSixBall[i] < arrRandomSixBall[j]) {
					temp = arrRandomSixBall[j];
					arrRandomSixBall[j] = arrRandomSixBall[i];
					arrRandomSixBall[i] = temp;
				}
			}
		}
		return arrRandomSixBall;//製造長度[6]陣列傳回,將6球照大小排列
	}
}

JAVA 大樂透 照大小排 以陣列達成 泡沫排序法

 

 

 

 

public class 魏功豪_作業_大樂透照大小排列_泡沫排序法 {
	public static void main(String[] args) {
		int betTimes = 10;
		int allBalls = 49;
		int [] arrSixBall = new int [6];
		int temp;
		//=============================
		for (int bt = 1; bt <= betTimes; bt++) {
			int [] arrOriginal = new int [allBalls];
			for (int i = 0; i <= arrOriginal.length - 1; i++) {
				arrOriginal[i] = i + 1;
			}
			System.out.print("第" + bt + "注\t");
			for (int times = 1; times <= 6; times++) {
				int randomTakeOneBall = (int)(Math.random()*arrOriginal.length);
				arrSixBall[times-1] = arrOriginal[randomTakeOneBall];
				int [] arrNew = new int [arrOriginal.length - 1];
				for (int i = 0; i <= (arrOriginal.length - 1) - 1; i++) {
					arrNew[i] = (i < randomTakeOneBall) ? arrOriginal[i] : arrOriginal[i + 1];
				}
				arrOriginal = arrNew;
			}

			for (int i = 0; i < 6; i++) {
				for (int j = i; j < 6; j++) {
					if (arrSixBall[i] < arrSixBall[j]) {
						temp = arrSixBall[j];
						arrSixBall[j] = arrSixBall[i];
						arrSixBall[i] = temp;
					}
				}
			}
			for (int x = 0; x < 6; x++) {
				System.out.print((arrSixBall[x] < 10) ? " " + arrSixBall[x] + ",\t" : arrSixBall[x] + ",\t");
			}   
		System.out.print("\n");
		}
		//=============================
	}
}

JAVA 大樂透 照大小排 以陣列達成

 

 

 

 

public class 魏功豪_作業_大樂透照大小排列 {
	public static void main(String[] args) {
		int betTimes = 6;
		int allBalls = 49;
		int [] arrSixBall = new int [6];
		//=============================
		for (int bt = 1; bt <= betTimes; bt++) {
			int [] arrOriginal = new int [allBalls];
			for (int i = 0; i <= arrOriginal.length - 1; i++) {
				arrOriginal[i] = i + 1;
			}
			System.out.print("第" + bt + "注\t");
			for (int times = 1; times <= 6; times++) {
				int randomTakeOneBall = (int)(Math.random()*arrOriginal.length);
				arrSixBall[times-1] = arrOriginal[randomTakeOneBall];
				int [] arrNew = new int [arrOriginal.length - 1];
				for (int i = 0; i <= (arrOriginal.length - 1) - 1; i++) {
					arrNew[i] = (i < randomTakeOneBall) ? arrOriginal[i] : arrOriginal[i + 1];
				}
				arrOriginal = arrNew;
			}
			for (int x = 1; x <= 6; x++) {
				int temp = arrSixBall[0];
				int arrNo = 0;
				for (int i = 1; i <=5; i++) {
					if (temp < arrSixBall[i]) {
						temp = arrSixBall[i];
						arrNo = i;
					}
				}
				System.out.print((temp < 10) ? " " + temp + ",\t" :temp + ",\t");
				arrSixBall[arrNo] = 0;    
			}
			System.out.print("\n");
		}
		//=============================
	}
}

JAVA 大樂透 隨機取球 不重複 以陣列達成

 

 

 

 

public class 魏功豪_大樂透 {
	public static void main(String[] args) {
		int betTimes = 10;
		int balls = 49;
//================================================================================
		for (int bt = 1; bt <= betTimes; bt++) {
			int [] arrOrg = new int [balls];
			for (int i = 0; i <= balls-1; i++) {
				arrOrg[i] = i + 1;
			}
			for (int sixTime = 0; sixTime < 6; sixTime++) {
				int randomArrNo = (int)(Math.random()*(balls - sixTime));
				if (arrOrg[randomArrNo] < 10) {
					System.out.print(" ");
				}
				System.out.print(arrOrg[randomArrNo] + ",\t");
				int [] arrNew = new int [arrOrg.length -1];

				for (int i = 0; i <= arrNew.length -1; i++) {
					//arrNew[i] = (i < randomArrNo) ? arrOrg[i] : arrOrg[i + 1];//三源運算子寫法
					if (i < randomArrNo) {
						arrNew[i] = arrOrg[i];
					}
					else {
						arrNew[i] = arrOrg[i + 1];
					}
				}
				arrOrg = arrNew;
			}
			System.out.print("\n");
		}
//================================================================================
	}
}

2011年11月6日 星期日

該死的 x++ 和 ++x 到底是啥小

其實講義寫得很明白 //但是我看得很模糊

讓我們看看講義128頁 遞增運算

++

●後置 and 前置

int a = 5;

//a++ 即 a = a + 1

a++; /* 5為此後置Expression的值 */

a=5;

//++a 即 a = a + 1

++a; /* 6為此後置Expression的值 */

 

 

 

 

喔 我的天 是我眼花嗎

a++ 即 a = a + 1

++a 即 a = a + 1

那他們到底有他X的什麼不同???

 

 

 

 

我們看看曹山的說法

曹山:這一切都沒有錯,只不過你走進了黃泉路666號

 

 

 

 

以上博君一笑,以下解答

單看a++和++a的確沒啥不同 都是a = a + 1的意思

但是與其他運算式加在一起就有所分別

範例

從結果來看就很清楚

x++和++x都會重新對x的值+1

所以

第3行 X初值為10

第4行 X值改為11

第5行 X值改為12

而後面c d 前面有10 * 運算

其意義為

10 * x++ 為[10先乘x]再將[x = x + 1]

10 * ++x 為[先將]x = x + 1[再乘10]

所以

第6行可以看做

c = 10 * x;//x=12

x = x + 1;//x=13

第7行可以看做

x = x + 1;//x=14

d = 10 * x;//x=14

 

 

 

 

簡單來說

x++ 就是會先與其他算式運算 之後才 x = x + 1 將x值增1

++x 就是先 x = x + 1 將x值增1 之後才與其他算式運算

JAVA 聖誕樹 使用continue

 

 

 

 

public class 魏功豪_test_continue {
	public static void main(String[] args) {
		int start = 1;
		int end = 9;
		for (int Counter = start; Counter <= end; Counter++) {
			if (Counter % 2 == 1) {
				for (int i = 1; i <= (end - Counter) / 2; i++) {
					System.out.print(" ");
				}
			}
			for (int i = 1; i <= Counter; i++) {
				if (Counter % 2 == 0) {
					continue;
				}
				System.out.print("*");
			}
			if (Counter % 2 == 0) {
				System.out.print("\n");
			}
		}
	}
}

JAVA 聖誕樹 多層寫法

 

 

 

 

public class 魏功豪_聖誕樹 {
	public static void main (String[] args) {
		int Line = 4;
		for (int i = 1; i <= Line; i++) {
			for (int j = 0; j <= i; j++) {
				for (int k = 1; k <= (Line - j); k++) {
					System.out.print(" ");
				}
				for (int l = 1; l <= 1 + (j * 2); l++) {
					System.out.print("▲");
				}
				System.out.println("");
			}
		}
		for (int z = 1; z <= Line; z++) {
			for (int x = 1; x <= Line; x++) {
				System.out.print(" ");
			}
			System.out.println("▲");
		}
	}
}

JAVA 聖誕樹

 

 

 

 

public class h04 {
	public static void main (String[] args) {
		int Line = 5;
		for (int i = 1; i <= Line; i++) {
			for (int j = 1; j <= Line - i; j++) {
				System.out.print(" ");
			}
			for (int k = 1; k <= (i * 2) - 1; k++) {
				System.out.print("*");
			}
			System.out.println("");
		}
	}
}

2011年11月5日 星期六

JAVA 求質數

 

 

 

 

public class 魏功豪_作業_求質數 {
	public static void main(String[] args) {
		int fin = 100;
		System.out.print(2 + ",");//唯一偶數質數
		for (int i = 3; i <= fin; i++) {
			boolean isPrimeNumber = true;
			for (int j = 2; j <= (i - 1); j++) {
				if (i % j == 0) {
					isPrimeNumber = false;
				}
			}
			if (isPrimeNumber) {
				System.out.print(i + ",");
			}
		}
	}
//網上神人用continue寫的
//	public static void main(String[] arg) {
//		int x, y;
//		String result = "2\n";
//		loopA:
//			for (x = 3; x <= 100; x++) {
//				for (y = 2; y <= x - 1; y++) {
//					if (x % y == 0)
//						continue loopA;       /看不懂 -.-
//				}
//				result += x + "\n";
//			}
//		System.out.println(result);
//	}
}