b47617

          攀登!進步!
          隨筆 - 20, 文章 - 1, 評論 - 0, 引用 - 0
          數據加載中……

          String,StringBuffer ,基本數據類型的對象包裝類

          e.g1:編寫一個程序來簡單了解String類的使用.程序一行一行地讀取從鍵盤上不停輸入的字符串,并打印顯示.直到輸入一行"bye"為止.
          ?class ReadLine{
          ???????? public static void main(String args[]){
          ?????????????????? byte[] buf? = new byte[1024];
          ?????????????????? int ch = 0;
          ?????? int pos = 0;
          ?????? String strInfo = null;
          ??????????????????? while(true){
          ?????????????????????????? try{
          ?????????????????????????????????? ch = System.in.read();
          ?????????????????????????? }catch(Exception e){
          ??????????????????????????????????? System.out.println(e.getMessage());
          ?????????????????????????? }
          ?????? switch(ch){
          ??????? case '\r':
          ???????? break;
          ??????? case '\n':
          ???????? strInfo = new String(buf,0,pos);//在buf中提取0至pos元素.
          ??????? if(strInfo.equals("bye"))
          ???????? return;
          ??????? else{
          ???????? System.out.println(strInfo);
          ???????? pos = 0;
          ???????? break;
          ??????? }
          ??????? default:
          ???????? buf[pos++] = (byte)ch;
          ?????? }
          ??
          ?????}
          ??????? }
          ?}
          e.g2:如,要將字符串轉換成基本數據類型,幾乎都使用"Xxx包裝類.parseXxx"方式實現(一個例外是對于Boolean類,用的是getBoolean方法):要將包裝類轉換成基本數據,幾乎都是"Xxx包裝類對象.xxxVaue"方式.
          class TestInteger{
          ?public static void main(String[] args){
          ??int w = Integer.parseInt(args[0]);
          ??int h = new Integer(args[1]).intValue();
          ??for(int i = 0; i < w;i++){
          ???StringBuffer str = new StringBuffer();
          ???for(int j = 0 ; j < h;j++){
          ????str.append("*");
          ???}
          ???System.out.println(str.toString());
          ??}

          ?}
          }

          posted on 2006-03-20 10:59 原語 閱讀(373) 評論(0)  編輯  收藏


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


          網站導航:
           
          主站蜘蛛池模板: 汤原县| 苍山县| 义乌市| 东莞市| 德格县| 老河口市| 长春市| 洪湖市| 班戈县| 长寿区| 瓮安县| 清河县| 建平县| 鹤岗市| 苏尼特左旗| 鲁甸县| 恩施市| 庆阳市| 沧源| 红原县| 新丰县| 金湖县| 安国市| 黑水县| 德兴市| 乳源| 大渡口区| 青冈县| 皋兰县| 蒙阴县| 盐津县| 天镇县| 华容县| 泾川县| 辽宁省| 无棣县| 南川市| 个旧市| 曲靖市| 墨竹工卡县| 舒兰市|