美文网首页
JAVA编程题

JAVA编程题

作者: yangkg | 来源:发表于2018-11-14 20:23 被阅读27次

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<s.length;i++){

ii[i] =Integer.parseInt(s[i]);

}

    Arrays.sort(ii);

    //asc

    for(int i=0;i<s.length;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);

相关文章

  • JAVA编程题

    JAVA编程题 1.现在输入n个数字,以逗号,分开;然后可选择升或者降序排序;按提交键就在另一页面显示按什么排序,...

  • 美团 Java 面试 154 道题分享!

    Java集合22题 JVM与调优21题 并发编程28题 spring 25题 设计模式 10题 springboo...

  • Java 技术栈

    1、Java基础 Leetcode 刷题、Java 编程思想、JVM 原理、设计模式、Java 8 新特性 2、项目经验

  • Java编程基础自测题

    Java编程基础自测题 共20道题 限时30分钟 你是Java新手还是高手?Java开发基础技术你掌握了多少?快来...

  • 《Python语言程序设计》第一章.练习与作业

    编程题 1.1 编程题 1.2 编程题 1.3 编程题 1.4 编程题 1.5 编程题 1.6 编程题 1.7 编...

  • JAVA 测试III

    记录一下遇见和做过的JAVA编程题... JAVA 测试I http://www.jianshu.com/p/7...

  • Java程序算法题50道

    JAVA基础编程练习50题 本文对50道经典的java程序题进行详细解说,对于初学者可以跳过一些逻辑性太强的题目,...

  • BD1 - Java 1-1

    面试时考查:Java基础逻辑思维读代码能力写代码能力(编程题)算法题 学习过程中 要从网上找题刷题!再看公司的需求...

  • BD1 - Java 1-4 周五总结

    上午小组总结: 关于收获: 面试时考查: Java基础 逻辑思维 读代码能力 写代码能力(编程题) 算法题 学习过...

  • Java编程题90道

    [Java编程题90道] 1.完成数组int[] a = {100,40, 60, 87, 34, 11, 56,...

网友评论

      本文标题:JAVA编程题

      本文链接:https://www.haomeiwen.com/subject/zrlsfqtx.html