某外包公司筆試題

          編程題:去掉字符串中多余的"0",
          例如
          0-->0
          3-->3
          000-->0
          01010-->101
          301100-->3011
          00103-->103
          1020.00-->1020
          001.0100-->1.01
          00.003-->0.003
          若字符串中字符(,E+-)則為非法字符串。

          public class RemoveZero {

              
          /**
                   * 
          @param args
                   
          */
              
          static String removeFirst(String str) {
              
          while (true) {
                  
          if (str.length() > 1) {
                  
          char c = str.charAt(0);
                  
          char nextC = str.charAt(1);
                  
          if (c != '0') {
                      
          break;
                  } 
          else {
                      
          if (nextC == '.') {
                      
          break;
                      } 
          else {
                      str 
          = str.substring(1);
                      }
                  }
                  } 
          else {
                  
          break;
                  }
              }
              
          return str;
              }

              
          static String removeLast(String str) {
              
          while (true) {
                  
          if (str.length() > 1) {
                  
          char c = str.charAt(str.length() - 1);
                  
          char beforeC = str.charAt(str.length() - 2);
                  
          if (c != '0') {
                      
          break;
                  } 
          else {
                      
          if (beforeC == '.') {
                      str 
          = str.substring(0, str.length() - 2);
                      
          break;
                      } 
          else {
                      str 
          = str.substring(0, str.length() - 1);
                      }
                  }
                  } 
          else {
                  
          break;
                  }
              }
              
          return str;
              }

              
          static boolean isCorrect(String str) {
              
          return !str.contains("E"&& !str.contains(","&& !str.contains("+")
                  
          && !str.contains("-");
              }

              
          public static void main(String[] args) {
              String str 
          = "003E+3";
              
          if (isCorrect(str)) {
                  System.out.println(str 
          + "-->" + removeLast(removeFirst(str)));
              } 
          else {
                  System.out.println(
          "字符串中含有非法字符(,E+-)!");
              }
              str 
          = "0";
              
          if (isCorrect(str)) {
                  System.out.println(str 
          + "-->" + removeLast(removeFirst(str)));
              } 
          else {
                  System.out.println(
          "字符串中含有非法字符,E+-!");
              }
              str 
          = "3";
              
          if (isCorrect(str)) {
                  System.out.println(str 
          + "-->" + removeLast(removeFirst(str)));
              } 
          else {
                  System.out.println(
          "字符串中含有非法字符,E+-!");
              }
              str 
          = "000";
              
          if (isCorrect(str)) {
                  System.out.println(str 
          + "-->" + removeLast(removeFirst(str)));
              } 
          else {
                  System.out.println(
          "字符串中含有非法字符,E+-!");
              }
              str 
          = "01010";
              
          if (isCorrect(str)) {
                  System.out.println(str 
          + "-->" + removeLast(removeFirst(str)));
              } 
          else {
                  System.out.println(
          "字符串中含有非法字符,E+-!");
              }
              str 
          = "301100";
              
          if (isCorrect(str)) {
                  System.out.println(str 
          + "-->" + removeLast(removeFirst(str)));
              } 
          else {
                  System.out.println(
          "字符串中含有非法字符,E+-!");
              }
              str 
          = "00103";
              
          if (isCorrect(str)) {
                  System.out.println(str 
          + "-->" + removeLast(removeFirst(str)));
              } 
          else {
                  System.out.println(
          "字符串中含有非法字符,E+-!");
              }
              str 
          = "1020.00";
              
          if (isCorrect(str)) {
                  System.out.println(str 
          + "-->" + removeLast(removeFirst(str)));
              } 
          else {
                  System.out.println(
          "字符串中含有非法字符,E+-!");
              }
              str 
          = "001.0100";
              
          if (isCorrect(str)) {
                  System.out.println(str 
          + "-->" + removeLast(removeFirst(str)));
              } 
          else {
                  System.out.println(
          "字符串中含有非法字符,E+-!");
              }
              str 
          = "00.003";
              
          if (isCorrect(str)) {
                  System.out.println(str 
          + "-->" + removeLast(removeFirst(str)));
              } 
          else {
                  System.out.println(
          "字符串中含有非法字符,E+-!");
              }
              }
          }


          posted on 2008-03-05 14:21 我為J狂 閱讀(431) 評論(0)  編輯  收藏 所屬分類: JavaSE


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


          網站導航:
           
          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          導航

          統計

          常用鏈接

          留言簿(11)

          隨筆分類(48)

          文章分類(29)

          常去逛逛

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 衡南县| 东平县| 休宁县| 扎鲁特旗| 岳西县| 涞源县| 加查县| 黄浦区| 江北区| 乾安县| 凤台县| 汉川市| 叙永县| 酒泉市| 陈巴尔虎旗| 凯里市| 皮山县| 彭阳县| 迁西县| 孟村| 紫阳县| 康定县| 浦北县| 汉川市| 芮城县| 遵化市| 长葛市| 和龙市| 云林县| 南漳县| 合作市| 连江县| 白朗县| 六安市| 赤壁市| 蒲江县| 凤庆县| 玉龙| 海兴县| 广昌县| 焦作市|