兩年前步入程式語言的漫漫長路,當時寫了很多不純熟的函式,並作成自己的函式庫--> mylib

但是因為裡面的格式、行數太混亂、中英夾雜,整理起來太困難,所以並沒有公開mylib

沒想到今年又要啟用封陳以久的java了,這次就來好好整理函式庫以fun為名公開給大家吧~

最下面有編譯器上色版本


package p002;
/*fun = function, Library */
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class fun {
	public static int Inverse(int x) /*功能:將數入的四位數字顛倒輸出*/
	{
		int y,p1,p2,p3,p4;/*P4 is MSB, p1 is LSB*/
		p1 = x%10;
		p2 = ((x-p1)/10)%10;
		p3 = ((x-p1-p2*10)/100)%10;
		p4 =((x-p1-p2*10-p3*100)/1000)%10;
		y = p1*1000 + p2*100 + p3*10 + p4;
		return(y);
	}
	
	public static int Rand(int n)  /*功能:數入n輸出0~n隨機整數亂數*/
	{return((int)(Math.random()*n));}
	
	public static int Rand(int n,int m)/*Overload*/  /*功能:數入n,m輸出n~m隨機整數亂數*/
	{return(n+Rand(m-n+1));}
	
	public static String Input(String msg) /*功能:Java的鍵盤輸入功能,可以鍵入字元字串;參數是輸入"告知使用者可以鍵入文字"的字串*/
	{
		System.out.println(msg);
		String s="";
		BufferedReader br = 
				new BufferedReader( new InputStreamReader(System.in)); 
		
		try
		{s=br.readLine();}catch(Exception e){}
		return(s);
	}
	
	public static int InputNum(String msg, String errmsg) /*功能:Java的鍵盤輸入功能,可以鍵入數字;參數是輸入"告知使用者可以鍵入文字"的字串、發生錯誤時的錯誤訊息*/
	{
		
		String  s ="";int k=0 ;boolean fg = true;
		while(fg)
		{
			fg=false;
			s = Input(msg);
			try
			{k= Integer.parseInt(s);}
			catch(Exception e){System.out.println(errmsg);fg=true;}
		}
		return(k);
	}
	
	public static String Inverse(String s)/*Overload*/ /*功能:將輸入字串顛倒輸出*/
    {
        String z="";
        int k = s.length();
        for(int i=0;i<k;i++)
        {
            z = s.substring(i,i+1) + z;
        }
        return z;
    }


public static int FindAndCount(String FromWhat,String FindWhat) /*功能:在字串中找字串累計出現次數*/
    {
        int k=0,cnt=0;
        do
        {
            k = FromWhat.indexOf(FindWhat,k);
            if(k>=0){cnt++;k=k+FindWhat.length();}
        }while(k>=0);
        
        return cnt;
    }

/*-----1A2BGAME-----*/ /*以下1A2B遊戲使用的函式*/
public static int[][] Make2DArray(int row,int column) /*功能:產生2維陣列*/
    {
        int[][] a = null;
        a = new int[row][column];
        for(int i=0;i<column;i++)
        {a[i]=new int[row];}
        
        return(a);
    }

public static boolean CheckSame(String s) /*功能:確定作為答案的字串式隨機產生,且數字不得重複*/
    {
        String p1="",p2="";boolean fg=false;
        for(int i=0;i<s.length()-1;i++)
        {
            p1=s.substring(i,i+1);p2=s.substring(i+1, s.length());
            if(p2.indexOf(p1)>=0){fg=true;break;}    
        }
        return fg;
    }

public static String GetAnswer1()/*1A2B=1XXX*/ /*功能:答案第一個數字不得為0*/
    {
        String ans ="";
        do{
            ans = String.valueOf(fun.Rand(1000, 9999));
        }while(CheckSame(ans)==true);
        
        return ans;
    }
    
    public static String GetAnswer0() /*功能:答案的第一個數字可以為0*/
    {
        String ans ="";
        do{
            ans = String.valueOf(fun.Rand(100, 9999));
            if(ans.length()==3){ans="0"+ans;}
        }while(CheckSame(ans)==true);
        
        return ans;
    }
    
    public static int CountAB(String ans, String gus){ /*功能:判斷是幾A幾B*/
        String p="";
        int A=0,B=0,k=0;
        
        for(int i=0;i<gus.length();i++){
            p = gus.substring(i, i+1);
            k = ans.indexOf(p);
            if(k>=0){
                if(k==i){A++;}else{B++;}
            }
        }
        return (A*10+B);
    }
        public static String GetGuess(String gus)/*功能:玩家輸入猜測的4個數自*/
        {
            int testgus=0;boolean fg=true;
                do{
                    do{
                        do{
                            gus = fun.Input("Plz guess 4 different numbers: ");
                        }while(fun.CheckSame(gus)==true);
                    }while(gus.length()!=4);
                    try{
                        fg = true;
                        testgus = Integer.parseInt(gus);
                    }catch(Exception e){fg = false;}
                }while(fg==false);
            return gus;
        }

}

