<!----><!---->
<!---->
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是世界上最流行的编程语言之一,尤其在企业级应用开发领域占据主导地位。..."11Java面试笔试题汇总和答案.pdf"这个资源将提供详细的题目和解答,帮助你全面复习并检查自己的知识盲点,为Java求职面试做好充分准备。
Java是一种广泛使用的面向对象的编程语言,以其平台...以上只是Java面试笔试中可能涉及的部分核心知识点,实际题目会根据应聘者的经验和岗位需求进行深入或扩展。熟练掌握这些知识,能帮助Java开发者在面试中脱颖而出。
java面试笔试资料java笔试题大集合及答案题库java笔试题汇总资料188个合集 100家大公司java笔试题汇总.doc 125条常见的java 面试笔试题大汇总.pdf 2011最新整理java经典代码.doc 25个经典的Spring面试问答.docx ...
java面试笔试资料Java经典项目集锦java笔试题大集合及答案题库java笔试题汇总资料个合集(188) 100家大公司java笔试题汇总.doc 125条常见的java 面试笔试题大汇总.pdf 2011最新整理java经典代码.doc 25个经典的Spring...
### Java面试笔试题知识点概述 #### 一、Java中的String与StringBuffer详解 在Java编程语言中,处理字符串是一项常见的任务。为了满足不同场景下的需求,Java提供了多种类型的字符串类,其中最为常用的便是`String...
这份“Java面试笔试题大汇总”资料可能包含以上各个领域的经典题目和解析,通过系统学习和实践,可以帮助求职者全面提升Java技术水平,以应对各种面试挑战。CSDNBlog.htm和CSDNBlog_files两个文件可能包含了文章内容...
以下是一些主要的Java面试笔试题相关的知识点,包括但不限于: 1. **基础语法**:理解基本数据类型、变量、运算符、流程控制(if、switch、for、while)、数组、字符串等。面试中可能会考察对Java内存模型的理解,...
100家大公司java笔试题汇总.doc 125条常见的java 面试笔试题大汇总.pdf 2011最新整理java经典代码.doc 25个经典的Spring面试问答.docx 8张图解java.docx Addison.Wesley.Java.Concurrency.in.Practice.May.2006.chm ...
这份“125条常见的Java面试笔试题大汇总”涵盖了Java的基础、进阶以及实际应用等多个方面,旨在帮助求职者全面了解和准备Java相关的面试与笔试。 首先,基础部分包括了对Java语法的理解,例如: 1. Java的三大特性...
100家大公司java笔试题汇总.doc 125条常见的java 面试笔试题大汇总.pdf 2011最新整理java经典代码.doc 25个经典的Spring面试问答.docx 8张图解java.docx Addison.Wesley.Java.Concurrency.in.Practice.May.2006.chm ...
Java是世界上最流行的编程语言之一,尤其在企业级应用开发领域占据主导地位。为了在Java开发岗位的竞争中..."java面试笔试题大汇总.pdf"这份文档将详细解析这些知识点,帮助你更好地准备面试,祝你在求职路上一帆风顺!