两道Java编程题

(1)现在输入n个数字,以逗号,分开;然后可选择升或者降序排序;按提交键就在另一页面显示按什么排序,结果为,提供reset

 1None.gifimport java.util.* ;
 2ExpandedBlockStart.gifContractedBlock.gifpublic class bycomma
{
 3ExpandedSubBlockStart.gifContractedSubBlock.gif public static String[] splitStringByComma(String source)
{
 4InBlock.gif   if(source==null||source.trim().equals(""
))
 5InBlock.gif    return null
;
 6InBlock.gif   StringTokenizer commaToker = new StringTokenizer(source,","
);
 7InBlock.gif   String[] result = new
 String[commaToker.countTokens()];
 8InBlock.gif   int i=0
;
 9ExpandedSubBlockStart.gifContractedSubBlock.gif   while(commaToker.hasMoreTokens())
{
10InBlock.gif    result[i] =
 commaToker.nextToken();
11InBlock.gif    i++
;
12ExpandedSubBlockEnd.gif   }

13InBlock.gif   return result;
14ExpandedSubBlockEnd.gif }

15ExpandedSubBlockStart.gifContractedSubBlock.gif public static void main(String args[]){
16InBlock.gif String[] s = splitStringByComma("5,8,7,4,3,9,1"
);
17InBlock.gif int[] ii = new int
[s.length];
18ExpandedSubBlockStart.gifContractedSubBlock.gif for(int i = 0;i<s.length;i++)
{
19InBlock.gif ii[i] =
Integer.parseInt(s[i]);
20ExpandedSubBlockEnd.gif }

21InBlock.gif    Arrays.sort(ii);
22InBlock.gif    //asc

23ExpandedSubBlockStart.gifContractedSubBlock.gif    for(int i=0;i<s.length;i++){
24
InBlock.gif    System.out.println(ii[i]);
25ExpandedSubBlockEnd.gif    }

26InBlock.gif    //desc
27ExpandedSubBlockStart.gifContractedSubBlock.gif    for(int i=(s.length-1);i>=0;i--){
28
InBlock.gif    System.out.println(ii[i]);
29ExpandedSubBlockEnd.gif    }

30ExpandedSubBlockEnd.gif }

31ExpandedBlockEnd.gif}