/*fun = function, Library */
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class fun {
		public static void swap(int x){}
	
		public static int Inverse(int x)
		{
			int y,p1,p2,p3,p4;/*P4 is MSB, p1 is LSB*/
			p1 = x%10;
			p2 = ((x-p1)/10)%10;
			p3 = ((x-p1-p2*10)/100)%10;
			p4 =((x-p1-p2*10-p3*100)/1000)%10;
			y = p1*1000 + p2*100 + p3*10 + p4;
			return(y);
		}
		
		public static int Rand(int n)
		{return((int)(Math.random()*n));}
		
		public static int Rand(int n,int m)/*Overload*/
		{return(n+Rand(m-n+1));}
		
		public static String Input(String msg)
		{
			System.out.println(msg);
			String s="";
			BufferedReader br = 
					new BufferedReader( new InputStreamReader(System.in)); 
			
			try
			{s=br.readLine();}catch(Exception e){}
			return(s);
		}
		
		public static int InputNum(String msg, String errmsg)
		{
			
			String  s ="";int k=0 ;boolean fg = true;
			while(fg)
			{
				fg=false;
				s = Input(msg);
				try
				{k= Integer.parseInt(s);}
				catch(Exception e){System.out.println(errmsg);fg=true;}
			}
			return(k);
		}
		
	public static String Inverse(String s)/*Overload*/
		{
			String z="";
			int k = s.length();
			for(int i=0;i<k;i++)
			{
				z = s.substring(i,i+1) + z;
			}
			return z;
		}


	public static int FindAndCount(String FromWhat,String FindWhat)
		{
			int k=0,cnt=0;
			do
			{
				k = FromWhat.indexOf(FindWhat,k);
				if(k>=0){cnt++;k=k+FindWhat.length();}
			}while(k>=0);
			
			return cnt;
		}

	/*-----1A2BGAME-----*/
	public static int[][] Make2DArray(int row,int column)
		{
			int[][] a = null;
			a = new int[row][column];
			for(int i=0;i<column;i++)
			{a[i]=new int[row];}
			
			return(a);
		}

	public static boolean CheckSame(String s)
		{
			String p1="",p2="";boolean fg=false;
			for(int i=0;i<s.length()-1;i++)
			{
				p1=s.substring(i,i+1);p2=s.substring(i+1, s.length());
				if(p2.indexOf(p1)>=0){fg=true;break;}	
			}
			return fg;
		}

	public static String GetAnswer1()/*1A2B=1XXX*/
		{
			String ans ="";
			do{
				ans = String.valueOf(fun.Rand(1000, 9999));
			}while(CheckSame(ans)==true);
			
			return ans;
		}
		
		public static String GetAnswer0()
		{
			String ans ="";
			do{
				ans = String.valueOf(fun.Rand(100, 9999));
				if(ans.length()==3){ans="0"+ans;}
			}while(CheckSame(ans)==true);
			
			return ans;
		}
		
		public static int CountAB(String ans, String gus){
			String p="";
			int A=0,B=0,k=0;
			
			for(int i=0;i<gus.length();i++){
				p = gus.substring(i, i+1);
				k = ans.indexOf(p);
				if(k>=0){
					if(k==i){A++;}else{B++;}
				}
			}
			return (A*10+B);
		}

		public static String GetGuess(String gus)
			{
				int testgus=0;boolean fg=true;
					do{
						do{
							do{
								gus = fun.Input("Plz guess 4 different numbers: ");
							}while(fun.CheckSame(gus)==true);
						}while(gus.length()!=4);
						try{
							fg = true;
							testgus = Integer.parseInt(gus);
						}catch(Exception e){fg = false;}
					}while(fg==false);
				return gus;
			}

}
arrow
arrow
    創作者介紹
    創作者 Kuihao 的頭像
    Kuihao

    溫暖午後的金針田__孕育有趣的創新

    Kuihao 發表在 痞客邦 留言(0) 人氣()