-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyInput.java
More file actions
124 lines (110 loc) · 2.89 KB
/
MyInput.java
File metadata and controls
124 lines (110 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package programCompetition;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
public class MyInput
{
public static void main(String[] args)
{
char[] chars = new char[] {' ','十','百','千','万','十','百','千'};
char[] ch = new char[] {'零','一','二','三','四','五','六','七','八','九'};
BufferedReader br = null;
try{
String input ="";//输入序列
String numStr = "";//输入有效序列
char[] series; //有效输入序列字符数组
for(; ;)
{//直到输入正确时退出循环
br = new BufferedReader(new InputStreamReader(System.in));
input = br.readLine();
if(input.length() > 8)
{
System.out.println("你输入了" + input.length()
+"个字符,用户每次最多输入8个字符,请重新输入:");
continue;
}
series =input.toCharArray();
//确保输入的全部是数字
boolean isRight = true;
for(char c: series)
{
if(c < 48 || c > 57)
{
System.out.println("输入序列不能包括非数字字符,请重新输入:");
isRight = false;
break;
}
numStr += c;
}
if(isRight)
{
continue;
}
break;
}
int data = Integer.parseInt(numStr);// 可消除高位的所有是0的数字
numStr = null;
numStr = data +"";
series = numStr.toCharArray();
if(series.length ==1 && series[0] == 48)
{//全零,输出零
System.out.println(ch[0]);
return;
}
StringBuffer sb = new StringBuffer();
//key = 0表示0, key =1 表示非0数字, value 为数字的下标index
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
int length = series.length;
for(int i = 0; i <length; i++)
{
int index = Integer.parseInt(series[i] + "");
if(index != 0)
{//不是数字0的情况处理
map.put(1,i);
if(map.get(0) != null && map.get(0)<map.get(1))
{
sb.append(ch[0]);
map.put(1, null);
map.put(0, null);
}
if(!(ch[index] == ch[1] && chars[length -i -1] == chars[1]))
{//不是“一十”
sb.append(ch[index]);
}else{//是“一十”,取“一十”or“十”
if(i != 0)
{
int previous = Integer.parseInt(series[i-1] + "");
if(previous != 0)
{// 前一个元素如果不是0,取“一十”,否则取“十”
sb.append(ch[index]);
}
}
}
sb.append(chars[length - i - 1]);
}else{//是数字0的情况处理
map.put(0, i);
if(chars[length - i -1] == chars[4])
{//数字0对应的单位是“万”
sb.append(chars[4]);
}
}
}
System.out.println(sb.toString());
}catch(NumberFormatException e){
System.out.println("用户输入的不都为数字,无法转换");
}catch(IOException e){
e.printStackTrace();
}finally{
try{
if(br != null)
{
br.close();
}
}catch(IOException e){
e.printStackTrace();
}
}
}
}