(2)金额转换,阿拉伯数字的金额转换成中国传统的形式如:(¥1011)->(一千零一拾一元整)输出。

  1None.gifpackage  test.format;
  2None.gifimport
 java.text.NumberFormat;
  3None.gifimport
 java.util.HashMap;
  4ExpandedBlockStart.gifContractedBlock.gifpublic class SimpleMoneyFormat 
{
  5InBlock.gif  public static final String EMPTY = ""
;
  6InBlock.gif  public static final String ZERO = ""
;
  7InBlock.gif  public static final String ONE = ""
;
  8InBlock.gif  public static final String TWO = ""
;
  9InBlock.gif  public static final String THREE = ""
;
 10InBlock.gif  public static final String FOUR = ""
;
 11InBlock.gif  public static final String FIVE = ""
;
 12InBlock.gif  public static final String SIX = ""
;
 13InBlock.gif  public static final String SEVEN = ""
;
 14InBlock.gif  public static final String EIGHT = ""
;
 15InBlock.gif  public static final String NINE = ""
;
 16InBlock.gif  public static final String TEN = ""
;
 17InBlock.gif  public static final String HUNDRED = ""
;
 18InBlock.gif  public static final String THOUSAND = ""
;
 19InBlock.gif  public static final String TEN_THOUSAND = ""
;
 20InBlock.gif  public static final String HUNDRED_MILLION = "亿"
;
 21InBlock.gif  public static final String YUAN = ""
;
 22InBlock.gif  public static final String JIAO = ""
;
 23InBlock.gif  public static final String FEN = ""
;
 24InBlock.gif  public static final String DOT = "."
;
 25
InBlock.gif
 26InBlock.gif  private static SimpleMoneyFormat formatter = null
;
 27InBlock.gif  private HashMap chineseNumberMap = new
 HashMap();
 28InBlock.gif  private HashMap chineseMoneyPattern = new
 HashMap();
 29InBlock.gif  private NumberFormat numberFormat =
 NumberFormat.getInstance();
 30
InBlock.gif
 31ExpandedSubBlockStart.gifContractedSubBlock.gif  private SimpleMoneyFormat() 
{
 32InBlock.gif    numberFormat.setMaximumFractionDigits(4
);
 33InBlock.gif    numberFormat.setMinimumFractionDigits(2
);
 34InBlock.gif    numberFormat.setGroupingUsed(false
);
 35
InBlock.gif
 36InBlock.gif    chineseNumberMap.put("0"
, ZERO);
 37InBlock.gif    chineseNumberMap.put("1"
, ONE);
 38InBlock.gif    chineseNumberMap.put("2"
, TWO);
 39InBlock.gif    chineseNumberMap.put("3"
, THREE);
 40InBlock.gif    chineseNumberMap.put("4"
, FOUR);
 41InBlock.gif    chineseNumberMap.put("5"
, FIVE);
 42InBlock.gif    chineseNumberMap.put("6"
, SIX);
 43InBlock.gif    chineseNumberMap.put("7"
, SEVEN);
 44InBlock.gif    chineseNumberMap.put("8"
, EIGHT);
 45InBlock.gif    chineseNumberMap.put("9"
, NINE);
 46
InBlock.gif    chineseNumberMap.put(DOT, DOT);
 47
InBlock.gif
 48InBlock.gif    chineseMoneyPattern.put("1"
, TEN);
 49InBlock.gif    chineseMoneyPattern.put("2"
, HUNDRED);
 50InBlock.gif    chineseMoneyPattern.put("3"
, THOUSAND);
 51InBlock.gif    chineseMoneyPattern.put("4"
, TEN_THOUSAND);
 52InBlock.gif    chineseMoneyPattern.put("5"
, TEN);
 53InBlock.gif    chineseMoneyPattern.put("6"
, HUNDRED);
 54InBlock.gif    chineseMoneyPattern.put("7"
, THOUSAND);
 55InBlock.gif    chineseMoneyPattern.put("8"
, HUNDRED_MILLION);
 56ExpandedSubBlockEnd.gif  }

 57InBlock.gif
 58ExpandedSubBlockStart.gifContractedSubBlock.gif  public static SimpleMoneyFormat getInstance() 
{
 59InBlock.gif    if (formatter == null
)
 60InBlock.gif      formatter = new
 SimpleMoneyFormat();
 61InBlock.gif    return
 formatter;
 62ExpandedSubBlockEnd.gif  }

 63InBlock.gif
 64ExpandedSubBlockStart.gifContractedSubBlock.gif  public String format(String moneyStr) 
{
 65
InBlock.gif    checkPrecision(moneyStr);
 66
InBlock.gif    String result;
 67InBlock.gif    result =
 convertToChineseNumber(moneyStr);
 68InBlock.gif    result =
 addUnitsToChineseMoneyString(result);
 69InBlock.gif    return
 result;
 70ExpandedSubBlockEnd.gif  }

 71InBlock.gif
 72ExpandedSubBlockStart.gifContractedSubBlock.gif  public String format(double moneyDouble) 
{
 73InBlock.gif    return
 format(numberFormat.format(moneyDouble));
 74ExpandedSubBlockEnd.gif  }

 75InBlock.gif
 76ExpandedSubBlockStart.gifContractedSubBlock.gif  public String format(int moneyInt) 
{
 77InBlock.gif    return
 format(numberFormat.format(moneyInt));
 78ExpandedSubBlockEnd.gif  }

 79InBlock.gif
 80ExpandedSubBlockStart.gifContractedSubBlock.gif  public String format(long moneyLong) 
{
 81InBlock.gif    return
 format(numberFormat.format(moneyLong));
 82ExpandedSubBlockEnd.gif  }

 83InBlock.gif
 84ExpandedSubBlockStart.gifContractedSubBlock.gif  public String format(Number moneyNum) 
{
 85InBlock.gif    return
 format(numberFormat.format(moneyNum));
 86ExpandedSubBlockEnd.gif  }

 87InBlock.gif
 88ExpandedSubBlockStart.gifContractedSubBlock.gif  private String convertToChineseNumber(String moneyStr) 
{
 89
InBlock.gif    String result;
 90InBlock.gif    StringBuffer cMoneyStringBuffer = new
 StringBuffer();
 91ExpandedSubBlockStart.gifContractedSubBlock.gif    for (int i = 0; i < moneyStr.length(); i++
{
 92InBlock.gif      cMoneyStringBuffer.append(chineseNumberMap.get(moneyStr.substring(i, i + 1
)));
 93ExpandedSubBlockEnd.gif    }

 94InBlock.gif    //拾佰仟万亿等都是汉字里面才有的单位,加上它们
 95InBlock.gif    int indexOfDot = cMoneyStringBuffer.indexOf(DOT);
 96InBlock.gif    int moneyPatternCursor = 1
;
 97ExpandedSubBlockStart.gifContractedSubBlock.gif    for (int i = indexOfDot - 1; i > 0; i--
{
 98InBlock.gif      cMoneyStringBuffer.insert(i, chineseMoneyPattern.get(EMPTY +
 moneyPatternCursor));
 99InBlock.gif      moneyPatternCursor = moneyPatternCursor == 8 ? 1 : moneyPatternCursor + 1
;
100ExpandedSubBlockEnd.gif    }

101InBlock.gif
102InBlock.gif    String fractionPart = cMoneyStringBuffer.substring(cMoneyStringBuffer.indexOf("."
));
103InBlock.gif    cMoneyStringBuffer.delete(cMoneyStringBuffer.indexOf("."
), cMoneyStringBuffer.length());
104ExpandedSubBlockStart.gifContractedSubBlock.gif    while (cMoneyStringBuffer.indexOf("零拾"!= -1
{
105InBlock.gif      cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf("零拾"), cMoneyStringBuffer.indexOf("零拾"+ 2
, ZERO);
106ExpandedSubBlockEnd.gif    }

107ExpandedSubBlockStart.gifContractedSubBlock.gif    while (cMoneyStringBuffer.indexOf("零佰"!= -1{
108InBlock.gif      cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf("零佰"), cMoneyStringBuffer.indexOf("零佰"+ 2
, ZERO);
109ExpandedSubBlockEnd.gif    }

110ExpandedSubBlockStart.gifContractedSubBlock.gif    while (cMoneyStringBuffer.indexOf("零仟"!= -1{
111InBlock.gif      cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf("零仟"), cMoneyStringBuffer.indexOf("零仟"+ 2
, ZERO);
112ExpandedSubBlockEnd.gif    }

113ExpandedSubBlockStart.gifContractedSubBlock.gif    while (cMoneyStringBuffer.indexOf("零万"!= -1{
114InBlock.gif      cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf("零万"), cMoneyStringBuffer.indexOf("零万"+ 2
, TEN_THOUSAND);
115ExpandedSubBlockEnd.gif    }

116ExpandedSubBlockStart.gifContractedSubBlock.gif    while (cMoneyStringBuffer.indexOf("零亿"!= -1{
117InBlock.gif      cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf("零亿"), cMoneyStringBuffer.indexOf("零亿"+ 2
, HUNDRED_MILLION);
118ExpandedSubBlockEnd.gif    }

119ExpandedSubBlockStart.gifContractedSubBlock.gif    while (cMoneyStringBuffer.indexOf("零零"!= -1{
120InBlock.gif      cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf("零零"), cMoneyStringBuffer.indexOf("零零"+ 2
, ZERO);
121ExpandedSubBlockEnd.gif    }

122InBlock.gif    if (cMoneyStringBuffer.lastIndexOf(ZERO) == cMoneyStringBuffer.length() - 1)
123InBlock.gif      cMoneyStringBuffer.delete(cMoneyStringBuffer.length() - 1
, cMoneyStringBuffer.length());
124
InBlock.gif    cMoneyStringBuffer.append(fractionPart);
125
InBlock.gif
126InBlock.gif    result =
 cMoneyStringBuffer.toString();
127InBlock.gif    return
 result;
128ExpandedSubBlockEnd.gif  }

129InBlock.gif
130ExpandedSubBlockStart.gifContractedSubBlock.gif  private String addUnitsToChineseMoneyString(String moneyStr) 
{
131
InBlock.gif    String result;
132InBlock.gif    StringBuffer cMoneyStringBuffer = new
 StringBuffer(moneyStr);
133InBlock.gif    int indexOfDot =
 cMoneyStringBuffer.indexOf(DOT);
134InBlock.gif    cMoneyStringBuffer.replace(indexOfDot, indexOfDot + 1
, YUAN);
135ExpandedSubBlockEnd.gif}

转载于:https://www.cnblogs.com/xxpal/articles/844877.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值