正文

java面试笔试题大汇总(7)2005-12-27 21:19:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/ddtme/8929.html

分享到:


JAVA
编程题
1
.现在输入n个数字,以逗号,分开;然后可选择升或者降序排序;按提交键就在另一页面显示按什么排序,结果为,提供reset
import java.util.*;
public class bycomma{
 public static String[] splitStringByComma(String source){
   if(source==null||source.trim().equals(""))
    return null;
   StringTokenizer commaToker = new StringTokenizer(source,",");
   String[] result = new String[commaToker.countTokens()];
   int i=0;
   while(commaToker.hasMoreTokens()){
    result[i] = commaToker.nextToken();
    i++;
   }
   return result;
 }
 public static void main(String args[]){
 String[] s = splitStringByComma("5,8,7,4,3,9,1");
 int[] ii = new int[s.length];
 for(int i = 0;i ii[i] =Integer.parseInt(s[i]);
 }
    Arrays.sort(ii);
    //asc
    for(int i=0;i    System.out.println(ii[i]);
    }
    //desc
    for(int i=(s.length-1);i>=0;i--){
    System.out.println(ii[i]);
    }
 }
}
2
.金额转换,阿拉伯数字的金额转换成中国传统的形式如:(¥1011)->(一千零一拾一元整)输出。
package test.format;
import java.text.NumberFormat;
import java.util.HashMap;
public class SimpleMoneyFormat {
  public static final String EMPTY = "";
  public static final String ZERO = "
";
  public static final String ONE = "
";
  public static final String TWO = "
";
  public static final String THREE = "
";
  public static final String FOUR = "
";
  public static final String FIVE = "
";
  public static final String SIX = "
";
  public static final String SEVEN = "
";
  public static final String EIGHT = "
";
  public static final String NINE = "
";
  public static final String TEN = "
";
  public static final String HUNDRED = "
";
  public static final String THOUSAND = "
";
  public static final String TEN_THOUSAND = "
";
  public static final String HUNDRED_MILLION = "
亿";
  public static final String YUAN = "
";
  public static final String JIAO = "
";
  public static final String FEN = "
";
  public static final String DOT = ".";

  private static SimpleMoneyFormat formatter = null;
  private HashMap chineseNumberMap = new HashMap();
  private HashMap chineseMoneyPattern = new HashMap();
  private NumberFormat numberFormat = NumberFormat.getInstance();

  private SimpleMoneyFormat() {
    numberFormat.setMaximumFractionDigits(4);
    numberFormat.setMinimumFractionDigits(2);
    numberFormat.setGroupingUsed(false);

    chineseNumberMap.put("0", ZERO);
    chineseNumberMap.put("1", ONE);
    chineseNumberMap.put("2", TWO);
    chineseNumberMap.put("3", THREE);
    chineseNumberMap.put("4", FOUR);
    chineseNumberMap.put("5", FIVE);
    chineseNumberMap.put("6", SIX);
    chineseNumberMap.put("7", SEVEN);
    chineseNumberMap.put("8", EIGHT);
    chineseNumberMap.put("9", NINE);
    chineseNumberMap.put(DOT, DOT);

    chineseMoneyPattern.put("1", TEN);
    chineseMoneyPattern.put("2", HUNDRED);
    chineseMoneyPattern.put("3", THOUSAND);
    chineseMoneyPattern.put("4", TEN_THOUSAND);
    chineseMoneyPattern.put("5", TEN);
    chineseMoneyPattern.put("6", HUNDRED);
    chineseMoneyPattern.put("7", THOUSAND);
    chineseMoneyPattern.put("8", HUNDRED_MILLION);
  }

  public static SimpleMoneyFormat getInstance() {
    if (formatter == null)
      formatter = new SimpleMoneyFormat();
    return formatter;
  }

  public String format(String moneyStr) {
    checkPrecision(moneyStr);
    String result;
    result = convertToChineseNumber(moneyStr);
    result = addUnitsToChineseMoneyString(result);
    return result;
  }

  public String format(double moneyDouble) {
    return format(numberFormat.format(moneyDouble));
  }

  public String format(int moneyInt) {
    return format(numberFormat.format(moneyInt));
  }

  public String format(long moneyLong) {
    return format(numberFormat.format(moneyLong));
  }

  public String format(Number moneyNum) {
    return format(numberFormat.format(moneyNum));
  }

  private String convertToChineseNumber(String moneyStr) {
    String result;
    StringBuffer cMoneyStringBuffer = new StringBuffer();
    for (int i = 0; i < moneyStr.length(); i++) {
      cMoneyStringBuffer.append(chineseNumberMap.get(moneyStr.substring(i, i + 1)));
    }
    //
拾佰仟万亿等都是汉字里面才有的单位,加上它们
    int indexOfDot = cMoneyStringBuffer.indexOf(DOT);
    int moneyPatternCursor = 1;
    for (int i = indexOfDot - 1; i > 0; i--) {
      cMoneyStringBuffer.insert(i, chineseMoneyPattern.get(EMPTY + moneyPatternCursor));
      moneyPatternCursor = moneyPatternCursor == 8 ? 1 : moneyPatternCursor + 1;
    }

    String fractionPart = cMoneyStringBuffer.substring(cMoneyStringBuffer.indexOf("."));
    cMoneyStringBuffer.delete(cMoneyStringBuffer.indexOf("."), cMoneyStringBuffer.length());
    while (cMoneyStringBuffer.indexOf("
零拾") != -1) {
      cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf("
零拾"), cMoneyStringBuffer.indexOf("零拾") + 2, ZERO);
    }
    while (cMoneyStringBuffer.indexOf("
零佰") != -1) {
      cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf("
零佰"), cMoneyStringBuffer.indexOf("零佰") + 2, ZERO);
    }
    while (cMoneyStringBuffer.indexOf("
零仟") != -1) {
      cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf("
零仟"), cMoneyStringBuffer.indexOf("零仟") + 2, ZERO);
    }
    while (cMoneyStringBuffer.indexOf("
零万") != -1) {
      cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf("
零万"), cMoneyStringBuffer.indexOf("零万") + 2, TEN_THOUSAND);
    }
    while (cMoneyStringBuffer.indexOf("
零亿") != -1) {
      cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf("
零亿"), cMoneyStringBuffer.indexOf("零亿") + 2, HUNDRED_MILLION);
    }
    while (cMoneyStringBuffer.indexOf("
零零") != -1) {
      cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf("
零零"), cMoneyStringBuffer.indexOf("零零") + 2, ZERO);
    }
    if (cMoneyStringBuffer.lastIndexOf(ZERO) == cMoneyStringBuffer.length() - 1)
      cMoneyStringBuffer.delete(cMoneyStringBuffer.length() - 1, cMoneyStringBuffer.length());
    cMoneyStringBuffer.append(fractionPart);

    result = cMoneyStringBuffer.toString();
    return result;
  }


  private String addUnitsToChineseMoneyString(String moneyStr) {
    String result;
    StringBuffer cMoneyStringBuffer = new StringBuffer(moneyStr);
    int indexOfDot = cMoneyStringBuffer.indexOf(DOT);
    cMoneyStringBuffer.replace(indexOfDot, indexOfDot + 1, YUAN);

阅读(4022) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册