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 原語 閱讀(371) 評論(0)  編輯  收藏


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


          網站導航:
           
          主站蜘蛛池模板: 抚州市| 横峰县| 旺苍县| 友谊县| 信宜市| 稷山县| 丹棱县| 昌图县| 宁德市| 牟定县| 台湾省| 全南县| 石屏县| 凯里市| 砚山县| 习水县| 和静县| 沙河市| 白银市| 东丽区| 延庆县| 尚志市| 乐东| 祁阳县| 开封县| 无极县| 修文县| 静安区| 江川县| 临江市| 兴义市| 元氏县| 尼玛县| 奈曼旗| 南部县| 西安市| 遵化市| 正镶白旗| 湘乡市| 旺苍县| 西充县|