hays

          海納百川
          posts - 25, comments - 48, trackbacks - 0, articles - 0
            BlogJava :: 首頁 ::  :: 聯系 :: 聚合  :: 管理

          編寫一個小程序的的想法

          Posted on 2006-06-04 14:59 hays(海納百川) 閱讀(341) 評論(1)  編輯  收藏 所屬分類: 配置
          ??????編寫一個程序:判斷一個字符是大寫還是小寫。第一眼看過去我覺的沒什么好寫的,不就是if ...else 嗎?第一次編寫的程序是下面的:
          public?class?Test?
          {
          ????
          public?static?void?main(String[]?args)
          ????
          {
          ????????
          char?temp='m';
          ????????
          if(temp>='A' && temp<='Z')
          ????????
          {
          ????????????System.out.print(temp
          +"是大寫字母");
          ????????}

          ????????
          else?if (temp>='a' && temp<='z'')
          ????????{
          ????????????
          ????????????System.out.print(temp
          +"是小寫字母");
          ????????}

          ????????
          else?
          ????????
          {
          ????????????System.out.print(temp
          +"不是字母");
          ????????}

          ?????
          ?????????????
          ????}
          ?得出的結果是“m是小寫字母”,呵呵,程序正確了;

          看了幾遍代碼后,發現代碼全密集在一個main函數中,做了下面的一些優化:
          public?class?Test?
          {
          ????
          public?static?void?main(String[]?args)
          ????
          {
          ????????
          char?temp='m';
          ????????
          if(isUpperLetter(temp))
          ????????
          {
          ????????????System.out.print(temp
          +"是大寫字母");
          ????????}

          ????????
          else?if(isLowerLetter(temp))?
          ????????
          {
          ????????????
          ????????????System.out.print(temp
          +"是小寫字母");
          ????????}

          ????????
          else?
          ????????
          {
          ????????????System.out.print(temp
          +"不是字母");
          ????????}

          ????????
          ?????????????
          ????}

          ????
          ????
          private?static??boolean?isUpperLitter(char?Symel)
          ????
          {????
          ????????
          if(Symel>='A'?&&?Symel<='Z')
          ????????
          {
          ????????????
          return??true;
          ????????}

          ????????
          return?false;
          ????}

          ????
          ????
          private?static?boolean?isLowerLitter(char?Symel)
          ????
          {????
          ????????
          if(Symel>='a'?&&?Symel<='z')
          ????????
          {
          ????????????
          return??true;
          ????????}

          ????????
          return?false;
          ????}

          ????
          }

          哎,發現優化的還不是不行。能不能用面向對象的方法來處理這個問題,我想到了創建一個MyLetter類

          package?com.vitamin.console;

          public?class?test1?{

          ????
          /**
          ?????*?
          @param?args
          ?????
          */

          ????
          public?static?void?main(String[]?args)?{
          ????????
          ????????
          //?TODO?自動生成方法存根
          ????????MyLetter?n?=new?MyLetter('1');
          ????????n.judgeOfLetter();

          ????}


          }


          public?class?MyLetter?{
          ????
          private?char?content;
          ????
          public?MyLetter()
          ????
          {
          ????????
          ????}

          ????
          public?MyLetter(char?symbol)
          ????
          {
          ????????content
          =symbol;
          ????}

          ????
          public?void?setLetter(char?symbol)
          ????
          {
          ????????
          this.content=?symbol;
          ????}

          ????
          ????
          public?char?getLetter()
          ????
          {
          ????????
          return?this.content;
          ????}

          ????
          ????
          public?void??judgeOfLetter()
          ????
          {
          ????????
          if(isUpperCase(this.content))
          ????????
          {
          ????????????System.out.print(
          this.content+"是大寫字母");
          ????????}

          ????????
          else?if(isLowerCase(this.content))?
          ????????
          {
          ????????????
          ????????????System.out.print(
          this.content+"是小寫字母");
          ????????}

          ????????
          else?
          ????????
          {
          ????????????System.out.print(
          this.content+"不是字母");
          ????????}

          ????????????
          ????????
          ????}

          ????
          private???boolean?isUpperCase(char?Symel)
          ????
          {????
          ????????
          if(Symel>='A'?&&?Symel<='Z')
          ????????
          {
          ????????????
          return??true;
          ????????}

          ????????
          return?false;
          ????}

          ????
          ????
          private??boolean?isLowerCase(char?Symel)
          ????
          {????
          ????????
          if(Symel>='a'?&&?Symel<='z')
          ????????
          {
          ????????????
          return??true;
          ????????}

          ????????
          return?false;
          ????}


          }



          哎,從小程序中還是可以學到很多東西的.

          評論

          # re: 編寫一個小程序的的想法  回復  更多評論   

          2006-06-05 08:41 by geniefox
          有這種想法,表明是思想上的一種提高

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


          網站導航:
           
          主站蜘蛛池模板: 司法| 自贡市| 屯留县| 威海市| 突泉县| 富宁县| 昌图县| 宁强县| 金塔县| 鄂伦春自治旗| 观塘区| 安图县| 庐江县| 福建省| 文登市| 镇康县| 梁河县| 石屏县| 慈利县| 台前县| 浙江省| 灵丘县| 绥德县| 张掖市| 雷山县| 石狮市| 左云县| 民权县| 高清| 郁南县| 阿鲁科尔沁旗| 视频| 新余市| 馆陶县| 胶州市| 江孜县| 长治市| 密山市| 济南市| 吉安市| 砀山县|