JaveShare&OpenSpace

          BlogJava 首頁 新隨筆 聯(lián)系 聚合 管理
            7 Posts :: 0 Stories :: 7 Comments :: 0 Trackbacks

          Class Random的常用的Method:
          nextInt ()
          nextInt (int?n)
          nextBytes(byte[]?bytes)
          nextDouble()
          nextFloat()
          nextLong ()
          nextBoolean ()
          由于Random類中沒有nextChar(),nextString()這樣的方法,所以要隨機產(chǎn)生一個字符或字符串應(yīng)該自己手動編寫Code
          下面的例子就是一個自動產(chǎn)生字符的類
          import java.util.*;

          public class RandomChar{
          ?private static Random rand=new Random();
          ?private static String? source="ABCEDFGHIGKLMNOPQRSTUVWXYZabcedfghijklmnopqrstuvwxuyz";
          ?private static char[] sur=source.toCharArray();
          ?public static char nextChar(){
          ??return sur[rand.nextInt(source.length())];
          ?}
          ?public static void main(String args[])
          ?{
          ??System.out.println(nextChar());
          ?}
          }

          再編寫一個隨機產(chǎn)生字符串的類:
          import java.util.*;
          public? class RandomString{
          ?private static Random rand=new Random();
          private static int len;//字符串的長度
          public RandomString(int len){ this.len=len;}
          private static String?? source="ABCEDFGHIGKLMNOPQRSTUVWXYZabcedfghijklmnopqrstuvwxuyz";
          ?private static char[] sur=source.toCharArray();
          public static char nextChar(){
          ? return sur[rand.nextInt(source.length())];
          ?}
          private static String nextString(){
          char [] buf=new char[len];
          for(int i=0;i<len;i++)
          buf[i]=nextChar();
          return new String(buf);}
          ?public static void main(String args[])
          ?{
          ? RandomString randStr=new RandomString(5);
          ? System.out.println(randStr.nextString());
          ?}

          }

          ?

          ?

          posted on 2006-08-25 10:17 ~哈哈~ 閱讀(760) 評論(3)  編輯  收藏

          Feedback

          # 數(shù)產(chǎn)生的相關(guān)知識 2008-09-13 10:31 羅比碧
          ik65uiout5jhkitofjtgiktgitorehtyryjhejyyteuejurtgryet與他人剛回一機會剛回好幾個好股1就開會看見幾個機會積累回家講課·可能嘉年華講課你們講課李克穆罵你幾百看見離開嗎離開liangshanb迷你版機會卡拉me離開就剛回講課卡拉嗎了機會剛回個gvhj1jk11k  回復(fù)  更多評論
            

          # 數(shù)產(chǎn)生的相關(guān)知識 2008-09-13 10:32 羅比碧
          挺討厭和剛5tg6yh6yhk][9g6t7hj8jk90fg4d3s2a12s3sd4fvgc nmjhvmjhvnjhkdfkjxlhfszkjgbshlfjdlahg;dszjdgsklamxcajkxkjznsxdksjfzsm,vlszjdkszvfd

















            回復(fù)  更多評論
            

          # 數(shù)產(chǎn)生的相關(guān)知識 2008-09-13 10:33 羅比碧
          @羅比碧
            回復(fù)  更多評論
            


          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 嵊泗县| 铅山县| 长垣县| 邵阳县| 沙河市| 霍邱县| 星子县| 嘉荫县| 鲁山县| 敖汉旗| 波密县| 洪江市| 界首市| 昂仁县| 韶关市| 柞水县| 江门市| 从江县| 哈尔滨市| 安宁市| 饶阳县| 武义县| 江门市| 紫阳县| 广平县| 海城市| 淮阳县| 金堂县| 来宾市| 通山县| 临武县| 湖南省| 濮阳县| 周宁县| 北票市| 中西区| 临武县| 晋中市| 白玉县| 全椒县| 宝兴县|