b47617

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

          String,StringBuffer ,基本數(shù)據(jù)類型的對象包裝類

          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:如,要將字符串轉換成基本數(shù)據(jù)類型,幾乎都使用"Xxx包裝類.parseXxx"方式實現(xiàn)(一個例外是對于Boolean類,用的是getBoolean方法):要將包裝類轉換成基本數(shù)據(jù),幾乎都是"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)  編輯  收藏


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


          網(wǎng)站導航:
           
          主站蜘蛛池模板: 大埔县| 天水市| 江门市| 阳江市| 新野县| 巴马| 利辛县| 山丹县| 泽州县| 怀化市| 聂荣县| 伊宁市| 出国| 连江县| 张掖市| 通道| 轮台县| 伊宁市| 会理县| 连江县| 司法| 九龙城区| 满城县| 囊谦县| 巩义市| 宿松县| 宁城县| 万全县| 织金县| 岫岩| 株洲县| 游戏| 安吉县| 布尔津县| 兴化市| 永福县| 蓝田县| 康马县| 攀枝花市| 江孜县| 九龙坡区|