函數是另一種常見的關係,我們常聽到的函數有二元一次函數跟一元一次函數和三角函數等等
給定兩個nonempty set X,Y,這種relation會有一個特定的對應關係,如同機器一樣經過某種運作方式將X的值轉換成Y,怎麼樣算是一個好的運作方式呢?首先,當然要每一個要方入機器的元素都成產生東西出來,所以對x ∈ X,皆存在y ∈ Y使得(x.y)∈ f,另外我們也希望相同的原料用同一個機器能夠產出固定的產品,所以對所有x ∈ X只會有維一的y ∈ Y使得(x.y)∈f,因此function的定義如下
Definition:
設X,Y為nonempty set 且 f ⊆ X ×Y, 為一個from X to Y的relation.若f滿足下列性質,則稱f為一個from X to Y的函數(function)
(1)對所有x ∈ X, 皆存在 y ∈ Y使得 (x, y) ∈ f .
Dnight 發表在 痞客邦 留言(0) 人氣(94)
Static這個關鍵字有些人理解是維一的意思,其實他指的是這個屬性是放在類別(class)那一層,不隨著物件(object)的變更去改動的,非static的屬性,每一個物件會有一個值
所以物件A的值改變了,物件B的值並不會改變,但是static的屬性你不管使用物件A、物件B去更動他的值,他的值就是固定在類別上,所以會一起更動,所以他有另一個特性:可以用類別名稱去呼叫,最簡單的例子就像是Math.pow()這種方法(method)一樣,他是static的,如果這個方法還是自己類別的static方法,甚至可以省略類別名稱直接打上方法,以下是程式範例
public class StaticDemo {
public static void main(String[] args) {
StaticDemo obj1 = new StaticDemo();
StaticDemo obj2 = new StaticDemo();
System.out.println("method1:");
System.out.println(obj1.method1());//1
System.out.println(obj2.method1());//1
System.out.println(obj1.method1());//2
System.out.println(obj2.method1());//2
System.out.println("method2:");
System.out.println(StaticDemo.method2());//1
System.out.println(method2());//2
System.out.println(obj1.method2());//3
System.out.println(obj2.method2());//4
}
private int i1=0;
public int method1(){
i1++;
return i1;
}
private static int i2=0;
//private int i2=0; //this will compile error
public static int method2(){
i2++;
return i2;
}
}
Dnight 發表在 痞客邦 留言(0) 人氣(16)
關於驗證身份證字號的方法請參考 身份證字號驗證
這個產生器的原理是隨機產生身份證字號,最後用身份證字號公式計算出最後一碼的數字為多少,再將整個字串拼起來
public class ID {
public static void main(String[] args) {
String idNumber = ID.IDRandom();
System.out.println("-------IDGeneratorTest-------");
System.out.println(idNumber);
System.out.println("-------IDGeneratorTest-------");
}
public String str="";
public static String IDRandom(){
char[] idByChar = new char[9];
String iDString = "";
//隨機生成A~Z
int x = (int)Math.floor(Math.random()*26+65);
idByChar[0] = (char) x;
//隨機生成1~2
idByChar[1] = (char)(int)Math.floor(Math.random()*2+49);
//隨機產出第3~第9個數字
for (int i=2;i<9;i++){
idByChar[i] = (char)(int)Math.floor(Math.random()*10+48);
}
//第十個數字由前九個字組成
iDString = new String(idByChar);
ID id = new ID(iDString);
int[] temp = new int[10];
temp[0]= id.D0();
for(int i=1;i<9;i++){
temp[i]=id.DNumber(i);
}
//最後將字組起來送出
temp[9]=(10-(id.CheckCode(temp)%10))%10;
iDString = iDString+temp[9];
return iDString;
}
//D0是找第一個字的代碼,因為有些數字不規律所以要用很多if做判斷
private int D0(){
int D00=0;
int temp = this.str.codePointAt(0);
if(72>=temp && temp>=65)
{
D00 = temp-55;
}else if(78>=temp&&temp>=74){
D00 = temp-56;
}else if(86>=temp&&temp>=80){
D00 = temp-57;
}else if(90>=temp&&temp>=88)
{
D00 =temp-58;
}
switch(temp){
case 74 :
D00 =temp-39;
case 79 :
D00 =temp-44;
break;
case 87 :
D00 =temp-55;
break;
default:
break;
}
//這邊是用來檢查使用者輸入小寫時的判斷式
if(104>=temp && temp>=97)
{
D00 = temp-87;
}else if(110>=temp&&temp>=106){
D00 = temp-88;
}else if(118>=temp&&temp>=112){
D00 = temp-89;
}else if(122>=temp&&temp>=120)
{
D00 =temp-90;
}
switch(temp){
case 106 :
D00 =temp-71;
case 111 :
D00 =temp-76;
break;
case 119 :
D00 =temp-87;
break;
default:
break;
}
return D00;
}
//DNumber用來撿查第2~第10個字,所以判斷式簡單許多
private int DNumber(int i){
int D1=100;
//這個初始100只是用來表示如果他不是0~9這個值就顯示100
//也可以用其他數值來表示,但是要避免使用0~9
int temp = this.str.codePointAt(i);
if(57>=temp&&temp>=48){
D1=temp-48;
}
return D1;
}
//這邊只是輔助計算身份證是否正確
private int CheckCode(int[] X){
int x1=Math.floorDiv(X[0],10);
int x2=X[0]%10;
int Y=x1+(9*x2)+(8*X[1]);
for(int i=2;i<=8;i++)
{
Y=(9-i)*X[i]+Y;
}
return Y;
}
public void ChangeID(String s){
this.str=s;
}
public ID(String s){
this.str=s;
}
public ID(){
}
}
Dnight 發表在 痞客邦 留言(0) 人氣(728)
關於身份證的規則請查照中華民國國民身份證wiki
codePointAt(i)這個方法是用來取出字串第i位置的字的編碼(左邊數來第一個字位置為0)ASCII table請參照此ASCII wiki
如果要找的是產生器請看此:身份證字號產生器
Dnight 發表在 痞客邦 留言(0) 人氣(887)
在Java中,如果要將數字轉換成字元或字串,或是反過來將字元字串轉成數字的時候,要充份了解互相轉換的機制,比如說
(char)49會將49以編碼的方式轉換,於是顯示出來的數字是1
如果想要將數字轉換成對應的字元,我通常會先轉換成字串再轉成字元
或許有更好的方法,以下列出各種字元/字串對應成整數的方法
public class demo {
public static void main(String[] args) {
int i = 1;
System.out.println("int to String");//整數轉換成字串
System.out.println(Integer.toString(i));//1 String
System.out.println(""+i);//1 String
System.out.println("int to char");
System.out.println("");
int i2 = 49;
//整數強制型別轉換成字元會當成編碼解讀
System.out.println((char)i2);//1 char
//轉換成字串在拆成字元
System.out.println((""+i2).charAt(0));//4 char
System.out.println((""+i2).charAt(1));//9 char
System.out.println("");
char ch = '1';
System.out.println("char to int");
//強制型別轉換會將字元轉換成編碼數字
System.out.println((int)ch);//49 int
//Character.getNumbericValue()才能取到原來數字
System.out.println(Character.getNumericValue(ch));//1 int
System.out.println("");
String str ="1";
System.out.println("String to int");
//Inter.parseInt()可將字串轉為數字
System.out.println(Integer.parseInt(str));//1 int
//需要讀取字串的編碼數字需要用codePointAt()
System.out.println(str.codePointAt(0));//49 int, ascii of 1
System.out.println("");
}
}
Dnight 發表在 痞客邦 留言(0) 人氣(2,231)
昨天聽到朋友說表單有個disable的功能
javascript可以使用.disabled=false和.disabled=true來開關按紐或是輸入表格的地方
於是我試著實作出驗證的表單
結果網頁在此
範例輸入
姓名:張三
密碼:a123@A
日期:2015/07/05
Dnight 發表在 痞客邦 留言(0) 人氣(12)
Relation中還有一種特別的關係,叫做Order relation,所謂的排序關係符合一些特別的性質,以下是介紹。
假設 X 為 nonempty set 且≼ 為 X 上的 relation. 若X符合以下三種性質,我們稱≼為X上的partial order
1.對所有的x ∈ X,皆有 x ≼ x
2.若x,y ∈ X,x≼y且y≼ x,則 x=y
3.若x,y,z ∈ X ,x≼y且y≼z,則 x≼z
Dnight 發表在 痞客邦 留言(0) 人氣(149)
在Java裡面時間轉換格式是一個很麻煩的課題
因為時間的表達方法各種各樣,有些人寫2015/07/03,有些人寫07/03/2015,有些人寫July 03 /2015,麻煩的是07/03到底表示的是三月七號還是七月三號呢?
所以在Java裡面有個class來格式化日期的類別叫SimpleDateFormat
關於格式的設定詳情請看api文件Date and Time Patterns
格式設定的語法大概如下
Dnight 發表在 痞客邦 留言(0) 人氣(618)
接續Java樂透的程式,解說就請參考Java那邊
要看結果請點下面網頁
結果如此網頁
| |
| <html>
|
| <head lang="zh-tw">
|
| <meta charset="UTF-8">
|
| <title>JavaScript Test</title>
|
| <style>
|
| |
| .type{
|
| border:1px solid #FFC010;
|
| border-radius: 15px;
|
| width: 500px;
|
| |
| }
|
| |
| .title{
|
| width:300px;
|
| border-bottom:1px solid #000000;
|
| border:3px;
|
| padding:right;
|
| }
|
| .t {
|
| width:100px;
|
| float:left;
|
| margin: 10px;
|
| text-align: right;
|
| vertical-align:center;
|
| |
| }
|
| |
| </style>
|
| |
| <script>
|
| |
| |
| function check(){
|
| if(parseInt(reg.balls.value)>=parseInt(reg.token.value)){
|
| takeBalls(reg.balls.value,reg.token.value);
|
| }else {
|
| window.alert("輸入的內容無法進行抽取喔~");
|
| }
|
| return undefined;
|
| }
|
| |
| |
| function takeBalls(balls, token){
|
| var result = new Array(token);
|
| |
| //document.write("<div style=\"border:2px solid #FFCC00; width:400px;border-radius: 20px;text-align: center; \">")
|
| document.getElementById("resultset").innerHTML = '<p style="color:#00DDAA">抽取結果如下</p>';
|
| //document.write("<p style=\"color:#00DDAA\">抽取結果如下</p>");
|
| for(var i=0;i<token;i++){
|
| result[i]=parseInt(Math.random()*balls+1);
|
| for(var j=0;j<i;j++){
|
| if(result[j]==result[i]){
|
| i--;
|
| }
|
| }
|
| }
|
| result.sort(sortNumber);
|
| for (var i = 0; i < token; i++) {
|
| var OriginalFont = document.getElementById("resultset").innerHTML;
|
| document.getElementById("resultset").innerHTML = OriginalFont + '<span style="color:#3377FF ">' + result[i] + '號</sapn>';
|
| //document.write("<span style=\"color:#3377FF \">"+result[i]+"號</sapn>");
|
| }
|
| |
| //document.write("<br><a href=\"js.html\">返回</a></div>");
|
| |
| return undefined;
|
| }
|
| function sortNumber(a,b)
|
| {
|
| return a - b;
|
| }
|
| |
| </script>
|
| |
| |
| </head>
|
| |
| <body>
|
| |
| |
| |
| <form action="#" method="post" name="reg">
|
| <fieldset class="type">
|
| <legend>樂透抽取程式</legend>
|
| <div class="title">
|
| <label class="t">總共號碼數</label><input type="number" name="balls" min="1" required="required">
|
| </div>
|
| <br>
|
| <div class="title">
|
| <label class="t">取球數</label><input type="number" name="token" min="1" required="required">
|
| </div>
|
| <br>
|
| |
| |
| <input type="button" value="確認" onclick="check()" >
|
| <input type="reset" value="清除">
|
| <br>
|
| <center id="resultset"></center>
|
| </fieldset>
|
| |
| |
| |
| </form>
|
| |
| |
| |
| </body>
|
| |
| </html> |
Dnight 發表在 痞客邦 留言(0) 人氣(561)
這是一個很經典的題目,從1~49,
隨機抓取六個不同的數字
在Java上要隨機抽取數字不難,用 java.util.Random()或是Math.random()都可以
我是習慣用Math.random()這個方法
Math.random()的取值是0<=Math.random()<1,所以要取1~49必須要寫成
Dnight 發表在 痞客邦 留言(0) 人氣(1,175)