hays

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

          編寫一個小程序的的想法

          Posted on 2006-06-04 14:59 hays(海納百川) 閱讀(332) 評論(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
          有這種想法,表明是思想上的一種提高

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


          網站導航:
           
          主站蜘蛛池模板: 根河市| 牡丹江市| 六枝特区| 商南县| 绵阳市| 富民县| 白河县| 浪卡子县| 麟游县| 远安县| 枣庄市| 四子王旗| 凉城县| 新津县| 吉水县| 哈密市| 四川省| 吉林市| 彝良县| 曲周县| 辰溪县| 凤山市| 海门市| 南木林县| 治多县| 黑龙江省| 阳东县| 永胜县| 苗栗市| 峨山| 忻州市| 凌海市| 兴隆县| 大城县| 都兰县| 夹江县| 金溪县| 赣榆县| 渭南市| 北流市| 齐齐哈尔市|