主頁(yè) > 教育培訓(xùn) > 電腦培訓(xùn) > java面試題及答案 java筆試試題練習(xí)(7)

java面試題及答案 java筆試試題練習(xí)(7)

  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 = "萬(wàn)";

  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)));

  }

  //拾佰仟萬(wàn)億等都是漢字里面才有的單位,加上它們

  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("零萬(wàn)") != -1) {

  cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf("零萬(wàn)"), cMoneyStringBuffer.indexOf("零萬(wàn)") + 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);

  cMoneyStringBuffer.insert(cMoneyStringBuffer.length() - 1, JIAO);

  cMoneyStringBuffer.insert(cMoneyStringBuffer.length(), FEN);

  if (cMoneyStringBuffer.indexOf("零角零分") != -1)//沒(méi)有零頭,加整

  cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf("零角零分"), cMoneyStringBuffer.length(), "整");

  else

  if (cMoneyStringBuffer.indexOf("零分") != -1)//沒(méi)有零分,加整

  cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf("零分"), cMoneyStringBuffer.length(), "整");

  else {

  if(cMoneyStringBuffer.indexOf("零角")!=-1)

  cMoneyStringBuffer.delete(cMoneyStringBuffer.indexOf("零角"),cMoneyStringBuffer.indexOf("零角")+2);

  // tmpBuffer.append("整");

  }

  result = cMoneyStringBuffer.toString();

  return result;

  }

  private void checkPrecision(String moneyStr) {

  int fractionDigits = moneyStr.length() - moneyStr.indexOf(DOT) - 1;

  if (fractionDigits > 2)

  throw new RuntimeException("金額" + moneyStr + "的小數(shù)位多于兩位。"); //精度不能比分低

  }

  public static void main(String args[]) {

  System.out.println(getInstance().format(new Double(10010001.01)));

  }

  }

  3、繼承時(shí)候類的執(zhí)行順序問(wèn)題,一般都是選擇題,問(wèn)你將會(huì)打印出什么?

  答:父類:

  package test;

  public class FatherClass {

  public FatherClass() {

  System.out.println("FatherClass Create");

  }

  }

  子類:

  package test;

  import test.FatherClass;

  public class ChildClass extends FatherClass {

  public ChildClass() {

  System.out.println("ChildClass Create");

  }

  public static void main(String[] args) {

  FatherClass fc = new FatherClass();

  ChildClass cc = new ChildClass();

  }

  }

  輸出結(jié)果:

  C:>java test.ChildClass

  FatherClass Create

  FatherClass Create

  ChildClass Create

免責(zé)聲明:該文觀點(diǎn)僅代表作者本人,查查吧平臺(tái)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)空間服務(wù),不承擔(dān)相關(guān)法律責(zé)任。圖片涉及侵權(quán)行為,請(qǐng)發(fā)送郵件至85868317@qq.com舉報(bào),一經(jīng)查實(shí),本站將立刻刪除。返回查查吧首頁(yè),查看更多>>
提示

該文觀點(diǎn)僅代表作者本人,查查吧平臺(tái)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)空間服務(wù),不承擔(dān)相關(guān)法律責(zé)任。圖片涉及侵權(quán)行為,請(qǐng)發(fā)送郵件至85868317@qq.com舉報(bào),一經(jīng)查實(shí),本站將立刻刪除。

優(yōu)惠商城

更多