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


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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 凉城县| 西吉县| 新丰县| 诏安县| 安新县| 双流县| 武义县| 阳西县| 灵石县| 延津县| 绿春县| 彭阳县| 青海省| 宜良县| 香河县| 多伦县| 禄劝| 蓝田县| 宣威市| 甘孜县| 成都市| 渝中区| 砚山县| 乐昌市| 浦江县| 迁安市| 峨眉山市| 长沙市| 四子王旗| 蓝田县| 肇东市| 芒康县| 凌源市| 平南县| 吉林市| 海城市| 武宣县| 陕西省| 孙吴县| 平陆县| 扎鲁特旗|